@brightspace-ui/intl
Advanced tools
Comparing version 3.0.1 to 3.0.2
@@ -17,2 +17,21 @@ import {getDocumentLocaleSettings, getLanguage, merge} from './common.js'; | ||
function formatDateString(date) { | ||
const timezone = getDocumentLocaleSettings().timezone.identifier; | ||
const options = { | ||
year: 'numeric', | ||
month: '2-digit', | ||
day: '2-digit', | ||
hour: '2-digit', | ||
minute: '2-digit', | ||
second: '2-digit', | ||
hour12: false, | ||
}; | ||
if (timezone) { | ||
options.timeZone = timezone; | ||
options.timeZoneName = 'short'; | ||
} | ||
const formatter = new Intl.DateTimeFormat('en-US', options); | ||
return formatter.format(date); | ||
} | ||
function getParts() { | ||
@@ -156,5 +175,52 @@ | ||
export function convertUTCToLocalDateTime(date) { | ||
if (!getDocumentLocaleSettings().timezone.identifier) { | ||
return date; | ||
} | ||
const utcDate = new Date(Date.UTC(date.year, date.month - 1, date.date, date.hours, date.minutes, date.seconds)); | ||
const formattedDateTime = formatDateString(utcDate); | ||
const re = /([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})( |, )([0-9]{1,2}):([0-9]{2}):([0-9]{2})/; | ||
const match = formattedDateTime.match(re); | ||
if (!match || match.length !== 8) { | ||
return date; | ||
} | ||
return { | ||
month: parseInt(match[1]), | ||
date: parseInt(match[2]), | ||
year: parseInt(match[3]), | ||
hours: parseInt(match[5]), | ||
minutes: parseInt(match[6]), | ||
seconds: parseInt(match[7]) | ||
}; | ||
} | ||
export function convertLocalToUTCDateTime(date) { | ||
if (!getDocumentLocaleSettings().timezone.identifier) { | ||
return date; | ||
} | ||
const dateDate = new Date(date.year, date.month - 1, date.date, date.hours, date.minutes, date.seconds); | ||
const timezone = formatDateString(dateDate).split(' ')[2]; | ||
const dateString = `${date.month}/${date.date}/${date.year}, ${date.hours}:${date.minutes}:${date.seconds} ${timezone}`; | ||
const parsedDateString = new Date(Date.parse(dateString)); | ||
// run again in case of DST (e.g., if timezone is CST for dateDate but local time is after CST is over, timezone is incorrect) | ||
const utcCorrectedTimezone = formatDateString(parsedDateString).split(' ')[2]; | ||
const dateStringInTimezone = `${date.month}/${date.date}/${date.year}, ${date.hours}:${date.minutes}:${date.seconds} ${utcCorrectedTimezone}`; | ||
const utcCorrectedDate = new Date(Date.parse(dateStringInTimezone)); | ||
return { | ||
month: utcCorrectedDate.getUTCMonth() + 1, | ||
date: utcCorrectedDate.getUTCDate(), | ||
year: utcCorrectedDate.getUTCFullYear(), | ||
hours: utcCorrectedDate.getUTCHours(), | ||
minutes: utcCorrectedDate.getUTCMinutes(), | ||
seconds: utcCorrectedDate.getUTCSeconds() | ||
}; | ||
} | ||
export function getDateTimeDescriptor() { | ||
const language = getLanguage(); | ||
const settings = getDocumentLocaleSettings(); | ||
@@ -168,2 +234,5 @@ const subtags = language.split('-'); | ||
} | ||
if (settings.overrides.date && settings.overrides.date.hour24 !== undefined) { | ||
hour24 = settings.overrides.date.hour24; | ||
} | ||
@@ -220,4 +289,2 @@ const timeFormat = getTimeFormat(hour24, language, baseLanguage); | ||
break; | ||
case 'en': | ||
break; | ||
case 'es': | ||
@@ -237,3 +304,3 @@ dateFormats = ['dddd d\' de \'MMMM\' de \'yyyy', 'd\' de \'MMMM\' de \'yyyy', 'd/M/yyyy', 'MMMM yyyy', 'd\' de \'MMMM']; | ||
case 'fr': | ||
dateFormats = ['dddd d MMMM yyyy', 'd MMM yyyy', 'dd/MM/yyyy', 'MMMM yyyy', 'd MMMM']; | ||
dateFormats = ['dddd\' le \'d MMMM yyyy', 'd MMM yyyy', 'dd/MM/yyyy', 'MMMM yyyy', 'd MMMM']; | ||
months = [ | ||
@@ -379,3 +446,2 @@ ['janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre'], | ||
const settings = getDocumentLocaleSettings(); | ||
if (settings.overrides.date) { | ||
@@ -382,0 +448,0 @@ merge(descriptor, settings.overrides.date); |
{ | ||
"name": "@brightspace-ui/intl", | ||
"version": "3.0.1", | ||
"version": "3.0.2", | ||
"description": "Internationalization APIs for number, date, time and file size formatting and parsing in D2L Brightspace.", | ||
@@ -5,0 +5,0 @@ "main": "lib/number.js", |
@@ -163,2 +163,40 @@ # intl | ||
## Date/Time Conversion based on user timezone | ||
**These are a work in progress and are not ready for usage yet** | ||
To convert an object containing a UTC date to an object containing a local date corresponding to the `data-timezone` attribute: | ||
```javascript | ||
import {convertUTCToLocalDateTime} from '@brightspace-ui/intl/lib/dateTime.js'; | ||
const UTCDateTime = { | ||
month: 12, | ||
date: 1, | ||
year: 2015, | ||
hours: 8, | ||
minutes: 0, | ||
seconds: 0 | ||
}; | ||
const localDateTime = convertUTCToLocalDateTime( | ||
UTCDateTime | ||
); // -> { month: 12, date: 1, year: 2015, hours: 3, minutes: 0, seconds: 0 } in America/Toronto | ||
``` | ||
To convert an object containing a local date corresponding to the `data-timezone` attribute to an object containing a UTC date: | ||
```javascript | ||
import {convertLocalToUTCDateTime} from '@brightspace-ui/intl/lib/dateTime.js'; | ||
const localDateTime = { | ||
month: 12, | ||
date: 1, | ||
year: 2015, | ||
hours: 8, | ||
minutes: 0, | ||
seconds: 0 | ||
}; | ||
const UTCDateTime = convertLocalToUTCDateTime( | ||
localDateTime | ||
); // -> { month: 12, date: 1, year: 2015, hours: 13, minutes: 0, seconds: 0 } in America/Toronto | ||
``` | ||
## File Size Formatting | ||
@@ -165,0 +203,0 @@ |
51984
1054
219