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