Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #186 from 5app/185-time-component-localisation
Browse files Browse the repository at this point in the history
feat: localisation time component, #185
  • Loading branch information
SarahHouben authored Mar 3, 2022
2 parents 032000c + 65da95e commit 69805a3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
13 changes: 13 additions & 0 deletions src/Time/README.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ The component auto-updates to keep the time accurate without requiring a page re
<Time dateTime="1981-05-12" />
</Canvas>

### DE locale
<Canvas>
<Time dateTime="2019-08-24T13:30:00Z" systemTime="2019-08-29T12:00:00Z" locale="de-DE" />
</Canvas>

<Canvas>
<Time dateTime="2019-01-24T13:30:00Z" systemTime="2019-08-29T12:00:00Z" locale="de-DE"/>
</Canvas>

<Canvas>
<Time dateTime="1981-05-12" locale="de-DE" />
</Canvas>

## Invalid examples

<Canvas>
Expand Down
16 changes: 12 additions & 4 deletions src/Time/getDateString.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@ function date(d) {

function getDateString({
dateTime,
locale,
locale = navigator.language,
systemOffset = 0,
readoutFunctions = {},
}) {
// Default hourCycle value
let hourCycle = 'h24';
const hourCycle12Languages = ['en-GB', 'en-US', 'es-ES'];
if (hourCycle12Languages.includes(locale)) {
// Set hourCycle to 12 for languages for which we want to display AM / PM
hourCycle = 'h12';
}

// Set default readout functions
const {
secondsAgoReadout = count =>
Expand Down Expand Up @@ -61,12 +69,12 @@ function getDateString({
const mins = parseInt(offset / ONE_MINUTE);
dateString = minutesAgoReadout(mins);
}
// Occcured today...
// Occured today...
else if (offset < ms_today) {
// Number of ms until end of the day...
delay = ONE_DAY - ms_today;
dateString = new Intl.DateTimeFormat(locale, {
hourCycle: 'h12',
hourCycle,
hour: 'numeric',
minute: 'numeric',
}).format(time);
Expand All @@ -78,7 +86,7 @@ function getDateString({
// Get day
dateString = new Intl.DateTimeFormat(locale, {
weekday: 'short',
hourCycle: 'h12',
hourCycle,
hour: 'numeric',
}).format(time);
} else if (time.getYear() === systemtime.getYear()) {
Expand Down

0 comments on commit 69805a3

Please sign in to comment.