-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: 🤖 user timezone handling (#90)
* chore: 🤖 guess user timezone, modifies dateRelative * chore: 🤖 chante date parser * chore: 🤖 remove comments * chore: 🤖 WIP offset time based in user timezone * test: 💍 test the data computations * test: 💍 use past time, not future time computations
- Loading branch information
Showing
4 changed files
with
101 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { dateRelative } from './date'; | ||
|
||
describe('date', () => { | ||
describe('dateRelative', () => { | ||
describe('for date 10/12/2021 15h00 (as ISOString)', () => { | ||
const timezoneEuropeLondon = 'Europe/London'; | ||
const timezoneAmericaLosAngels = 'America/Los_Angeles'; | ||
const timestamp = '2021-12-10T15:00:00.000Z'; | ||
const timestampMinusEigthHours = '2021-12-10T07:00:00.000Z'; | ||
const timestampPlusThirtyMinutes = '2021-12-10T15:30:00.000Z'; | ||
const timestampPlusTwoHours = '2021-12-10T17:00:00.000Z'; | ||
const getNow = ( | ||
timestamp: string, | ||
timeZone: string, | ||
) => new Date( | ||
new Date(timestamp).toLocaleString("en-US", { timeZone }), | ||
).toISOString(); | ||
|
||
describe('on timezone Europe/London', () => { | ||
it('on checking immediately should describe as few seconds ago', () => { | ||
const now = getNow( | ||
timestamp, | ||
timezoneEuropeLondon, | ||
); | ||
const humanFriendlyDate = dateRelative( | ||
timestamp, | ||
now, | ||
); | ||
|
||
const expected = 'a few seconds ago'; | ||
|
||
expect(humanFriendlyDate).toBe(expected); | ||
}); | ||
|
||
it('on checking 30m after should be a 30m difference', () => { | ||
const now = getNow( | ||
timestampPlusThirtyMinutes, | ||
timezoneEuropeLondon, | ||
); | ||
const humanFriendlyDate = dateRelative( | ||
timestamp, | ||
now, | ||
); | ||
|
||
const expected = '30 minutes ago'; | ||
|
||
expect(humanFriendlyDate).toBe(expected); | ||
}); | ||
}); | ||
|
||
describe('on timezone America/Los Angeles', () => { | ||
it('on a timestamp which is 8 hours earlier should show few seconds ago', () => { | ||
const now = getNow( | ||
timestamp, | ||
timezoneAmericaLosAngels, | ||
); | ||
const humanFriendlyDate = dateRelative( | ||
timestampMinusEigthHours, | ||
now, | ||
); | ||
|
||
const expected = 'a few seconds ago'; | ||
|
||
expect(humanFriendlyDate).toBe(expected); | ||
}); | ||
|
||
it('on 2 hours from source timestamp should be a 2 hours difference', () => { | ||
const now = getNow( | ||
timestampPlusTwoHours, | ||
timezoneAmericaLosAngels, | ||
); | ||
const humanFriendlyDate = dateRelative( | ||
timestampMinusEigthHours, | ||
now, | ||
); | ||
|
||
const expected = '2 hours ago'; | ||
|
||
expect(humanFriendlyDate).toBe(expected); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,23 @@ | ||
/* eslint-disable import/prefer-default-export */ | ||
import dayjs from 'dayjs'; | ||
import utc from 'dayjs/plugin/utc'; | ||
import timezone from 'dayjs/plugin/timezone'; | ||
import relativeTime from 'dayjs/plugin/relativeTime'; | ||
import customParseFormat from 'dayjs/plugin/customParseFormat'; | ||
|
||
// Extensions | ||
[utc, relativeTime, customParseFormat].forEach((plugin) => dayjs.extend(plugin as any)); | ||
[utc, timezone, relativeTime, customParseFormat].forEach((plugin) => dayjs.extend(plugin as any)); | ||
|
||
export const dateRelative = (timestamp: string) => dayjs.utc(timestamp, 'DD/MM/YYYY').fromNow(); | ||
// Defaults | ||
dayjs.tz.setDefault("Europe/London"); | ||
|
||
const USER_TIMEZONE = dayjs.tz.guess(); | ||
|
||
const NOW = new Date( | ||
new Date().toLocaleString("en-US", { timeZone: USER_TIMEZONE }) | ||
).toISOString(); | ||
|
||
export const dateRelative = ( | ||
timestamp: string, | ||
now: string = NOW, | ||
) => dayjs.utc(timestamp).from(now); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters