1+ /* @flow strict */
2+
13import { strftime , makeFormatter , isDayFirst } from './utils'
24import ExtendedTimeElement from './extended-time-element'
35
46const formatters = new WeakMap ( )
57
68export default class LocalTimeElement extends ExtendedTimeElement {
7- attributeChangedCallback ( attrName , oldValue , newValue ) {
9+ attributeChangedCallback ( attrName : string , oldValue : string , newValue : string ) {
810 if ( attrName === 'hour' || attrName === 'minute' || attrName === 'second' || attrName === 'time-zone-name' ) {
911 formatters . delete ( this )
1012 }
@@ -27,13 +29,12 @@ export default class LocalTimeElement extends ExtendedTimeElement {
2729 // second - "numeric", "2-digit"
2830 //
2931 // Returns a formatted time String.
30- getFormattedDate ( ) {
31- if ( ! this . _date ) {
32- return
33- }
32+ getFormattedDate ( ) : ?string {
33+ const d = this . _date
34+ if ( ! d ) return
3435
35- const date = formatDate ( this ) || ''
36- const time = formatTime ( this ) || ''
36+ const date = formatDate ( this , d ) || ''
37+ const time = formatTime ( this , d ) || ''
3738 return `${ date } ${ time } ` . trim ( )
3839 }
3940}
@@ -48,7 +49,7 @@ export default class LocalTimeElement extends ExtendedTimeElement {
4849// el - The local-time element to format.
4950//
5051// Returns a date String or null if no date formats are provided.
51- function formatDate ( el ) {
52+ function formatDate ( el : Element , date : Date ) {
5253 // map attribute values to strftime
5354 const props = {
5455 weekday : {
@@ -80,7 +81,7 @@ function formatDate(el) {
8081 format = format . replace ( / ( \s , ) | ( , \s $ ) / , '' )
8182
8283 // squeeze spaces from final string
83- return strftime ( el . _date , format )
84+ return strftime ( date , format )
8485 . replace ( / \s + / , ' ' )
8586 . trim ( )
8687}
@@ -91,21 +92,18 @@ function formatDate(el) {
9192// el - The local-time element to format.
9293//
9394// Returns a time String or null if no time formats are provided.
94- function formatTime ( el ) {
95- // retrieve format settings from attributes
96- const options = {
97- hour : el . getAttribute ( 'hour' ) ,
98- minute : el . getAttribute ( 'minute' ) ,
99- second : el . getAttribute ( 'second' ) ,
100- timeZoneName : el . getAttribute ( 'time-zone-name' )
101- }
95+ function formatTime ( el : Element , date : Date ) {
96+ const options : Intl$DateTimeFormatOptions = { }
10297
103- // Remove unset format attributes.
104- for ( const opt in options ) {
105- if ( ! options [ opt ] ) {
106- delete options [ opt ]
107- }
108- }
98+ // retrieve format settings from attributes
99+ const hour = el . getAttribute ( 'hour' )
100+ if ( hour === 'numeric' || hour === '2-digit' ) options . hour = hour
101+ const minute = el . getAttribute ( 'minute' )
102+ if ( minute === 'numeric' || minute === '2-digit' ) options . minute = minute
103+ const second = el . getAttribute ( 'second' )
104+ if ( second === 'numeric' || second === '2-digit' ) options . second = second
105+ const tz = el . getAttribute ( 'time-zone-name' )
106+ if ( tz === 'short' || tz === 'long' ) options . timeZoneName = tz
109107
110108 // No time format attributes provided.
111109 if ( Object . keys ( options ) . length === 0 ) {
@@ -121,11 +119,11 @@ function formatTime(el) {
121119 const formatter = factory ( )
122120 if ( formatter ) {
123121 // locale-aware formatting of 24 or 12 hour times
124- return formatter . format ( el . _date )
122+ return formatter . format ( date )
125123 } else {
126124 // fall back to strftime for non-Intl browsers
127125 const timef = options . second ? '%H:%M:%S' : '%H:%M'
128- return strftime ( el . _date , timef )
126+ return strftime ( date , timef )
129127 }
130128}
131129
0 commit comments