-
Notifications
You must be signed in to change notification settings - Fork 20
/
moment.cfc
executable file
·497 lines (443 loc) · 16.1 KB
/
moment.cfc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
MOMENT.CFC
-------------------
Inspired by (but not a strict port of) moment.js: http://momentjs.com/
With help from: @seancorfield, @ryanguill
And contributions (witting or otherwise) from:
- Dan Switzer: https://github.com/CounterMarch/momentcfc/issues/5
- Ryan Heldt: http://www.ryanheldt.com/post.cfm/working-with-fuzzy-dates-and-times
- Ben Nadel: http://www.bennadel.com/blog/2501-converting-coldfusion-date-time-values-into-iso-8601-time-strings.htm
- Zack Pitts: http://stackoverflow.com/a/16309780/751
*/
component displayname="moment" {
this.zone = '';
this.time = '';
this.utcTime = '';
this.localTime = '';
/*
Call:
new moment();
-- for instance initalized to current time in current system TZ
new moment( someTimeValue );
-- for instance initialized to someTimeValue in current system TZ
new moment( someTimeValue, someTZID )
-- for instance initialized to someTimeValue in someTZID TZ
*/
public function init( time = now(), zone = getSystemTZ() ) {
this.time = (time contains '{ts') ? time : parseDateTime( arguments.time );
this.zone = zone;
this.utc_conversion_offset = getTargetOffsetDiff( getSystemTZ(), zone, time );
this.utcTime = TZtoUTC( arguments.time, arguments.zone );
this.localTime = UTCtoTZ( this.utcTime, getSystemTZ() );
return this;
}
//===========================================
//MUTATORS
//===========================================
public function utc() hint="convert datetime to utc zone" {
this.time = this.utcTime;
this.zone = 'UTC';
return this;
}
public function tz( required string zone ) hint="convert datetime to specified zone" {
// this.utc_conversion_offset = getZoneCurrentOffset( arguments.zone ) * 1000;
this.utc_conversion_offset = getTargetOffsetDiff( getSystemTZ(), this.zone, this.time );
this.time = UTCtoTZ( this.utcTime, arguments.zone );
this.zone = arguments.zone;
return this;
}
public function add( required numeric amount, required string part ){
part = canonicalizeDatePart( part, 'dateAdd' );
this.time = dateAdd( part, amount, this.time );
this.utcTime = TZtoUTC( this.time, this.zone );
this.localTime = UTCtoTZ( this.utcTime, getSystemTZ() );
return this;
}
public function subtract( required numeric amount, required string part ){
return add( -1 * amount, part );
}
public function startOf( required string part ){
part = canonicalizeDatePart(part, "startOf");
var dest = '';
switch (part){
case 'year':
dest = createDateTime(year(this.localTime),1,1,0,0,0);
break;
case 'quarter':
dest = createDateTime(year(this.localTime),(int((month(this.localTime)-1)/3)+1)*3-2,1,0,0,0);
break;
case 'month':
dest = createDateTime(year(this.localTime),month(this.localTime),1,0,0,0);
break;
case 'week':
dest = createDateTime(year(this.localTime),month(this.localTime),day(this.localTime),0,0,0);
dest = dateAdd("d", (dayOfWeek(dest)-1)*-1, dest);
break;
case 'day':
dest = createDateTime(year(this.localTime),month(this.localTime),day(this.localTime),0,0,0);
break;
case 'hour':
dest = createDateTime(year(this.localTime),month(this.localTime),day(this.localTime),hour(this.localTime),0,0);
break;
case 'minute':
dest = createDateTime(year(this.localTime),month(this.localTime),day(this.localTime),hour(this.localTime),minute(this.localTime),0);
break;
default:
throw(message="Invalid date part value, expected one of: year, quarter, month, week, day, hour, minute; or one of their acceptable aliases (see dateTimeFormat docs)");
}
return init( dest, this.zone );
}
public function endOf(required string part) {
part = canonicalizeDatePart(part, "startOf");
var dest = '';
switch (part){
case 'year':
dest = createDateTime(year(this.localTime),12,31,23,59,59);
break;
case 'quarter':
dest = createDateTime(year(this.localTime),(int((month(this.localTime)-1)/3)+1)*3,1,23,59,59); //first day of last month of quarter (e.g. 12/1)
dest = dateAdd('m', 1, dest); //first day of following month
dest = dateAdd('d', -1, dest); //last day of last month of quarter
break;
case 'month':
dest = createDateTime(year(this.localTime),month(this.localTime),1,23,59,59); //first day of month
dest = dateAdd('m', 1, dest); //first day of following month
dest = dateAdd('d', -1, dest); //last day of target month
break;
case 'week':
dest = createDateTime(year(this.localTime),month(this.localTime),day(this.localTime),23,59,59);
dest = dateAdd("d", (7-dayOfWeek(dest)), dest);
break;
case 'day':
dest = createDateTime(year(this.localTime),month(this.localTime),day(this.localTime),23,59,59);
break;
case 'hour':
dest = createDateTime(year(this.localTime),month(this.localTime),day(this.localTime),hour(this.localTime),59,59);
break;
case 'minute':
dest = createDateTime(year(this.localTime),month(this.localTime),day(this.localTime),hour(this.localTime),minute(this.localTime),59);
break;
default:
throw(message="Invalid date part value, expected one of: year, quarter, month, week, day, hour, minute; or one of their acceptable aliases (see dateTimeFormat docs)");
}
return init( dest, this.zone );
}
//===========================================
//STATICS
//===========================================
public moment function clone() hint="returns a new instance with the same time & zone" {
return new moment( this.time, this.zone );
}
public moment function min( required moment a, required moment b ) hint="returns whichever moment came first" {
if ( a.isBefore( b ) ){
return a;
}
return b;
}
public moment function max( required moment a, required moment b ) hint="returns whichever moment came last" {
if ( a.isAfter( b ) ){
return a;
}
return b;
}
public numeric function diff( required moment b, part = 'seconds' ) hint="get the difference between the current date and the specified date" {
part = canonicalizeDatePart( part, 'dateDiff' );
if (part == 'L'){ //custom support for millisecond diffing... because adobe couldn't be bothered to support it themselves
return b.epoch() - this.epoch();
}
return dateDiff( part, this.getDateTime(), b.getDateTime() );
}
public function getZoneCurrentOffset( required string zone ) hint="returns the offset in seconds (considering DST) of the specified zone" {
return getTZ( arguments.zone ).getOffset( getSystemTimeMS() ) / 1000;
}
public string function getSystemTZ(){
return createObject('java', 'java.util.TimeZone').getDefault().getId();
}
public struct function getZoneTable(){
var list = createObject('java', 'java.util.TimeZone').getAvailableIDs();
var data = {};
for (tz in list){
//display *CURRENT* offsets
var ms = getTZ( tz ).getOffset( getSystemTimeMS() );
data[ tz ] = readableOffset( ms );
}
return data;
}
public function getArbitraryTimeOffset( required time, required string zone ) hint="returns what the offset was at that specific moment"{
var timezone = getTZ( zone );
//can't use a moment for this math b/c it would cause infinite recursion: constructor uses this method
var epic = createDateTime(1970, 1, 1, 0, 0, 0);
var seconds = timezone.getOffset( javacast('long', dateDiff('s', epic, arguments.time)*1000) ) / 1000;
return seconds;
}
//===========================================
//TERMINATORS
//===========================================
public function format( required string mask ) hint="return datetime formatted with specified mask (dateTimeFormat mask rules)" {
switch( mask ){
case 'mysql':
mask = 'yyyy-mm-dd HH:nn:ss';
break;
case 'iso8601':
case 'mssql':
return dateTimeFormat(this.time, 'yyyy-mm-dd') & 'T' & dateTimeFormat(this.time, 'HH:nn:ss') & 'Z';
default:
mask = mask;
}
return dateTimeFormat( this.localTime, mask, this.zone );
}
public function from( required moment compare ) hint="returns fuzzy-date string e.g. 2 hours ago" {
var base = this.clone().utc();
var L = this.min( base, compare.clone().utc() ).getDateTime();
var R = this.max( base, compare.clone().utc() ).getDateTime();
var diff = 0;
//Seconds
if (dateDiff('s', L, R) < 60){
return 'Just now';
}
//Minutes
diff = dateDiff('n', L, R);
if (diff < 60){
return diff & " minute#(diff gt 1 ? 's' : '')# ago";
}
//Hours
diff = dateDiff('h', L, R);
if (diff < 24){
return diff & " hour#(diff gt 1 ? 's' : '')# ago";
}
//Days
diff = dateDiff('d', L, R);
if (diff < 7){
if (diff < 2){
return 'Yesterday';
}else if (diff >= 2){
return diff & ' days ago';
}
}
//Weeks
diff = dateDiff('ww', L, R);
if (diff == 1){
return '1 week ago';
}else if (diff lte 4){
return diff & ' weeks ago';
}
//Months/Years
diff = dateDiff('m', L, R);
if (diff < 12){
return diff & " month#(diff gt 1 ? 's' : '')# ago";
}else if (diff == 12){
return 'Last year';
}else{
diff = dateDiff('yyyy', L, R);
return diff & " year#(diff gt 1 ? 's' : '')# ago";
}
}
public function fromNow() {
var nnow = new moment().clone().utc();
return from( nnow );
}
public function epoch() hint="returns the number of milliseconds since 1/1/1970 (local). Call .utc() first to get utc epoch" {
/*
It seems that we can't get CF to give us an actual UTC datetime object without using DateConvert(), which we
can not rely on, because it depends on the system time being the local time converting from/to. Instead, we've
devised a system of detecting the target time zone's offset and using it here (the only place it seems necessary)
to return the expected epoch values.
*/
return this.clone().getDateTime().getTime() - this.utc_conversion_offset;
var adjustment = (this.utc_conversion_offset > 0) ? -1 : 1;
return this.clone().getDateTime().getTime();
return this.clone().getDateTime().getTime() - (this.utc_conversion_offset * adjustment);
}
public function getDateTime() hint="return raw datetime object in current zone" {
return this.time;
}
public string function getZone() hint="return the current zone" {
return this.zone;
}
public numeric function getOffset() hint="returns the offset in seconds (considering DST) of the current moment" {
return getArbitraryTimeOffset( this.time, this.zone );
}
public function year( newYear = '' ){
if ( newYear == '' ){
return getDatePart( 'year' );
}else{
return init(
time: createDateTime( newYear, month(this.time), day(this.time), hour(this.time), minute(this.time), second(this.time) )
,zone: this.zone
);
}
}
public function month( newMonth = '' ){
if ( newMonth == '' ){
return getDatePart( 'month' );
}else{
return init(
time: createDateTime( year(this.time), newMonth, day(this.time), hour(this.time), minute(this.time), second(this.time) )
,zone: this.zone
);
}
}
public function day( newDay = '' ){
if ( newDay == '' ){
return getDatePart( 'day' );
}else{
return init(
time: createDateTime( year(this.time), month(this.time), newDay, hour(this.time), minute(this.time), second(this.time) )
,zone: this.zone
);
}
}
public function hour( newHour = '' ){
if ( newHour == '' ){
return getDatePart( 'hour' );
}else{
return init(
time: createDateTime( year(this.time), month(this.time), day(this.time), newHour, minute(this.time), second(this.time) )
,zone: this.zone
);
}
}
public function minute( newMinute = '' ){
if ( newMinute == '' ){
return getDatePart( 'minute' );
}else{
return init(
time: createDateTime( year(this.time), month(this.time), day(this.time), hour(this.time), newMinute, second(this.time) )
,zone: this.zone
);
}
}
public function second( newSecond = '' ){
if ( newSecond == '' ){
return getDatePart( 'second' );
}else{
return init(
time: createDateTime( year(this.time), month(this.time), day(this.time), hour(this.time), minute(this.time), newSecond )
,zone: this.zone
);
}
}
//===========================================
//QUERY
//===========================================
public boolean function isBefore( required moment compare, part = 'seconds' ) {
part = canonicalizeDatePart( part, 'dateCompare' );
return (dateCompare( this.time, compare.getDateTime(), part ) == -1);
}
public boolean function isSame( required moment compare, part = 'seconds' ) {
part = canonicalizeDatePart( part, 'dateCompare' );
return (dateCompare( this.time, compare.getDateTime(), part ) == 0);
}
public boolean function isAfter( required moment compare, part = 'seconds' ) {
part = canonicalizeDatePart( part, 'dateCompare' );
return (dateCompare( this.time, compare.getDateTime(), part ) == 1);
}
public boolean function isBetween( required moment a, required moment c, part = 'seconds' ) {
part = canonicalizeDatePart( part, 'dateCompare' );
if ( isBefore(c, part) && isAfter(a, part) ){
return true;
}else if ( isBefore(a, part) && isAfter(c, part) ){
return true;
}
return false;
}
public boolean function isDST() {
var dt = createObject('java', 'java.util.Date').init( this.epoch() );
return getTZ( this.zone ).inDayLightTime( dt );
}
//===========================================
//INTERNAL HELPERS
//===========================================
private function getSystemTimeMS(){
return createObject('java', 'java.lang.System').currentTimeMillis();
}
private function getTZ( id ){
return createObject('java', 'java.util.TimeZone').getTimezone( id );
}
private function TZtoUTC( time, tz = getSystemTZ() ){
var seconds = getArbitraryTimeOffset( time, tz );
return dateAdd( 's', -1 * seconds, time );
}
private function UTCtoTZ( required time, required string tz ){
var seconds = getArbitraryTimeOffset( time, tz );
return dateAdd( 's', seconds, time );
}
private function readableOffset( offset ){
var h = offset / 1000 / 60 / 60; //raw hours (decimal) offset
var hh = fix( h ); //int hours
var mm = ( hh == h ? ':00' : ':' & abs(round((h-hh)*60)) ); //hours modulo used to determine minutes
var rep = ( h >= 0 ? '+' : '' ) & hh & mm;
return rep;
}
private function canonicalizeDatePart( part, method = 'dateAdd' ){
var isDateAdd = (lcase(method) == 'dateadd');
var isDateDiff = (lcase(method) == 'datediff');
var isDateCompare = (lcase(method) == 'datecompare');
var isStartOf = (lcase(method) == 'startof');
switch( lcase(arguments.part) ){
case 'years':
case 'year':
case 'y':
if (isStartOf) return 'year';
return 'yyyy';
case 'quarters':
case 'quarter':
case 'q':
if (isStartOf) return 'quarter';
if (!isDateCompare) return 'q';
throw(message='DateCompare doesn''t support Quarter precision');
case 'months':
case 'month':
case 'm':
if (isStartOf) return 'month';
return 'm';
case 'weeks':
case 'week':
case 'w':
if (isStartOf) return 'week';
if (!isDateCompare) return 'ww';
throw(message='DateCompare doesn''t support Week precision');
case 'days':
case 'day':
case 'd':
if (isStartOf) return 'day';
return 'd';
case 'weekdays':
case 'weekday':
case 'wd':
if (!isDateCompare) return 'w';
throw(message='DateCompare doesn''t support Weekday precision');
case 'hours':
case 'hour':
case 'h':
if (isStartOf) return 'hour';
return 'h';
case 'minutes':
case 'minute':
case 'n':
if (isStartOf) return 'minute';
return 'n';
case 'seconds':
case 'second':
case 's':
if (isStartOf) return 'second';
return 's';
case 'milliseconds':
case 'millisecond':
case 'ms':
if (isStartOf) return 'millisecond';
if (isDateAdd) return 'L';
if (isDateDiff) return 'L'; //custom support for ms diffing is provided interally, because adobe sucks
throw(message='#method# doesn''t support Millisecond precision');
}
throw(message='Unrecognized Date Part: `#part#`');
}
private function getTargetOffsetDiff( sourceTZ, destTZ, time ) hint="used to calculate what the custom offset should be, based on current target and new target"{
var startOffset = getArbitraryTimeOffset( time, sourceTZ );
var targetOffset = getArbitraryTimeOffset( time, destTZ );
return (targetOffset - startOffset) * 1000;
}
private function getDatePart( datePart ){
return val( format( canonicalizeDatePart( arguments.datePart ) ) );
}
}