From 1d36fda239fa6ca2093ce485414a7b20e57e9bcb Mon Sep 17 00:00:00 2001 From: Vadim Ogievetsky Date: Thu, 7 Nov 2013 18:20:47 -0800 Subject: [PATCH] added isDST and getAbbreviation to TimeZoneTime --- client/commonjs/olson/timezonetime.js | 8 ++++++++ client/walltime.js | 8 ++++++++ client/walltime.min.js | 2 +- lib/olson/timezonetime.coffee | 12 ++++++++---- test/timezonetime_spec.coffee | 26 ++++++++++++++++++++------ 5 files changed, 45 insertions(+), 11 deletions(-) diff --git a/client/commonjs/olson/timezonetime.js b/client/commonjs/olson/timezonetime.js index e417104..4a718b1 100644 --- a/client/commonjs/olson/timezonetime.js +++ b/client/commonjs/olson/timezonetime.js @@ -95,6 +95,14 @@ return base - dst; }; + TimeZoneTime.prototype.isDST = function() { + return this.save.hours !== 0 || this.save.mins !== 0; + }; + + TimeZoneTime.prototype.getAbbreviation = function() { + return this.zone.format.replace('%s', this.isDST() ? 'D' : 'S'); + }; + TimeZoneTime.prototype.toISOString = function() { return this.utc.toISOString(); }; diff --git a/client/walltime.js b/client/walltime.js index 3c037b6..ef6f742 100644 --- a/client/walltime.js +++ b/client/walltime.js @@ -450,6 +450,14 @@ return base - dst; }; + TimeZoneTime.prototype.isDST = function() { + return this.save.hours !== 0 || this.save.mins !== 0; + }; + + TimeZoneTime.prototype.getAbbreviation = function() { + return this.zone.format.replace('%s', this.isDST() ? 'D' : 'S'); + }; + TimeZoneTime.prototype.toISOString = function() { return this.utc.toISOString(); }; diff --git a/client/walltime.min.js b/client/walltime.min.js index 89a9281..4fae5ce 100644 --- a/client/walltime.min.js +++ b/client/walltime.min.js @@ -3,4 +3,4 @@ * Copyright (c) 2013 Sprout Social, Inc. * Available under the MIT License (http://bit.ly/walltime-license) */ - (function(){var e,t,n,r,i,s;(s=Array.prototype).indexOf||(s.indexOf=function(e){var t,n,r,i;for(t=r=0,i=this.length;r]?=)([0-9]*)"),MonthIndex:function(e){return this.MonthsShortNames.indexOf(e.slice(0,3))},IsDayOfMonthRule:function(e){return e.indexOf(">")>-1||e.indexOf("<")>-1||e.indexOf("=")>-1},IsLastDayOfMonthRule:function(e){return e.slice(0,4)==="last"},DayOfMonthByRule:function(e,t,n){var r,s,o,u,a,f,l,c,h;f=this.CompareRuleMatch.exec(e);if(!f)throw new Error("Unable to parse the 'on' rule for "+e);h=f.slice(1,4),a=h[0],c=h[1],o=h[2],o=parseInt(o,10);if(o===NaN)throw new Error("Unable to parse the dateIndex of the 'on' rule for "+e);u=i.Days.DayIndex(a),s={">=":function(e,t){return e>=t},"<=":function(e,t){return e<=t},">":function(e,t){return e>t},"<":function(e,t){return e12&&!e&&(t-=12),t===0&&(t=12),r=this.getMinutes(),r<10&&(r="0"+r),n=i>11?" PM":" AM",e&&(n=""),""+t+":"+r+n},t.prototype.setTime=function(t){return this.wallTime=e.Time.UTCToWallTime(new Date(t),this.zone.offset,this.save),this._updateUTC()},t.prototype.setFullYear=function(e){return this.wallTime.setUTCFullYear(e),this._updateUTC()},t.prototype.setMonth=function(e){return this.wallTime.setUTCMonth(e),this._updateUTC()},t.prototype.setDate=function(e){return this.wallTime.setUTCDate(e),this._updateUTC()},t.prototype.setHours=function(e){return this.wallTime.setUTCHours(e),this._updateUTC()},t.prototype.setMinutes=function(e){return this.wallTime.setUTCMinutes(e),this._updateUTC()},t.prototype.setSeconds=function(e){return this.wallTime.setUTCSeconds(e),this._updateUTC()},t.prototype.setMilliseconds=function(e){return this.wallTime.setUTCMilliseconds(e),this._updateUTC()},t.prototype._updateUTC=function(){return this.utc=e.Time.WallTimeToUTC(this.offset,this.save,this.getFullYear(),this.getMonth(),this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds(),this.getMilliseconds()),this.utc.getTime()},t}(),t},typeof window=="undefined"?(t=require("./helpers"),module.exports=e(t)):typeof define!="undefined"?define("olson/timezonetime",["olson/helpers"],e):(this.WallTime||(this.WallTime={}),this.WallTime.TimeZoneTime=e(this.WallTime.helpers))}.call(this),function(){var e,t,n,r={}.hasOwnProperty;e=function(e,t){var n,i,s,o,u,a;return s=function(){function e(){}return e.prototype.applies=function(e){return!isNaN(parseInt(e,10))},e.prototype.parseDate=function(e){return parseInt(e,10)},e}(),i=function(){function t(){}return t.prototype.applies=e.Months.IsLastDayOfMonthRule,t.prototype.parseDate=function(t,n,r,i,s,o){return e.Months.LastDayOfMonthRule(t,n,r)},t}(),n=function(){function t(){}return t.prototype.applies=e.Months.IsDayOfMonthRule,t.prototype.parseDate=function(t,n,r){return e.Months.DayOfMonthByRule(t,n,r)},t}(),o=function(){function t(t,n,r,i,s,o,u,a,f){var l,c,h,p;this.name=t,this._from=n,this._to=r,this.type=i,this["in"]=s,this.on=o,this.at=u,this._save=a,this.letter=f,this.from=parseInt(this._from,10),this.isMax=!1,h=this.from;switch(this._to){case"max":h=e.Time.MaxDate().getUTCFullYear(),this.isMax=!0;break;case"only":h=this.from;break;default:h=parseInt(this._to,10)}this.to=h,p=this._parseTime(this._save),l=p[0],c=p[1],this.save={hours:l,mins:c}}return t.prototype.forZone=function(t){return this.offset=t,this.fromUTC=e.Time.MakeDateFromParts(this.from,0,1,0,0,0),this.fromUTC=e.Time.ApplyOffset(this.fromUTC,t),this.toUTC=e.Time.MakeDateFromParts(this.to,11,31,23,59,59,999),this.toUTC=e.Time.ApplyOffset(this.toUTC,t)},t.prototype.setOnUTC=function(t,n,r){var i,s,o,u,a,f,l,c=this;return f=e.Months.MonthIndex(this["in"]),s=parseInt(this.on,10),o=isNaN(s)?this._parseOnDay(this.on,t,f):s,l=this._parseTime(this.at),u=l[0],a=l[1],i=l[2],this.onUTC=e.Time.MakeDateFromParts(t,f,o,u,a),this.onUTC.setUTCMilliseconds(this.onUTC.getUTCMilliseconds()-1),this.atQualifier=i!==""?i:"w",this.onUTC=e.Time.QualifiedTimeToUTC(this.onUTC,this.atQualifier,n,function(){return r(c)}),this.onSort=""+f+"-"+o+"-"+this.onUTC.getUTCHours()+"-"+this.onUTC.getUTCMinutes()},t.prototype.appliesToUTC=function(e){return this.fromUTC<=e&&e<=this.toUTC},t.prototype._parseOnDay=function(e,t,r){var o,u,a,f;u=[new s,new i,new n];for(a=0,f=u.length;au)u=f.to;o[f.to]=o[f.to]||[],o[f.to].push(f),i[f.from]=i[f.from]||[],i[f.from].push(f)}this.minYear=a,this.maxYear=u,s=function(n,i){var s,u,a,l;n==null&&(n="toUTC"),i==null&&(i=o),l=[];for(u in i){if(!r.call(i,u))continue;t=i[u],a=p.allThatAppliesTo(t[0][n]);if(a.length<1)continue;t=p._sortRulesByOnTime(t),s=a.slice(-1)[0];if(s.save.hours===0&&s.save.mins===0)continue;l.push(function(){var r,i,o;o=[];for(r=0,i=t.length;r0&&(s=r.slice(-1)[0].save),new t(n,this.timeZone,s)},n.prototype.getUTCForWallTime=function(t){var n,r,i,s,o,u,a,f;u=e.Time.StandardTimeToUTC(this.timeZone.offset,t),o=function(){var e,t,n,r;n=this.rules,r=[];for(e=0,t=n.length;e0&&(i=n.slice(-1)[0].save),e.Time.WallTimeToUTC(this.timeZone.offset,i,t)},n.prototype.getYearEndDST=function(t){var n,r,i,s,o,u,a,f,l;a=typeof t===number?t:t.getUTCFullYear(),u=e.Time.StandardTimeToUTC(this.timeZone.offset,a,11,31,23,59,59),o=function(){var e,t,n,r;n=this.rules,r=[];for(e=0,t=n.length;e0&&(i=n.slice(-1)[0].save),i},n.prototype.isAmbiguous=function(t){var n,r,i,s,o,u,a,f,l,c,h,p,d,v;p=e.Time.StandardTimeToUTC(this.timeZone.offset,t),l=function(){var e,t,n,r;n=this.rules,r=[];for(e=0,t=n.length;er.end.getTime()&&(i=r.begin,r.begin=r.end,r.end=i),r},o=c?h.last:-h.prev,a=s(i.onUTC,o),p=e.Time.WallTimeToUTC(this.timeZone.offset,u,t),a.begin<=p&&p<=a.end))},n.prototype._sortRulesByOnTime=function(t){return t.sort(function(t,n){return e.Months.MonthIndex(t["in"])-e.Months.MonthIndex(n["in"])})},n}(),a={Rule:o,RuleSet:u,OnFieldHandlers:{NumberHandler:s,LastHandler:i,CompareHandler:n}},a},typeof window=="undefined"?(n=require("./helpers"),t=require("./timezonetime"),module.exports=e(n,t)):typeof define!="undefined"?define("olson/rule",["olson/helpers","olson/timezonetime"],e):(this.WallTime||(this.WallTime={}),this.WallTime.rule=e(this.WallTime.helpers,this.WallTime.TimeZoneTime))}.call(this),function(){var e,t,n,r;e=function(e,t,n){var r,i,s;return r=function(){function r(t,n,r,i,s,o){var u,a,f,l,c,h;this.name=t,this._offset=n,this._rule=r,this.format=i,this._until=s,h=e.Time.ParseGMTOffset(this._offset),a=h[0],f=h[1],l=h[2],c=h[3],this.offset={negative:a,hours:f,mins:l,secs:isNaN(c)?0:c},u=o?e.Time.MakeDateFromTimeStamp(o.range.end.getTime()+1):e.Time.MinDate(),this.range={begin:u,end:this._parseUntilDate(this._until)}}return r.prototype._parseUntilDate=function(t){var n,r,i,s,o,u,a,f,l,c,h,p,d;return p=t.split(" "),h=p[0],u=p[1],n=p[2],c=p[3],d=c?e.Time.ParseGMTOffset(c):[!1,0,0,0],a=d[0],i=d[1],s=d[2],f=d[3],f=isNaN(f)?0:f,!h||h===""?e.Time.MaxDate():(h=parseInt(h,10),o=u?e.Months.MonthIndex(u):0,n||(n="1"),e.Months.IsDayOfMonthRule(n)?n=e.Months.DayOfMonthByRule(n,h,o):e.Months.IsLastDayOfMonthRule(n)?n=e.Months.LastDayOfMonthRule(n,h,o):n=parseInt(n,10),l=e.Time.StandardTimeToUTC(this.offset,h,o,n,i,s,f),r=e.Time.MakeDateFromTimeStamp(l.getTime()-1),r)},r.prototype.updateEndForRules=function(n){var r,i,s,o,u;if(this._rule==="-"||this._rule==="")return;return this._rule.indexOf(":")>=0&&(u=e.Time.ParseTime(this._rule),i=u[0],s=u[1],this.range.end=e.Time.ApplySave(this.range.end,{hours:i,mins:s})),o=new t.RuleSet(n(this._rule),this),r=o.getYearEndDST(this.range.end),this.range.end=e.Time.ApplySave(this.range.end,r)},r.prototype.UTCToWallTime=function(r,i){var s,o,u,a;return this._rule==="-"||this._rule===""?new n(r,this,e.noSave):this._rule.indexOf(":")>=0?(a=e.Time.ParseTime(this._rule),s=a[0],o=a[1],new n(r,this,{hours:s,mins:o})):(u=new t.RuleSet(i(this._rule),this),u.getWallTimeForUTC(r))},r.prototype.WallTimeToUTC=function(n,r){var i,s,o,u;return this._rule==="-"||this._rule===""?e.Time.StandardTimeToUTC(this.offset,n):this._rule.indexOf(":")>=0?(u=e.Time.ParseTime(this._rule),i=u[0],s=u[1],e.Time.WallTimeToUTC(this.offset,{hours:i,mins:s},n)):(o=new t.RuleSet(r(this._rule),this),o.getUTCForWallTime(n,this.offset))},r.prototype.IsAmbiguous=function(n,r){var i,s,o,u,a,f,l,c,h;if(this._rule==="-"||this._rule==="")return!1;if(this._rule.indexOf(":")>=0){f=e.Time.StandardTimeToUTC(this.offset,n),l=e.Time.ParseTime(this._rule),s=l[0],u=l[1],o=function(t){var n,r;return n={begin:this.range.begin,end:e.Time.ApplySave(this.range.begin,{hours:s,mins:u})},n.end.getTime()0?this.name=this.zones[0].name:this.name="",s=this.zones;for(r=0,i=s.length;r"));return t=this.zones[e],this.zoneSet=new n.ZoneSet(t,function(e){return r.rules[e]}),this.timeZoneName=e},r.prototype.Date=function(t,n,r,i,s,o,u){return n==null&&(n=0),r==null&&(r=1),i==null&&(i=0),s==null&&(s=0),o==null&&(o=0),u==null&&(u=0),t||(t=(new Date).getUTCFullYear()),e.Time.MakeDateFromParts(t,n,r,i,s,o,u)},r.prototype.UTCToWallTime=function(e,t){t==null&&(t=this.timeZoneName),typeof e=="number"&&(e=new Date(e)),t!==this.timeZoneName&&this.setTimeZone(t);if(!this.zoneSet)throw new Error("Must set the time zone before converting times");return this.zoneSet.getWallTimeForUTC(e)},r.prototype.WallTimeToUTC=function(t,n,r,i,s,o,u,a){var f;return t==null&&(t=this.timeZoneName),r==null&&(r=0),i==null&&(i=1),s==null&&(s=0),o==null&&(o=0),u==null&&(u=0),a==null&&(a=0),t!==this.timeZoneName&&this.setTimeZone(t),f=typeof n=="number"?e.Time.MakeDateFromParts(n,r,i,s,o,u,a):n,this.zoneSet.getUTCForWallTime(f)},r.prototype.IsAmbiguous=function(t,n,r,i,s,o){var u;return t==null&&(t=this.timeZoneName),o==null&&(o=0),t!==this.timeZoneName&&this.setTimeZone(t),u=typeof n=="number"?e.Time.MakeDateFromParts(n,r,i,s,o):n,this.zoneSet.isAmbiguous(u)},r}(),new r};if(typeof window=="undefined")s=require("./olson/zone"),i=require("./olson/rule"),r=require("./olson/helpers"),module.exports=t(r,i,s);else if(typeof define!="undefined")require.specified("walltime-data")||(typeof console!="undefined"&&console!==null&&typeof console.warn=="function"&&console.warn("To use WallTime with requirejs please include the walltime-data.js script before requiring walltime"),define("walltime-data",[],function(){return null})),define("walltime",["olson/helpers","olson/rule","olson/zone","walltime-data"],function(e,n,r,i){var s;return s=t(e,n,r),(i!=null?i.zones:void 0)&&s.init(i.rules,i.zones),s});else{this.WallTime||(this.WallTime={}),e=t(this.WallTime.helpers,this.WallTime.rule,this.WallTime.zone),u=this.WallTime;for(n in u){if(!l.call(u,n))continue;o=u[n],e[n]=o}this.WallTime=e,this.WallTime.autoinit&&((a=this.WallTime.data)!=null?a.rules:void 0)&&((f=this.WallTime.data)!=null?f.zones:void 0)&&this.WallTime.init(this.WallTime.data.rules,this.WallTime.data.zones)}}.call(this); \ No newline at end of file + (function(){var e,t,n,r,i,s;(s=Array.prototype).indexOf||(s.indexOf=function(e){var t,n,r,i;for(t=r=0,i=this.length;r]?=)([0-9]*)"),MonthIndex:function(e){return this.MonthsShortNames.indexOf(e.slice(0,3))},IsDayOfMonthRule:function(e){return e.indexOf(">")>-1||e.indexOf("<")>-1||e.indexOf("=")>-1},IsLastDayOfMonthRule:function(e){return e.slice(0,4)==="last"},DayOfMonthByRule:function(e,t,n){var r,s,o,u,a,f,l,c,h;f=this.CompareRuleMatch.exec(e);if(!f)throw new Error("Unable to parse the 'on' rule for "+e);h=f.slice(1,4),a=h[0],c=h[1],o=h[2],o=parseInt(o,10);if(o===NaN)throw new Error("Unable to parse the dateIndex of the 'on' rule for "+e);u=i.Days.DayIndex(a),s={">=":function(e,t){return e>=t},"<=":function(e,t){return e<=t},">":function(e,t){return e>t},"<":function(e,t){return e12&&!e&&(t-=12),t===0&&(t=12),r=this.getMinutes(),r<10&&(r="0"+r),n=i>11?" PM":" AM",e&&(n=""),""+t+":"+r+n},t.prototype.setTime=function(t){return this.wallTime=e.Time.UTCToWallTime(new Date(t),this.zone.offset,this.save),this._updateUTC()},t.prototype.setFullYear=function(e){return this.wallTime.setUTCFullYear(e),this._updateUTC()},t.prototype.setMonth=function(e){return this.wallTime.setUTCMonth(e),this._updateUTC()},t.prototype.setDate=function(e){return this.wallTime.setUTCDate(e),this._updateUTC()},t.prototype.setHours=function(e){return this.wallTime.setUTCHours(e),this._updateUTC()},t.prototype.setMinutes=function(e){return this.wallTime.setUTCMinutes(e),this._updateUTC()},t.prototype.setSeconds=function(e){return this.wallTime.setUTCSeconds(e),this._updateUTC()},t.prototype.setMilliseconds=function(e){return this.wallTime.setUTCMilliseconds(e),this._updateUTC()},t.prototype._updateUTC=function(){return this.utc=e.Time.WallTimeToUTC(this.offset,this.save,this.getFullYear(),this.getMonth(),this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds(),this.getMilliseconds()),this.utc.getTime()},t}(),t},typeof window=="undefined"?(t=require("./helpers"),module.exports=e(t)):typeof define!="undefined"?define("olson/timezonetime",["olson/helpers"],e):(this.WallTime||(this.WallTime={}),this.WallTime.TimeZoneTime=e(this.WallTime.helpers))}.call(this),function(){var e,t,n,r={}.hasOwnProperty;e=function(e,t){var n,i,s,o,u,a;return s=function(){function e(){}return e.prototype.applies=function(e){return!isNaN(parseInt(e,10))},e.prototype.parseDate=function(e){return parseInt(e,10)},e}(),i=function(){function t(){}return t.prototype.applies=e.Months.IsLastDayOfMonthRule,t.prototype.parseDate=function(t,n,r,i,s,o){return e.Months.LastDayOfMonthRule(t,n,r)},t}(),n=function(){function t(){}return t.prototype.applies=e.Months.IsDayOfMonthRule,t.prototype.parseDate=function(t,n,r){return e.Months.DayOfMonthByRule(t,n,r)},t}(),o=function(){function t(t,n,r,i,s,o,u,a,f){var l,c,h,p;this.name=t,this._from=n,this._to=r,this.type=i,this["in"]=s,this.on=o,this.at=u,this._save=a,this.letter=f,this.from=parseInt(this._from,10),this.isMax=!1,h=this.from;switch(this._to){case"max":h=e.Time.MaxDate().getUTCFullYear(),this.isMax=!0;break;case"only":h=this.from;break;default:h=parseInt(this._to,10)}this.to=h,p=this._parseTime(this._save),l=p[0],c=p[1],this.save={hours:l,mins:c}}return t.prototype.forZone=function(t){return this.offset=t,this.fromUTC=e.Time.MakeDateFromParts(this.from,0,1,0,0,0),this.fromUTC=e.Time.ApplyOffset(this.fromUTC,t),this.toUTC=e.Time.MakeDateFromParts(this.to,11,31,23,59,59,999),this.toUTC=e.Time.ApplyOffset(this.toUTC,t)},t.prototype.setOnUTC=function(t,n,r){var i,s,o,u,a,f,l,c=this;return f=e.Months.MonthIndex(this["in"]),s=parseInt(this.on,10),o=isNaN(s)?this._parseOnDay(this.on,t,f):s,l=this._parseTime(this.at),u=l[0],a=l[1],i=l[2],this.onUTC=e.Time.MakeDateFromParts(t,f,o,u,a),this.onUTC.setUTCMilliseconds(this.onUTC.getUTCMilliseconds()-1),this.atQualifier=i!==""?i:"w",this.onUTC=e.Time.QualifiedTimeToUTC(this.onUTC,this.atQualifier,n,function(){return r(c)}),this.onSort=""+f+"-"+o+"-"+this.onUTC.getUTCHours()+"-"+this.onUTC.getUTCMinutes()},t.prototype.appliesToUTC=function(e){return this.fromUTC<=e&&e<=this.toUTC},t.prototype._parseOnDay=function(e,t,r){var o,u,a,f;u=[new s,new i,new n];for(a=0,f=u.length;au)u=f.to;o[f.to]=o[f.to]||[],o[f.to].push(f),i[f.from]=i[f.from]||[],i[f.from].push(f)}this.minYear=a,this.maxYear=u,s=function(n,i){var s,u,a,l;n==null&&(n="toUTC"),i==null&&(i=o),l=[];for(u in i){if(!r.call(i,u))continue;t=i[u],a=p.allThatAppliesTo(t[0][n]);if(a.length<1)continue;t=p._sortRulesByOnTime(t),s=a.slice(-1)[0];if(s.save.hours===0&&s.save.mins===0)continue;l.push(function(){var r,i,o;o=[];for(r=0,i=t.length;r0&&(s=r.slice(-1)[0].save),new t(n,this.timeZone,s)},n.prototype.getUTCForWallTime=function(t){var n,r,i,s,o,u,a,f;u=e.Time.StandardTimeToUTC(this.timeZone.offset,t),o=function(){var e,t,n,r;n=this.rules,r=[];for(e=0,t=n.length;e0&&(i=n.slice(-1)[0].save),e.Time.WallTimeToUTC(this.timeZone.offset,i,t)},n.prototype.getYearEndDST=function(t){var n,r,i,s,o,u,a,f,l;a=typeof t===number?t:t.getUTCFullYear(),u=e.Time.StandardTimeToUTC(this.timeZone.offset,a,11,31,23,59,59),o=function(){var e,t,n,r;n=this.rules,r=[];for(e=0,t=n.length;e0&&(i=n.slice(-1)[0].save),i},n.prototype.isAmbiguous=function(t){var n,r,i,s,o,u,a,f,l,c,h,p,d,v;p=e.Time.StandardTimeToUTC(this.timeZone.offset,t),l=function(){var e,t,n,r;n=this.rules,r=[];for(e=0,t=n.length;er.end.getTime()&&(i=r.begin,r.begin=r.end,r.end=i),r},o=c?h.last:-h.prev,a=s(i.onUTC,o),p=e.Time.WallTimeToUTC(this.timeZone.offset,u,t),a.begin<=p&&p<=a.end))},n.prototype._sortRulesByOnTime=function(t){return t.sort(function(t,n){return e.Months.MonthIndex(t["in"])-e.Months.MonthIndex(n["in"])})},n}(),a={Rule:o,RuleSet:u,OnFieldHandlers:{NumberHandler:s,LastHandler:i,CompareHandler:n}},a},typeof window=="undefined"?(n=require("./helpers"),t=require("./timezonetime"),module.exports=e(n,t)):typeof define!="undefined"?define("olson/rule",["olson/helpers","olson/timezonetime"],e):(this.WallTime||(this.WallTime={}),this.WallTime.rule=e(this.WallTime.helpers,this.WallTime.TimeZoneTime))}.call(this),function(){var e,t,n,r;e=function(e,t,n){var r,i,s;return r=function(){function r(t,n,r,i,s,o){var u,a,f,l,c,h;this.name=t,this._offset=n,this._rule=r,this.format=i,this._until=s,h=e.Time.ParseGMTOffset(this._offset),a=h[0],f=h[1],l=h[2],c=h[3],this.offset={negative:a,hours:f,mins:l,secs:isNaN(c)?0:c},u=o?e.Time.MakeDateFromTimeStamp(o.range.end.getTime()+1):e.Time.MinDate(),this.range={begin:u,end:this._parseUntilDate(this._until)}}return r.prototype._parseUntilDate=function(t){var n,r,i,s,o,u,a,f,l,c,h,p,d;return p=t.split(" "),h=p[0],u=p[1],n=p[2],c=p[3],d=c?e.Time.ParseGMTOffset(c):[!1,0,0,0],a=d[0],i=d[1],s=d[2],f=d[3],f=isNaN(f)?0:f,!h||h===""?e.Time.MaxDate():(h=parseInt(h,10),o=u?e.Months.MonthIndex(u):0,n||(n="1"),e.Months.IsDayOfMonthRule(n)?n=e.Months.DayOfMonthByRule(n,h,o):e.Months.IsLastDayOfMonthRule(n)?n=e.Months.LastDayOfMonthRule(n,h,o):n=parseInt(n,10),l=e.Time.StandardTimeToUTC(this.offset,h,o,n,i,s,f),r=e.Time.MakeDateFromTimeStamp(l.getTime()-1),r)},r.prototype.updateEndForRules=function(n){var r,i,s,o,u;if(this._rule==="-"||this._rule==="")return;return this._rule.indexOf(":")>=0&&(u=e.Time.ParseTime(this._rule),i=u[0],s=u[1],this.range.end=e.Time.ApplySave(this.range.end,{hours:i,mins:s})),o=new t.RuleSet(n(this._rule),this),r=o.getYearEndDST(this.range.end),this.range.end=e.Time.ApplySave(this.range.end,r)},r.prototype.UTCToWallTime=function(r,i){var s,o,u,a;return this._rule==="-"||this._rule===""?new n(r,this,e.noSave):this._rule.indexOf(":")>=0?(a=e.Time.ParseTime(this._rule),s=a[0],o=a[1],new n(r,this,{hours:s,mins:o})):(u=new t.RuleSet(i(this._rule),this),u.getWallTimeForUTC(r))},r.prototype.WallTimeToUTC=function(n,r){var i,s,o,u;return this._rule==="-"||this._rule===""?e.Time.StandardTimeToUTC(this.offset,n):this._rule.indexOf(":")>=0?(u=e.Time.ParseTime(this._rule),i=u[0],s=u[1],e.Time.WallTimeToUTC(this.offset,{hours:i,mins:s},n)):(o=new t.RuleSet(r(this._rule),this),o.getUTCForWallTime(n,this.offset))},r.prototype.IsAmbiguous=function(n,r){var i,s,o,u,a,f,l,c,h;if(this._rule==="-"||this._rule==="")return!1;if(this._rule.indexOf(":")>=0){f=e.Time.StandardTimeToUTC(this.offset,n),l=e.Time.ParseTime(this._rule),s=l[0],u=l[1],o=function(t){var n,r;return n={begin:this.range.begin,end:e.Time.ApplySave(this.range.begin,{hours:s,mins:u})},n.end.getTime()0?this.name=this.zones[0].name:this.name="",s=this.zones;for(r=0,i=s.length;r"));return t=this.zones[e],this.zoneSet=new n.ZoneSet(t,function(e){return r.rules[e]}),this.timeZoneName=e},r.prototype.Date=function(t,n,r,i,s,o,u){return n==null&&(n=0),r==null&&(r=1),i==null&&(i=0),s==null&&(s=0),o==null&&(o=0),u==null&&(u=0),t||(t=(new Date).getUTCFullYear()),e.Time.MakeDateFromParts(t,n,r,i,s,o,u)},r.prototype.UTCToWallTime=function(e,t){t==null&&(t=this.timeZoneName),typeof e=="number"&&(e=new Date(e)),t!==this.timeZoneName&&this.setTimeZone(t);if(!this.zoneSet)throw new Error("Must set the time zone before converting times");return this.zoneSet.getWallTimeForUTC(e)},r.prototype.WallTimeToUTC=function(t,n,r,i,s,o,u,a){var f;return t==null&&(t=this.timeZoneName),r==null&&(r=0),i==null&&(i=1),s==null&&(s=0),o==null&&(o=0),u==null&&(u=0),a==null&&(a=0),t!==this.timeZoneName&&this.setTimeZone(t),f=typeof n=="number"?e.Time.MakeDateFromParts(n,r,i,s,o,u,a):n,this.zoneSet.getUTCForWallTime(f)},r.prototype.IsAmbiguous=function(t,n,r,i,s,o){var u;return t==null&&(t=this.timeZoneName),o==null&&(o=0),t!==this.timeZoneName&&this.setTimeZone(t),u=typeof n=="number"?e.Time.MakeDateFromParts(n,r,i,s,o):n,this.zoneSet.isAmbiguous(u)},r}(),new r};if(typeof window=="undefined")s=require("./olson/zone"),i=require("./olson/rule"),r=require("./olson/helpers"),module.exports=t(r,i,s);else if(typeof define!="undefined")require.specified("walltime-data")||(typeof console!="undefined"&&console!==null&&typeof console.warn=="function"&&console.warn("To use WallTime with requirejs please include the walltime-data.js script before requiring walltime"),define("walltime-data",[],function(){return null})),define("walltime",["olson/helpers","olson/rule","olson/zone","walltime-data"],function(e,n,r,i){var s;return s=t(e,n,r),(i!=null?i.zones:void 0)&&s.init(i.rules,i.zones),s});else{this.WallTime||(this.WallTime={}),e=t(this.WallTime.helpers,this.WallTime.rule,this.WallTime.zone),u=this.WallTime;for(n in u){if(!l.call(u,n))continue;o=u[n],e[n]=o}this.WallTime=e,this.WallTime.autoinit&&((a=this.WallTime.data)!=null?a.rules:void 0)&&((f=this.WallTime.data)!=null?f.zones:void 0)&&this.WallTime.init(this.WallTime.data.rules,this.WallTime.data.zones)}}.call(this); \ No newline at end of file diff --git a/lib/olson/timezonetime.coffee b/lib/olson/timezonetime.coffee index 6fc1ac6..354fb51 100644 --- a/lib/olson/timezonetime.coffee +++ b/lib/olson/timezonetime.coffee @@ -1,7 +1,7 @@ init = (helpers) -> - + class TimeZoneTime constructor: (@utc, @zone, @save) -> @offset = @zone.offset @@ -16,7 +16,7 @@ init = (helpers) -> getMinutes: -> @wallTime.getUTCMinutes() getSeconds: -> @wallTime.getUTCSeconds() getMilliseconds: -> @wallTime.getUTCMilliseconds() - + # UTC Date overrides getUTCFullYear: -> @utc.getUTCFullYear() getUTCMonth: -> @utc.getUTCMonth() @@ -39,8 +39,12 @@ init = (helpers) -> base - dst + isDST: -> @save.hours isnt 0 or @save.mins isnt 0 + + getAbbreviation: -> @zone.format.replace('%s', if @isDST() then 'D' else 'S') + toISOString: -> @utc.toISOString() - + toUTCString: -> @wallTime.toUTCString() toDateString: -> @@ -59,7 +63,7 @@ init = (helpers) -> meridiem = if origHour > 11 then ' PM' else ' AM' meridiem = '' if use24HourTime - + "#{hour}:#{min}#{meridiem}" setTime: (ms) -> diff --git a/test/timezonetime_spec.coffee b/test/timezonetime_spec.coffee index 7e6353f..3b61643 100644 --- a/test/timezonetime_spec.coffee +++ b/test/timezonetime_spec.coffee @@ -19,7 +19,7 @@ describe "timezonetime", -> Feb11_2013_2333 = 1360647207000 Mar14_2013_800 = 1363266000000 - + readTimezoneFile = (file = "./test/rsrc/full/northamerica", next) -> reader = new OlsonReader @@ -31,7 +31,7 @@ describe "timezonetime", -> for own ruleName, ruleVal of result.rules rules[ruleName] ||= [] rules[ruleName] = rules[ruleName].concat ruleVal - + zones[zoneName] = zoneVal.zones for own zoneName, zoneVal of result.zones do next @@ -74,10 +74,24 @@ describe "timezonetime", -> date.getTimezoneOffset().should.equal expect, "dst" + it "gets DST correctly", -> + date = WallTime.UTCToWallTime Feb11_2013_2333 + date.isDST().should.equal false + + date = WallTime.UTCToWallTime Mar14_2013_800 + date.isDST().should.equal true + + it "gets abbreviation correctly", -> + date = WallTime.UTCToWallTime Feb11_2013_2333 + date.getAbbreviation().should.equal 'CST' + + date = WallTime.UTCToWallTime Mar14_2013_800 + date.getAbbreviation().should.equal 'CDT' + it "sets year correctly", -> date = WallTime.UTCToWallTime Feb12_2013_1433 date.getFullYear().should.equal 2013 - + date.setFullYear 2012 date.getFullYear().should.equal 2012 @@ -87,7 +101,7 @@ describe "timezonetime", -> it "sets month correctly", -> date = WallTime.UTCToWallTime Feb12_2013_1433 date.getMonth().should.equal 1 - + date.setMonth 0 date.getMonth().should.equal 0 @@ -96,7 +110,7 @@ describe "timezonetime", -> it "sets day correctly", -> date = WallTime.UTCToWallTime Feb12_2013_1433 date.getDate().should.equal 12 - + date.setDate 1 date.getFullYear().should.equal 2013 @@ -139,7 +153,7 @@ describe "timezonetime", -> date.getFullYear().should.equal 2013 date.setTime Feb12_2013_1135 - + date.getHours().should.equal 11 date.getMinutes().should.equal 35 date.getMonth().should.equal 1