ilib
Version:
iLib is a cross-engine library of internationalization (i18n) classes written in pure JS
1 lines • 8.23 kB
JavaScript
var ilib=require("./ilib.js");var Utils=require("./Utils.js");var MathUtils=require("./MathUtils.js");var JSUtils=require("./JSUtils.js");var Locale=require("./Locale.js");var LocaleInfo=require("./LocaleInfo.js");var GregRataDie=require("./GregRataDie.js");var CalendarFactory=require("./CalendarFactory.js");var IString=require("./IString.js");var TimeZone=function(e){this.sync=true;this.locale=new Locale;this.isLocal=false;if(e){if(e.locale){this.locale=typeof e.locale==="string"?new Locale(e.locale):e.locale}if(e.id){var t=e.id.toString();if(t==="local"){this.isLocal=true;var i=new Date,n=new Date(i.getFullYear(),0,1),s=new Date(i.getFullYear(),5,1);this.offsetJan1=-n.getTimezoneOffset();this.offsetJun1=-s.getTimezoneOffset();this.offset=Math.min(this.offsetJan1,this.offsetJun1)}this.id=t}else if(e.offset){this.offset=typeof e.offset==="string"?parseInt(e.offset,10):e.offset;this.id=this.getDisplayName(undefined,undefined)}if(typeof e.sync!=="undefined"){this.sync=!!e.sync}this.loadParams=e.loadParams;this.onLoad=e.onLoad}if(!this.id){new LocaleInfo(this.locale,{sync:this.sync,onLoad:ilib.bind(this,function(e){this.id=e.getTimeZone()||"Etc/UTC";this._loadtzdata()})})}else{this._loadtzdata()}};TimeZone.prototype._loadtzdata=function(){if(!ilib.data.zoneinfo[this.id]&&typeof this.offset==="undefined"){Utils.loadData({object:TimeZone,nonlocale:true,name:"zoneinfo/"+this.id+".json",sync:this.sync,loadParams:this.loadParams,callback:ilib.bind(this,function(e){if(e&&!JSUtils.isEmpty(e)){ilib.data.zoneinfo[this.id]=e}this._initZone()})})}else{this._initZone()}};TimeZone.prototype._initZone=function(){this.zone=ilib.data.zoneinfo[this.id];if(!this.zone&&typeof this.offset==="undefined"){this.id="Etc/UTC";this.zone=ilib.data.zoneinfo[this.id]}this._calcDSTSavings();if(typeof this.offset==="undefined"&&this.zone.o){var e=this._offsetStringToObj(this.zone.o);this.offset=(Math.abs(e.h||0)*60+(e.m||0))*MathUtils.signum(e.h||0)}if(this.onLoad&&typeof this.onLoad==="function"){this.onLoad(this)}};TimeZone._marshallIds=function(e,t,i){var n,s=[];if(!e){s.push("local");for(n in ilib.data.timezones){if(ilib.data.timezones[n]){s.push(ilib.data.timezones[n])}}if(typeof i==="function"){i(s)}}else{if(!ilib.data.zoneinfo.zonetab){Utils.loadData({object:TimeZone,nonlocale:true,name:"zoneinfo/zonetab.json",sync:t,callback:ilib.bind(this,function(t){if(t){ilib.data.zoneinfo.zonetab=t}s=ilib.data.zoneinfo.zonetab[e];if(typeof i==="function"){i(s)}})})}else{s=ilib.data.zoneinfo.zonetab[e];if(typeof i==="function"){i(s)}}}return s};TimeZone.getAvailableIds=function(e,t,i){var n,s=[];if(typeof t!=="boolean"){t=true}if(ilib.data.timezones.length===0){if(typeof ilib._load!=="undefined"&&typeof ilib._load.listAvailableFiles==="function"){ilib._load.listAvailableFiles(t,function(n){for(var o in n){var a=n[o];if(ilib.isArray(a)){a.forEach(function(e){if(e&&e.match(/^zoneinfo/)){ilib.data.timezones.push(e.replace(/^zoneinfo\//,"").replace(/\.json$/,""))}})}}s=TimeZone._marshallIds(e,t,i)})}else{for(n in ilib.data.zoneinfo){if(ilib.data.zoneinfo[n]){ilib.data.timezones.push(n)}}s=TimeZone._marshallIds(e,t,i)}}else{s=TimeZone._marshallIds(e,t,i)}return s};TimeZone.prototype.getId=function(){return this.id.toString()};TimeZone.prototype.getDisplayName=function(e,t){t=this.isLocal||typeof this.zone==="undefined"?"rfc822":t||"standard";switch(t){default:case"standard":if(this.zone.f&&this.zone.f!=="zzz"){if(this.zone.f.indexOf("{c}")!==-1){var i="";i=this.inDaylightTime(e)?this.zone.s&&this.zone.s.c:this.zone.e&&this.zone.e.c;var n=new IString(this.zone.f);return n.format({c:i||""})}return this.zone.f}var n="GMT"+this.zone.o;if(this.inDaylightTime(e)){n+="+"+this.zone.s.v}return n;break;case"rfc822":var s=this.getOffset(e),o="UTC",a=s.h||0,f=s.m||0;if(a!==0){o+=a>0?"+":"-";if(Math.abs(a)<10){o+="0"}o+=a<0?-a:a;if(f<10){o+="0"}o+=f}return o;case"long":if(this.zone.n){if(this.zone.n.indexOf("{c}")!==-1){var r=this.inDaylightTime(e)?"Daylight":"Standard";var n=new IString(this.zone.n);return n.format({c:r||""})}return this.zone.n}var n="GMT"+this.zone.o;if(this.inDaylightTime(e)){n+="+"+this.zone.s.v}return n;break}};TimeZone.prototype._offsetStringToObj=function(e){var t=typeof e==="string"?e.split(":"):[],i={h:0},n;if(t.length>0){i.h=parseInt(t[0],10);if(t.length>1){n=parseInt(t[1],10);if(n){i.m=n}if(t.length>2){n=parseInt(t[2],10);if(n){i.s=n}}}}return i};TimeZone.prototype.getOffset=function(e){if(!e){return this.getRawOffset()}var t=this.getOffsetMillis(e)/6e4;var i=MathUtils.down(t/60),n=Math.abs(t)-Math.abs(i)*60;var s={h:i};if(n!=0){s.m=n}return s};TimeZone.prototype.getOffsetMillis=function(e){var t;if(this.isLocal&&typeof e.dst==="undefined"){var i=!e?new Date:new Date(e.getTimeExtended());return-i.getTimezoneOffset()*6e4}t=this.offset;if(e&&this.inDaylightTime(e)){t+=this.dstSavings}return t*6e4};TimeZone.prototype._getOffsetMillisWallTime=function(e){var t;t=this.offset;if(e&&this.inDaylightTime(e,true)){t+=this.dstSavings}return t*6e4};TimeZone.prototype.getOffsetStr=function(e){var t=this.getOffset(e),i;i=t.h;if(typeof t.m!=="undefined"){i+=":"+t.m;if(typeof t.s!=="undefined"){i+=":"+t.s}}else{i+=":0"}return i};TimeZone.prototype.getRawOffset=function(){var e=MathUtils.down(this.offset/60),t=Math.abs(this.offset)-Math.abs(e)*60;var i={h:e};if(t!=0){i.m=t}return i};TimeZone.prototype.getRawOffsetMillis=function(){return this.offset*6e4};TimeZone.prototype.getRawOffsetStr=function(){var e=this.getRawOffset();return e.h+":"+(e.m||"0")};TimeZone.prototype.getDSTSavings=function(){if(this.isLocal){var e=Math.abs(this.offsetJan1-this.offsetJun1);var t=MathUtils.down(e/60),i=e-t*60;return{h:t,m:i}}else if(this.zone&&this.zone.s){return this._offsetStringToObj(this.zone.s.v)}return{h:0}};TimeZone.prototype.getDSTSavingsStr=function(){if(this.isLocal){var e=this.getDSTSavings();return e.h+":"+e.m}else if(typeof this.offset!=="undefined"&&this.zone&&this.zone.s){return this.zone.s.v}return"0:0"};TimeZone.prototype._calcRuleStart=function(e,t){var i="=",n=0,s,o,a,f=0,r=0,h=0,l,d;if(typeof e.j!=="undefined"){o=new GregRataDie({julianday:e.j})}else{if(e.r.charAt(0)=="l"||e.r.charAt(0)=="f"){a=CalendarFactory({type:"gregorian"});i=e.r.charAt(0);n=parseInt(e.r.substring(1),10);s=i==="l"?a.getMonLength(e.m,t):1}else{d=e.r.indexOf("<");if(d==-1){d=e.r.indexOf(">")}if(d!=-1){i=e.r.charAt(d);n=parseInt(e.r.substring(0,d),10);s=parseInt(e.r.substring(d+1),10)}else{s=parseInt(e.r,10)}}if(e.t){l=e.t.split(":");f=parseInt(l[0],10);if(l.length>1){r=parseInt(l[1],10);if(l.length>2){h=parseInt(l[2],10)}}}o=new GregRataDie({year:t,month:e.m,day:s,hour:f,minute:r,second:h})}var u=o.getRataDie();switch(i){case"l":case"<":u=o.onOrBefore(n);break;case"f":case">":u=o.onOrAfter(n);break}return u};TimeZone.prototype._calcDSTSavings=function(){var e=this.getDSTSavings();this.dstSavings=(Math.abs(e.h||0)*60+(e.m||0))*MathUtils.signum(e.h||0)};TimeZone.prototype._getDSTStartRule=function(e){return this.zone.s};TimeZone.prototype._getDSTEndRule=function(e){return this.zone.e};TimeZone.prototype.inDaylightTime=function(e,t){var i,n,s,o;if(this.isLocal){var a=0;if(typeof e.dst!=="undefined"&&!e.dst){a=this.dstSavings*6e4}var f=new Date(e?e.getTimeExtended()+a:undefined);var r=Math.max(this.offsetJan1,this.offsetJun1);return-f.getTimezoneOffset()===r}if(!e||!e.cal||e.cal.type!=="gregorian"){var h=e&&typeof e.getTimeExtended==="function"?e.getTimeExtended():undefined;i=new GregRataDie({unixtime:h}).getRataDie();o=new Date(h).getUTCFullYear()}else{i=e.rd.getRataDie();o=e.year}if(!this.useDaylightTime(o)){return false}var l=this._getDSTStartRule(o);var d=this._getDSTEndRule(o);n=this._calcRuleStart(l,o);s=this._calcRuleStart(d,o);if(t){n+=this.dstSavings/1440}else{n-=this.offset/1440;s-=(this.offset+this.dstSavings)/1440}if(i<s&&s-i<=this.dstSavings/1440&&typeof e.dst==="boolean"){return e.dst}if(n<s){return i>=n&&i<s?true:false}return i>=n||i<s?true:false};TimeZone.prototype.useDaylightTime=function(e){return this.isLocal&&this.offsetJan1!==this.offsetJun1||typeof this.zone!=="undefined"&&typeof this.zone.s!=="undefined"&&typeof this.zone.e!=="undefined"};TimeZone.prototype.getCountry=function(){return this.zone.c};module.exports=TimeZone;