UNPKG

ilib

Version:

iLib is a cross-engine library of internationalization (i18n) classes written in pure JS

2 lines 9.19 kB
// !data localeinfo zoneinfo var ilib=require("./ilib.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"),TimeZone=function(options){if(this.sync=!0,this.locale=new Locale(),this.isLocal=!1,options){if(options.locale&&(this.locale="string"==typeof options.locale?new Locale(options.locale):options.locale),options.id){var id=options.id.toString();if("local"===id){this.isLocal=!0;var now=new Date(),jan1=new Date(now.getFullYear(),0,1),jun1=new Date(now.getFullYear(),5,1);this.offsetJan1=-jan1.getTimezoneOffset(),this.offsetJun1=-jun1.getTimezoneOffset(),this.offset=Math.min(this.offsetJan1,this.offsetJun1)}this.id=id}else 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){if(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){var offsetParts=this._offsetStringToObj(this.zone.o);this.offset=(60*Math.abs(offsetParts.h||0)+(offsetParts.m||0))*MathUtils.signum(offsetParts.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){var files=hash[dir];ilib.isArray(files)&&files.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":if(this.zone.f&&"zzz"!==this.zone.f){if(-1===this.zone.f.indexOf("{c}"))return this.zone.f;var letter;return 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||""})}return temp="GMT"+this.zone.o,this.inDaylightTime(date)&&(temp+="+"+this.zone.s.v),temp;case"rfc822":var offset=this.getOffset(date),ret="UTC",hour=offset.h||0,minute=offset.m||0;return 0!==hour&&(ret+=0<hour?"+":"-",Math.abs(hour)<10&&(ret+="0"),ret+=hour<0?-hour:hour,minute<10&&(ret+="0"),ret+=minute),ret;case"long":if(this.zone.n){if(-1===this.zone.n.indexOf("{c}"))return this.zone.n;var str=this.inDaylightTime(date)?"Daylight":"Standard";return(temp=new IString(this.zone.n)).format({c:str||""})}return temp="GMT"+this.zone.o,this.inDaylightTime(date)&&(temp+="+"+this.zone.s.v),temp}},TimeZone.prototype._offsetStringToObj=function(str){var temp,offsetParts="string"==typeof str?str.split(":"):[],ret={h:0};return 0<offsetParts.length&&(ret.h=parseInt(offsetParts[0],10),1<offsetParts.length&&((temp=parseInt(offsetParts[1],10))&&(ret.m=temp),2<offsetParts.length&&(temp=parseInt(offsetParts[2],10))&&(ret.s=temp))),ret},TimeZone.prototype.getOffset=function(date){if(!date)return this.getRawOffset();var offset=this.getOffsetMillis(date)/6e4,hours=MathUtils.down(offset/60),minutes=Math.abs(offset)-60*Math.abs(hours),ret={h:hours};return 0!=minutes&&(ret.m=minutes),ret},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;return ret=this.offset,date&&this.inDaylightTime(date,!0)&&(ret+=this.dstSavings),6e4*ret},TimeZone.prototype.getOffsetStr=function(date){var ret,offset=this.getOffset(date);return ret=offset.h,void 0!==offset.m?(ret+=":"+offset.m,void 0!==offset.s&&(ret+=":"+offset.s)):ret+=":0",ret},TimeZone.prototype.getRawOffset=function(){var hours=MathUtils.down(this.offset/60),minutes=Math.abs(this.offset)-60*Math.abs(hours),ret={h:hours};return 0!=minutes&&(ret.m=minutes),ret},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(){if(this.isLocal){var savings=Math.abs(this.offsetJan1-this.offsetJun1),hours=MathUtils.down(savings/60);return{h:hours,m:savings-60*hours}}return this.zone&&this.zone.s?this._offsetStringToObj(this.zone.s.v):{h:0}},TimeZone.prototype.getDSTSavingsStr=function(){if(this.isLocal){var savings=this.getDSTSavings();return savings.h+":"+savings.m}return void 0!==this.offset&&this.zone&&this.zone.s?this.zone.s.v:"0:0"},TimeZone.prototype._calcRuleStart=function(rule,year){var day,refDay,cal,time,i,type="=",weekday=0,hour=0,minute=0,second=0,d=(refDay=void 0!==rule.j?new GregRataDie({julianday:rule.j}):(day="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==(i=rule.r.indexOf("<"))&&(i=rule.r.indexOf(">")),-1!=i?(type=rule.r.charAt(i),weekday=parseInt(rule.r.substring(0,i),10),parseInt(rule.r.substring(i+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:day,hour:hour,minute:minute,second:second}))).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,startRd,endRd,year;if(this.isLocal){var offset=6e4*this.offset;void 0===date.dst||date.dst||(offset+=6e4*this.dstSavings);var d=new Date(date?date.getTimeExtended()-offset:void 0),dst=Math.max(this.offsetJan1,this.offsetJun1);return-d.getTimezoneOffset()===dst}if(date&&date.cal&&"gregorian"===date.cal.type)rd=date.rd.getRataDie(),year=date.year;else{var time=date&&"function"==typeof date.getTimeExtended?date.getTimeExtended():void 0;rd=new GregRataDie({unixtime:time}).getRataDie(),year=new Date(time).getUTCFullYear()}if(!this.useDaylightTime(year))return!1;var startrule=this._getDSTStartRule(year),endrule=this._getDSTEndRule(year);return startRd=this._calcRuleStart(startrule,year),endRd=this._calcRuleStart(endrule,year),wallTime?startRd+=this.dstSavings/1440:(startRd-=this.offset/1440,endRd-=(this.offset+this.dstSavings)/1440),rd<endRd&&endRd-rd<=this.dstSavings/1440&&"boolean"==typeof date.dst?date.dst:startRd<endRd?startRd<=rd&&rd<endRd:startRd<=rd||rd<endRd},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;