UNPKG

ilib

Version:

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

2 lines 5.39 kB
// !data dateformats sysres var ilib=require("../index.js"),JSUtils=require("./JSUtils.js"),Locale=require("./Locale.js"),CalendarFactory=require("./CalendarFactory.js"),DateFmt=require("./DateFmt.js"),IString=require("./IString.js"),GregorianCal=require("./GregorianCal.js"),DateFactory=require("./DateFactory.js"),DateRngFmt=function(options){var sync=!0,loadParams=void 0,opts=(this.locale=new Locale(),this.length="s",options&&(options.locale&&(this.locale="string"==typeof options.locale?new Locale(options.locale):options.locale),options.calendar&&(this.calName=options.calendar),!options.length||"short"!==options.length&&"medium"!==options.length&&"long"!==options.length&&"full"!==options.length||(this.length=options.length.charAt(0)),options.timezone&&(this.timezone=options.timezone),void 0!==options.sync&&(sync=!!options.sync),loadParams=options.loadParams),{});JSUtils.shallowCopy(options,opts),opts.sync=sync,opts.loadParams=loadParams,opts.onLoad=ilib.bind(this,function(fmt){if(this.dateFmt=fmt)this.locinfo=this.dateFmt.locinfo,this.calName=this.calName||this.locinfo.getCalendar()||"gregorian",CalendarFactory({type:this.calName,sync:sync,loadParams:loadParams,onLoad:ilib.bind(this,function(cal){this.cal=cal,this.cal||(this.cal=new GregorianCal),this.timeTemplate=this.dateFmt._getFormat(this.dateFmt.formats.time[this.dateFmt.clock],this.dateFmt.timeComponents,this.length)||"hh:mm",this.timeTemplateArr=this.dateFmt._tokenize(this.timeTemplate),options&&"function"==typeof options.onLoad&&options.onLoad(this)})});else{if(!options||"boolean"!=typeof options.sync||options.sync||"function"!=typeof options.onLoad)throw"No formats available for calendar "+this.calName+" in locale "+this.locale.getSpec();options.onLoad(void 0)}}),new DateFmt(opts)};DateRngFmt.prototype={getLocale:function(){return this.locale},getCalendar:function(){return this.dateFmt.getCalendar()},getLength:function(){return DateFmt.lenmap[this.length]||""},getTimeZone:function(){return this.dateFmt.getTimeZone()},getClock:function(){return this.dateFmt.getClock()},format:function(startDateLike,endDateLike){var yearTemplate,monthTemplate,dayTemplate,formats,endRd,tmp,fmt="",thisZoneName=this.dateFmt.tz&&this.dateFmt.tz.getId()||"local",startDateLike=DateFactory._dateToIlib(startDateLike,thisZoneName,this.locale),endDateLike=DateFactory._dateToIlib(endDateLike,thisZoneName,this.locale);return("object"!=typeof startDateLike||!startDateLike.getCalendar||startDateLike.getCalendar()!==this.calName||this.timezone&&startDateLike.timezone&&startDateLike.timezone!==this.timezone)&&(startDateLike=DateFactory({type:this.calName,timezone:thisZoneName,julianday:startDateLike.getJulianDay()})),("object"!=typeof endDateLike||!endDateLike.getCalendar||endDateLike.getCalendar()!==this.calName||this.timezone&&endDateLike.timezone&&endDateLike.timezone!==this.timezone)&&(endDateLike=DateFactory({type:this.calName,timezone:thisZoneName,julianday:endDateLike.getJulianDay()})),thisZoneName=startDateLike.getRataDie(),endRd=endDateLike.getRataDie(),formats=this.dateFmt.formats.date,yearTemplate=this.dateFmt._tokenize(this.dateFmt._getFormatInternal(formats,"y",this.length)||"yyyy"),monthTemplate=this.dateFmt._tokenize(this.dateFmt._getFormatInternal(formats,"m",this.length)||"MM"),dayTemplate=this.dateFmt._tokenize(this.dateFmt._getFormatInternal(formats,"d",this.length)||"dd"),endRd-thisZoneName<3?fmt=startDateLike.year===endDateLike.year?startDateLike.month===endDateLike.month?startDateLike.day===endDateLike.day?new IString(this.dateFmt._getFormat(this.dateFmt.formats.range,"c00",this.length)):new IString(this.dateFmt._getFormat(this.dateFmt.formats.range,"c01",this.length)):new IString(this.dateFmt._getFormat(this.dateFmt.formats.range,"c02",this.length)):new IString(this.dateFmt._getFormat(this.dateFmt.formats.range,"c03",this.length)):endRd-thisZoneName<730?fmt=startDateLike.year===endDateLike.year?startDateLike.month===endDateLike.month?new IString(this.dateFmt._getFormat(this.dateFmt.formats.range,"c10",this.length)):new IString(this.dateFmt._getFormat(this.dateFmt.formats.range,"c11",this.length)):new IString(this.dateFmt._getFormat(this.dateFmt.formats.range,"c12",this.length)):endRd-thisZoneName<3650?(fmt=new IString(this.dateFmt._getFormat(this.dateFmt.formats.range,"c20",this.length)),(tmp=this.dateFmt._getFormatInternal(formats,"mys",this.length))&&(-1<tmp.indexOf("r")&&(endRd=this.dateFmt._getFormatInternal(formats,"r",this.length))&&(yearTemplate=this.dateFmt._tokenize(endRd)),-1<tmp.indexOf("L"))&&(thisZoneName=this.dateFmt._getFormatInternal(formats,"l",this.length))&&(monthTemplate=this.dateFmt._tokenize(thisZoneName))):(fmt=new IString(this.dateFmt._getFormat(this.dateFmt.formats.range,"c30",this.length)),(tmp=this.dateFmt._getFormatInternal(formats,"r",this.length))&&(yearTemplate=this.dateFmt._tokenize(tmp))),fmt.format({sy:this.dateFmt._formatTemplate(startDateLike,yearTemplate),sm:this.dateFmt._formatTemplate(startDateLike,monthTemplate),sd:this.dateFmt._formatTemplate(startDateLike,dayTemplate),st:this.dateFmt._formatTemplate(startDateLike,this.timeTemplateArr),ey:this.dateFmt._formatTemplate(endDateLike,yearTemplate),em:this.dateFmt._formatTemplate(endDateLike,monthTemplate),ed:this.dateFmt._formatTemplate(endDateLike,dayTemplate),et:this.dateFmt._formatTemplate(endDateLike,this.timeTemplateArr)})}},module.exports=DateRngFmt;