1 line
4.7 KiB
JavaScript
1 line
4.7 KiB
JavaScript
|
|
import globalize from"./globalize.js";function parseISO8601Date(s){return new Date(Date.parse(s))}function getDisplayRunningTime(ticks){var parts=[],hours=ticks/36e9;(hours=Math.floor(hours))&&parts.push(hours);let minutes=(ticks-=36e9*hours)/6e8,seconds=(ticks-=6e8*(minutes=Math.floor(minutes)),minutes<10&&hours&&(minutes="0"+minutes),parts.push(minutes),ticks/1e7);return(seconds=Math.floor(seconds))<10&&(seconds="0"+seconds),parts.push(seconds),parts.join(":")}let toLocaleTimeStringSupportsLocales=(()=>{try{(new Date).toLocaleTimeString("i")}catch(e){return"RangeError"===e.name}return!1})();function getOptionList(options){var i,list=[];for(i in options)list.push({name:i,value:options[i]});return list}let dateFormatters={};function getDateTimeFormatter(locale,options){if(!Intl.DateTimeFormat)return null;var key=locale,key=(key=(key=(key=(key+=options.weekday||"null")+(options.year||"null")+(options.month||"null"))+(options.day||"null")+(options.hour||"null"))+(options.minute||"null")+(options.second||"null"))+(options.dateStyle||"null")+(options.timeStyle||"null");let formatter=dateFormatters[key];return formatter||(console.log("creating DateTimeFormat for "+locale+", with options: "+JSON.stringify(options)),formatter=new Intl.DateTimeFormat(locale,options),dateFormatters[key]=formatter),formatter}function toLocaleString(date,options){if(!date)throw new Error("date cannot be null");if(options=options||{dateStyle:"short",timeStyle:"short"},toLocaleTimeStringSupportsLocales){var formatter,currentLocale=globalize.getCurrentDateTimeLocale();if(currentLocale)return(formatter=getDateTimeFormatter(currentLocale,options))?formatter.format(date):date.toLocaleString(currentLocale,options)}return date.toLocaleString()}function toLocaleDateString(date,options){if(!date)throw new Error("date cannot be null");if(options=options||{},toLocaleTimeStringSupportsLocales){var currentLocale=globalize.getCurrentDateTimeLocale();if(currentLocale)return(formatter=getDateTimeFormatter(currentLocale,options))?formatter.format(date):date.toLocaleDateString(currentLocale,options)}var formatter=getOptionList(options);return 1===formatter.length&&"weekday"===formatter[0].name?(currentLocale=[],"long"===formatter[0].value?(currentLocale[0]="Sunday",currentLocale[1]="Monday",currentLocale[2]="Tuesday",currentLocale[3]="Wednesday",currentLocale[4]="Thursday",currentLocale[5]="Friday",currentLocale[6]="Saturday"):(currentLocale[0]="Sun",currentLocale[1]="Mon",currentLocale[2]="Tue",currentLocale[3]="Wed",currentLocale[4]="Thu",currentLocale[5]="Fri",currentLocale[6]="Sat"),currentLocale[date.getDay()]):date.toLocaleDateString()}function toLocaleTimeString(date,options){if(!date)throw new Error("date cannot be null");if(options=options||{},toLocaleTimeStringSupportsLocales){var formatter,currentLocale=globalize.getCurrentDateTimeLocale();if(currentLocale)return(formatter=getDateTimeFormatter(currentLocale,options))?formatter.format(date):date.toLocaleTimeString(currentLocale,options)}return date.toLocaleTimeString()}function getDisplayTime(date){if(!date)throw new Error("date cannot be null");if("string"===(typeof date).toString().toLowerCase())try{date=new Date(Date.parse(date))}catch(err){return date}if(toLocaleTimeStringSupportsLocales)return toLocaleTimeString(date,{hour:"numeric",minute:"2-digit"});let time=toLocaleTimeString(date);var timeLower=time.toLowerCase();if(timeLower.includes("am")||timeLower.includes("pm")){time=timeLower;let hour=date.getHours()%12;timeLower=11<date.getHours()?"pm":"am";hour=hour||12;let minutes=date.getMinutes();minutes=":"+(minutes=minutes<10?"0"+minutes:minutes),time=hour+minutes+timeLower}else{date=time.split(":");2<date.length&&(date.length=2,time=date.join(":"))}return time}function isRelativeDay(date,offsetInDays){var yesterday;if(date)return offsetInDays=(yesterday=new Date).getDate()+offsetInDays,yesterday.setDate(offsetInDays),date.getFullYear()===yesterday.getFullYear()&&date.getMonth()===yesterday.getMonth()&&date.getDate()===offsetInDays;throw new Error("date cannot be null")}function getHumanReadableRuntim
|