ilib
Version:
iLib is a cross-engine library of internationalization (i18n) classes written in pure JS
2 lines • 4.27 kB
JavaScript
// !data dateformats sysres
var ilib=require("./ilib.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;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))),void 0!==options.sync&&(sync=!!options.sync),loadParams=options.loadParams);var opts={};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 startRd,endRd,yearTemplate,monthTemplate,dayTemplate,formats,fmt="",thisZoneName=this.dateFmt.tz&&this.dateFmt.tz.getId()||"local",start=DateFactory._dateToIlib(startDateLike,thisZoneName,this.locale),end=DateFactory._dateToIlib(endDateLike,thisZoneName,this.locale);if("object"!=typeof start||!start.getCalendar||start.getCalendar()!==this.calName||"object"!=typeof end||!end.getCalendar||end.getCalendar()!==this.calName)throw"Wrong calendar type";return startRd=start.getRataDie(),fmt=(endRd=end.getRataDie())-startRd<3?start.year===end.year?start.month===end.month?start.day===end.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-startRd<730?start.year===end.year?start.month===end.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)):new IString(endRd-startRd<3650?this.dateFmt._getFormat(this.dateFmt.formats.range,"c20",this.length):this.dateFmt._getFormat(this.dateFmt.formats.range,"c30",this.length)),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"),fmt.format({sy:this.dateFmt._formatTemplate(start,yearTemplate),sm:this.dateFmt._formatTemplate(start,monthTemplate),sd:this.dateFmt._formatTemplate(start,dayTemplate),st:this.dateFmt._formatTemplate(start,this.timeTemplateArr),ey:this.dateFmt._formatTemplate(end,yearTemplate),em:this.dateFmt._formatTemplate(end,monthTemplate),ed:this.dateFmt._formatTemplate(end,dayTemplate),et:this.dateFmt._formatTemplate(end,this.timeTemplateArr)})}},module.exports=DateRngFmt;