UNPKG

luxon

Version:
437 lines (404 loc) 16.5 kB
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <base data-ice="baseUrl" href="../../../"> <title data-ice="title">Luxon</title> <link type="text/css" rel="stylesheet" href="css/style.css"> <link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css"> <script src="script/prettify/prettify.js"></script> <script src="script/manual.js"></script> <link data-ice="userStyle" rel="stylesheet" href="user/css/0-styles.css"> </head> <body class="layout-container" data-ice="rootContainer"> <header><span class="luxon-title">Luxon</span> <a href="./">Home</a> <a href="identifiers.html">Reference</a> <a href="source.html">Source</a> <a data-ice="repoURL" href="https://github.com/icambron/luxon" class="repo-url-github">Repository</a> <div class="search-box"> <span> <img src="./image/search.png"> <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span> </span> <ul class="search-result"></ul> </div> </header> <nav class="navigation" data-ice="nav"><div> <ul> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/datetime.js~DateTime.html">DateTime</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/duration.js~Duration.html">Duration</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/info.js~Info.html">Info</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/interval.js~Interval.html">Interval</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/settings.js~Settings.html">Settings</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-interface">I</span><span data-ice="name"><span><a href="class/src/zone.js~Zone.html">Zone</a></span></span></li> </ul> </div> </nav> <div class="content" data-ice="content"><h1 data-ice="title">src/impl/formatter.js</h1> <pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">import { Util } from &apos;./util&apos;; import { DateTime } from &apos;../datetime&apos;; import { English } from &apos;./english&apos;; function stringifyTokens(splits, tokenToString) { let s = &apos;&apos;; for (const token of splits) { if (token.literal) { s += token.val; } else { s += tokenToString(token.val); } } return s; } /** * @private */ export class Formatter { static create(locale, opts = {}) { const formatOpts = Object.assign({}, { round: true }, opts); return new Formatter(locale, formatOpts); } static parseFormat(fmt) { let current = null, currentFull = &apos;&apos;, bracketed = false; const splits = []; for (let i = 0; i &lt; fmt.length; i++) { const c = fmt.charAt(i); if (c === &quot;&apos;&quot;) { if (currentFull.length &gt; 0) { splits.push({ literal: bracketed, val: currentFull }); } current = null; currentFull = &apos;&apos;; bracketed = !bracketed; } else if (bracketed) { currentFull += c; } else if (c === current) { currentFull += c; } else { if (currentFull.length &gt; 0) { splits.push({ literal: false, val: currentFull }); } currentFull = c; current = c; } } if (currentFull.length &gt; 0) { splits.push({ literal: bracketed, val: currentFull }); } return splits; } constructor(locale, formatOpts) { this.opts = formatOpts; this.loc = locale; } formatDateTime(dt, opts = {}) { const [df, d] = this.loc.dtFormatter(dt, Object.assign({}, this.opts, opts)); return df.format(d); } formatDateTimeParts(dt, opts = {}) { const [df, d] = this.loc.dtFormatter(dt, Object.assign({}, this.opts, opts)); return df.format(d); } resolvedOptions(dt, opts = {}) { const [df, d] = this.loc.dtFormatter(dt, Object.assign({}, this.opts, opts)); return df.resolvedOptions(d); } num(n, p = 0) { const opts = Object.assign({}, this.opts); if (p &gt; 0) { opts.padTo = p; } return this.loc.numberFormatter(opts).format(n); } formatDateTimeFromString(dt, fmt) { const knownEnglish = this.loc.listingMode() === &apos;en&apos;; const string = (opts, extract) =&gt; this.loc.extract(dt, opts, extract), formatOffset = opts =&gt; { if (dt.isOffsetFixed &amp;&amp; dt.offset === 0 &amp;&amp; opts.allowZ) { return &apos;Z&apos;; } const hours = Util.towardZero(dt.offset / 60), minutes = Math.abs(dt.offset % 60), sign = hours &gt;= 0 ? &apos;+&apos; : &apos;-&apos;, base = `${sign}${Math.abs(hours)}`; switch (opts.format) { case &apos;short&apos;: return `${sign}${this.num(Math.abs(hours), 2)}:${this.num(minutes, 2)}`; case &apos;narrow&apos;: return minutes &gt; 0 ? `${base}:${minutes}` : base; case &apos;techie&apos;: return `${sign}${this.num(Math.abs(hours), 2)}${this.num(minutes, 2)}`; default: throw new RangeError(`Value format ${opts.format} is out of range for property format`); } }, meridiem = () =&gt; knownEnglish ? English.meridiemForDateTime(dt) : string({ hour: &apos;numeric&apos;, hour12: true }, &apos;dayperiod&apos;), month = (length, standalone) =&gt; knownEnglish ? English.monthForDateTime(dt, length) : string(standalone ? { month: length } : { month: length, day: &apos;numeric&apos; }, &apos;month&apos;), weekday = (length, standalone) =&gt; knownEnglish ? English.weekdayForDateTime(dt, length) : string( standalone ? { weekday: length } : { weekday: length, month: &apos;long&apos;, day: &apos;numeric&apos; }, &apos;weekday&apos; ), era = length =&gt; knownEnglish ? English.eraForDateTime(dt, length) : string({ era: length }, &apos;era&apos;), tokenToString = token =&gt; { const outputCal = this.loc.outputCalendar; // Where possible: http://cldr.unicode.org/translation/date-time#TOC-Stand-Alone-vs.-Format-Styles switch (token) { // ms case &apos;S&apos;: return this.num(dt.millisecond); case &apos;SSS&apos;: return this.num(dt.millisecond, 3); // seconds case &apos;s&apos;: return this.num(dt.second); case &apos;ss&apos;: return this.num(dt.second, 2); // minutes case &apos;m&apos;: return this.num(dt.minute); case &apos;mm&apos;: return this.num(dt.minute, 2); // hours case &apos;h&apos;: return this.num(dt.hour === 12 ? 12 : dt.hour % 12); case &apos;hh&apos;: return this.num(dt.hour === 12 ? 12 : dt.hour % 12, 2); case &apos;H&apos;: return this.num(dt.hour); case &apos;HH&apos;: return this.num(dt.hour, 2); // offset case &apos;Z&apos;: // like +6 return formatOffset({ format: &apos;narrow&apos;, allowZ: true }); case &apos;ZZ&apos;: // like +06:00 return formatOffset({ format: &apos;short&apos;, allowZ: true }); case &apos;ZZZ&apos;: // like +0600 return formatOffset({ format: &apos;techie&apos;, allowZ: false }); case &apos;ZZZZ&apos;: // like EST return dt.offsetNameShort; case &apos;ZZZZZ&apos;: // like Eastern Standard Time return dt.offsetNameLong; // zone case &apos;z&apos;: return dt.zoneName; // like America/New_York // meridiems case &apos;a&apos;: return meridiem(); // dates case &apos;d&apos;: return outputCal ? string({ day: &apos;numeric&apos; }, &apos;day&apos;) : this.num(dt.day); case &apos;dd&apos;: return outputCal ? string({ day: &apos;2-digit&apos; }, &apos;day&apos;) : this.num(dt.day, 2); // weekdays - standalone case &apos;c&apos;: // like 1 return this.num(dt.weekday); case &apos;ccc&apos;: // like &apos;Tues&apos; return weekday(&apos;short&apos;, true); case &apos;cccc&apos;: // like &apos;Tuesday&apos; return weekday(&apos;long&apos;, true); case &apos;ccccc&apos;: // like &apos;T&apos; return weekday(&apos;narrow&apos;, true); // weekdays - format case &apos;E&apos;: // like 1 return this.num(dt.weekday); case &apos;EEE&apos;: // like &apos;Tues&apos; return weekday(&apos;short&apos;, false); case &apos;EEEE&apos;: // like &apos;Tuesday&apos; return weekday(&apos;long&apos;, false); case &apos;EEEEE&apos;: // like &apos;T&apos; return weekday(&apos;narrow&apos;, false); // months - standalone case &apos;L&apos;: // like 1 return outputCal ? string({ month: &apos;numeric&apos;, day: &apos;numeric&apos; }, &apos;month&apos;) : this.num(dt.month); case &apos;LL&apos;: // like 01, doesn&apos;t seem to work return outputCal ? string({ month: &apos;2-digit&apos;, day: &apos;numeric&apos; }, &apos;month&apos;) : this.num(dt.month, 2); case &apos;LLL&apos;: // like Jan return month(&apos;short&apos;, true); case &apos;LLLL&apos;: // like January return month(&apos;long&apos;, true); case &apos;LLLLL&apos;: // like J return month(&apos;narrow&apos;, true); // months - format case &apos;M&apos;: // like 1 return outputCal ? string({ month: &apos;numeric&apos; }, &apos;month&apos;) : this.num(dt.month); case &apos;MM&apos;: // like 01 return outputCal ? string({ month: &apos;2-digit&apos; }, &apos;month&apos;) : this.num(dt.month, 2); case &apos;MMM&apos;: // like Jan return month(&apos;short&apos;, false); case &apos;MMMM&apos;: // like January return month(&apos;long&apos;, false); case &apos;MMMMM&apos;: // like J return month(&apos;narrow&apos;, false); // years case &apos;y&apos;: // like 2014 return outputCal ? string({ year: &apos;numeric&apos; }, &apos;year&apos;) : this.num(dt.year); case &apos;yy&apos;: // like 14 return outputCal ? string({ year: &apos;2-digit&apos; }, &apos;year&apos;) : this.num(dt.year.toString().slice(-2), 2); case &apos;yyyy&apos;: // like 0012 return outputCal ? string({ year: &apos;numeric&apos; }, &apos;year&apos;) : this.num(dt.year, 4); // eras case &apos;G&apos;: // like AD return era(&apos;short&apos;); case &apos;GG&apos;: // like Anno Domini return era(&apos;long&apos;); case &apos;GGGGG&apos;: return era(&apos;narrow&apos;); case &apos;kk&apos;: return this.num(dt.weekYear.toString().slice(-2), 2); case &apos;kkkk&apos;: return this.num(dt.weekYear, 4); case &apos;W&apos;: return this.num(dt.weekNumber); case &apos;WW&apos;: return this.num(dt.weekNumber, 2); case &apos;o&apos;: return this.num(dt.ordinal); case &apos;ooo&apos;: return this.num(dt.ordinal, 3); // macros case &apos;D&apos;: return this.formatDateTime(dt, DateTime.DATE_SHORT); case &apos;DD&apos;: return this.formatDateTime(dt, DateTime.DATE_MED); case &apos;DDD&apos;: return this.formatDateTime(dt, DateTime.DATE_FULL); case &apos;DDDD&apos;: return this.formatDateTime(dt, DateTime.DATE_HUGE); case &apos;t&apos;: return this.formatDateTime(dt, DateTime.TIME_SIMPLE); case &apos;tt&apos;: return this.formatDateTime(dt, DateTime.TIME_WITH_SECONDS); case &apos;ttt&apos;: return this.formatDateTime(dt, DateTime.TIME_WITH_SHORT_OFFSET); case &apos;tttt&apos;: return this.formatDateTime(dt, DateTime.TIME_WITH_LONG_OFFSET); case &apos;T&apos;: return this.formatDateTime(dt, DateTime.TIME_24_SIMPLE); case &apos;TT&apos;: return this.formatDateTime(dt, DateTime.TIME_24_WITH_SECONDS); case &apos;TTT&apos;: return this.formatDateTime(dt, DateTime.TIME_24_WITH_SHORT_OFFSET); case &apos;TTTT&apos;: return this.formatDateTime(dt, DateTime.TIME_24_WITH_LONG_OFFSET); case &apos;f&apos;: return this.formatDateTime(dt, DateTime.DATETIME_SHORT); case &apos;ff&apos;: return this.formatDateTime(dt, DateTime.DATETIME_MED); case &apos;fff&apos;: return this.formatDateTime(dt, DateTime.DATETIME_FULL); case &apos;ffff&apos;: return this.formatDateTime(dt, DateTime.DATETIME_HUGE); case &apos;F&apos;: return this.formatDateTime(dt, DateTime.DATETIME_SHORT_WITH_SECONDS); case &apos;FF&apos;: return this.formatDateTime(dt, DateTime.DATETIME_MED_WITH_SECONDS); case &apos;FFF&apos;: return this.formatDateTime(dt, DateTime.DATETIME_FULL_WITH_SECONDS); case &apos;FFFF&apos;: return this.formatDateTime(dt, DateTime.DATETIME_HUGE_WITH_SECONDS); default: return token; } }; return stringifyTokens(Formatter.parseFormat(fmt), tokenToString); } formatDuration() {} formatDurationFromString(dur, fmt) { const tokenToField = token =&gt; { switch (token[0]) { case &apos;S&apos;: return &apos;millisecond&apos;; case &apos;s&apos;: return &apos;second&apos;; case &apos;m&apos;: return &apos;minute&apos;; case &apos;h&apos;: return &apos;hour&apos;; case &apos;d&apos;: return &apos;day&apos;; case &apos;M&apos;: return &apos;month&apos;; case &apos;y&apos;: return &apos;year&apos;; default: return null; } }, tokenToString = lildur =&gt; token =&gt; { const mapped = tokenToField(token); if (mapped) { return this.num(lildur.get(mapped), token.length); } else { return token; } }, tokens = Formatter.parseFormat(fmt), realTokens = tokens.reduce( (found, { literal, val }) =&gt; (literal ? found : found.concat(val)), [] ), collapsed = dur.shiftTo(...realTokens.map(tokenToField).filter(t =&gt; t)); return stringifyTokens(tokens, tokenToString(collapsed)); } } </code></pre> </div> <footer class="footer"> Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(0.5.2)</span><img src="./image/esdoc-logo-mini-black.png"></a> </footer> <script src="script/search_index.js"></script> <script src="script/search.js"></script> <script src="script/pretty-print.js"></script> <script src="script/inherited-summary.js"></script> <script src="script/test-summary.js"></script> <script src="script/inner-link.js"></script> <script src="script/patch-for-local.js"></script> </body> </html>