Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added ability to accept epoch when moment is initialized #18

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ env:
before_install:
- echo 'Canada/Pacific' | sudo tee /etc/timezone
- sudo dpkg-reconfigure --frontend noninteractive tzdata
- sudo ntpdate ntp.ubuntu.com

install: ant -Dtest.framework=$TESTFRAMEWORK -Dsource=remote -Dwork.dir=$HOME/work -Dbuild.dir=$TRAVIS_BUILD_DIR -Dplatform=$PLATFORM install-ci-deps
script: ant -Dtest.framework=$TESTFRAMEWORK -Dsource=remote -Dwork.dir=$HOME/work -Dbuild.dir=$TRAVIS_BUILD_DIR -Dplatform=$PLATFORM test-ci
168 changes: 167 additions & 1 deletion moment.cfc
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ component displayname="moment" {
-- for instance initialized to someTimeValue in someTZID TZ
*/
public function init( time = now(), zone = getSystemTZ() ) {
this.time = (time contains '{ts') ? time : parseDateTime( arguments.time );
if (isNumeric(time))
this.time = epochToDate(arguments.time);
else
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 );
Expand Down Expand Up @@ -63,6 +67,78 @@ component displayname="moment" {
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
//===========================================
Expand Down Expand Up @@ -212,6 +288,72 @@ component displayname="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
//===========================================
Expand Down Expand Up @@ -244,7 +386,17 @@ component displayname="moment" {
//===========================================
//INTERNAL HELPERS
//===========================================

private function epochToDate( epoch ){
var d = "";
if (isValid("integer",arguments.epoch))
d = createObject("java","java.util.Date").init(javacast("long",arguments.epoch*1000));
else if (isNumeric(arguments.epoch) and val(arguments.epoch) gt 1000)
d = createObject("java","java.util.Date").init(javacast("long",arguments.epoch));

return d;
}

private function getSystemTimeMS(){
return createObject('java', 'java.lang.System').currentTimeMillis();
}
Expand Down Expand Up @@ -275,29 +427,35 @@ component displayname="moment" {
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':
Expand All @@ -307,18 +465,22 @@ component displayname="moment" {
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');
Expand All @@ -332,4 +494,8 @@ component displayname="moment" {
return (targetOffset - startOffset) * 1000;
}

private function getDatePart( datePart ){
return val( format( canonicalizeDatePart( arguments.datePart ) ) );
}

}
25 changes: 25 additions & 0 deletions readme.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ Here's a list of all masks you can use with add/subtract:

**\* \* Another deviation from the official dateAdd mask:** `ms` just makes more sense than the `l` (lower case L) that Adobe uses.

#### startOf / endOf

Returns a new moment instance with the date/time shifted to the start or end of the specified date part. For example, the end of the current week*:

endOfWeek = new moment().endOf('week');

Or the start of the next quarter:

nextQuarter = new moment().startOf('quarter').add(1, 'quarter');

**\* Weeks are assumed to be Sun-Sat**

#### Clone

Returns a new moment instance with the same datetime and time zone.
Expand Down Expand Up @@ -163,6 +175,19 @@ After all is said and done, sometimes you just need the raw datetime object back
raw = new moment( '2008-11-27 13:47' ).getDateTime();
//=> {ts '2008-11-27 13:47:00'}

#### year, month, day, hour, minute, second

Read or write just the year portion of the moment. When updating, returns a new moment instance.

x = new moment();
x.year();
//=> returns the current year (numeric)

x.year( 2000 );
//=> returns a new moment with the date/time rewound to the same moment in the year 2000

The same pattern repeats for each of the following methods: `year()`, `month()`, `day()`, `hour()`, `minute()`, `second()`. Execute it with no arguments to get the current value, or execute it with an appropriate argument value to return a new moment maching the same date and time except for the specified date-part modification.

## Time Zones

In addition to all of the great date math you can do (with moment's better syntax), moment also has baked-in support for Time Zone functionality, using the robust underlying `java.util.TimeZone` class.
Expand Down
Loading