Skip to content

Commit 7adff2e

Browse files
committed
Merge main into experiment-temporal-api
2 parents 1a1f648 + 9540657 commit 7adff2e

File tree

7 files changed

+86
-23
lines changed

7 files changed

+86
-23
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ datetime(now, 'time') // '10:29'
5858
datetimeTz(now, 'datetime', -7) // '2021-03-14T10:29-07:00'
5959
utc(now, 'time') // '09:29Z'
6060

61-
tzOffset(-9, 30) // '-09:30' (Marquesas Islands)
61+
tzOffset(-9, -30) // '-09:30' (Marquesas Islands)
6262
duration({ d: 4, h: 3, m: 17 }) // 'P4DT3H17M'
6363

6464
const importantMeeting = new DateTime(2021, 12, 17, 19, 00) // 17/11
@@ -209,14 +209,17 @@ import { tzOffset } from 'datetime-attribute'
209209

210210
tzOffset(3) // '+03:00' (Moscow)
211211

212-
tzOffset(-9, 30) // '-09:30' (Marquesas Islands)
212+
tzOffset(-9, -30) // '-09:30' (Marquesas Islands)
213213
tzOffset(-9.5) // '-09:30' (same with 1 parameter)
214214

215+
tzOffset(5, -30) // '+04:30' (Afghanistan)
216+
tzOffset(5, 30) // '+05:30' (India)
217+
215218
tzOffset(0) // 'Z' (Ghana; 'Z' is equal to '+00:00')
216219

217220
// in Belgium
218221
tzOffset() // '+01:00'
219-
tzOffset() // '+02:00' (under daylight time saving)
222+
tzOffset() // '+02:00' (under daylight saving time)
220223
```
221224

222225
### Hours-minutes separator
@@ -251,7 +254,7 @@ tzOffset(44) // '+20:00'
251254
tzOffset(44, 0, true) // '-04:00'
252255
```
253256

254-
Curious about timezones? Have a look at [the timezone map](https://fr.m.wikipedia.org/wiki/Fichier:World_Time_Zones_Map.png) and the [daylight time saving chaos](https://en.wikipedia.org/wiki/Daylight_saving_time_by_country).
257+
Curious about timezones? Have a look at [the timezone map](https://fr.m.wikipedia.org/wiki/Fichier:World_Time_Zones_Map.png) and the [daylight saving time chaos](https://en.wikipedia.org/wiki/Daylight_saving_time_by_country).
255258

256259
## Adding a timezone offset to a moment with `datetimeTz()`
257260

@@ -296,7 +299,7 @@ datetimeTz(now, 'time', -3, 30) // '23:51-03:30'
296299
datetimeTz(now, 'time', -14, 0, true) // '23:51+10:00'
297300
```
298301

299-
`datetimeTz()` **does not convert** your moment to another timezone: it **only adds the wanted timezone** to the moment. Its purpose is to generate a valid `datetime` attribute saying “here’s a moment, it has this [hours, minutes] timezone offset”.
302+
`datetimeTz()` **does not convert** your moment to another timezone: it **only adds the wanted timezone** to the moment. Its purpose is to generate a valid `datetime` attribute saying “here’s a moment, it has this [hours, minutes and] timezone offset”.
300303

301304
Let’s take this sentence and its HTML:
302305

src/datetime.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, expect, test } from 'vitest'
22

33
import { datetime, datetimeTz, tzOffset, utc } from './index.js'
4-
import { getNormalizeDay } from './utils/date.js'
4+
import { getNormalizedDay } from './utils/date.js'
55

66
const togoIndependanceDay = new Date(1960, 3, 27)
77
const date = togoIndependanceDay // alias for the sake of brevity
@@ -90,7 +90,7 @@ describe('datetime', () => {
9090

9191
// 1st day of the year is after Thurdsay
9292
test('week on 2021-01-01 is 2020-W53', () => {
93-
expect(getNormalizeDay(january1st2021)).toBeGreaterThan(4)
93+
expect(getNormalizedDay(january1st2021)).toBeGreaterThan(4)
9494
expect(datetime(january1st2021, 'week')).toBe('2020-W53')
9595
})
9696

@@ -109,7 +109,7 @@ describe('datetime', () => {
109109
// 1st day of the month is after Thurdsay, but it’s not in January
110110
test('week on 2021-03-01 is 2021-W17', () => {
111111
const march1st2021 = new Date(2021, 4, 1)
112-
expect(getNormalizeDay(march1st2021)).toBeGreaterThan(4)
112+
expect(getNormalizedDay(march1st2021)).toBeGreaterThan(4)
113113
expect(datetime(march1st2021, 'week')).toBe('2021-W17')
114114
})
115115

src/timezone.test.js

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,49 +21,89 @@ describe('tzOffset', () => {
2121

2222
describe('in boundaries', () => {
2323

24-
test('tzOffset(-8) is -08:00', () => {
24+
test('tzOffset(-23, -59) is -23:59', () => {
25+
expect(tzOffset(-23, -59)).toBe('-23:59')
26+
})
27+
28+
test('tzOffset(-9, -30) is -09:30', () => {
29+
expect(tzOffset(-9, -30)).toBe('-09:30')
30+
})
31+
32+
test('tzOffset(-9, 30) is -08:30', () => {
33+
expect(tzOffset(-9, 30)).toBe('-08:30')
34+
})
35+
36+
test('tzOffset(-8) is -08:00', () => {
2537
expect(tzOffset(-8)).toBe('-08:00')
2638
})
2739

28-
test('tzOffset(-4.5) is -04:30', () => {
40+
test('tzOffset(-4.5) is -04:30', () => {
2941
expect(tzOffset(-4.5)).toBe('-04:30')
3042
})
3143

32-
test('tzOffset(0, -30) is -00:30', () => {
44+
test('tzOffset(0, -30) is -00:30', () => {
3345
expect(tzOffset(0, -30)).toBe('-00:30')
3446
})
3547

36-
test('tzOffset(0) is Z', () => {
48+
test('tzOffset(0) is Z', () => {
3749
expect(tzOffset(0)).toBe('Z')
3850
})
3951

40-
test('tzOffset(0, 30) is +00:30', () => {
52+
test('tzOffset(0, 30) is +00:30', () => {
4153
expect(tzOffset(0, 30)).toBe('+00:30')
4254
})
4355

44-
test('tzOffset(1) is +01:00', () => {
56+
test('tzOffset(1) is +01:00', () => {
4557
expect(tzOffset(1)).toBe('+01:00')
4658
})
4759

48-
test('tzOffset(2, -200) is -01:20', () => {
60+
test('tzOffset(2, -200) is -01:20', () => {
4961
expect(tzOffset(2, -200)).toBe('-01:20')
5062
})
5163

52-
test('tzOffset(4, 30) is +04:30', () => {
64+
test('tzOffset(4, 30) is +04:30', () => {
5365
expect(tzOffset(4, 30)).toBe('+04:30')
5466
})
5567

56-
test('tzOffset(12, 45) is +12:45', () => {
68+
test('tzOffset(12, 45) is +12:45', () => {
5769
expect(tzOffset(12, 45)).toBe('+12:45')
5870
})
5971

60-
test('tzOffset(12.75) is +12:45', () => {
72+
test('tzOffset(12.75) is +12:45', () => {
6173
expect(tzOffset(12.75)).toBe('+12:45')
6274
})
75+
76+
test('tzOffset(23, 59) is +23:59', () => {
77+
expect(tzOffset(23, 59)).toBe('+23:59')
78+
})
6379
})
6480

6581
describe('out of boundaries', () => {
6682

83+
test('tzOffset(-24) is Z', () => {
84+
expect(tzOffset(-24)).toBe('Z')
85+
})
86+
87+
test('tzOffset(24) is Z', () => {
88+
expect(tzOffset(24)).toBe('Z')
89+
})
90+
91+
test('tzOffset(-24, 0, true) is Z', () => {
92+
expect(tzOffset(-24, 0, true)).toBe('Z')
93+
})
94+
95+
test('tzOffset(24, 0, true) is Z', () => {
96+
expect(tzOffset(24, 0, true)).toBe('Z')
97+
})
98+
99+
test('tzOffset(-24, -1) is -00:01', () => {
100+
expect(tzOffset(-24, -1)).toBe('-00:01')
101+
})
102+
103+
test('tzOffset(24, 1) is 00:01', () => {
104+
expect(tzOffset(24, 1)).toBe('+00:01')
105+
})
106+
67107
test('tzOffset(-35) is -11:00', () => {
68108
expect(tzOffset(-35)).toBe('-11:00')
69109
})

src/utils/const.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
export const MINUTES_PER_DAY = 60 * 24
22
export const MILLISECONDS_PER_DAY = 1000 * 60 * 60 * 24
33
export const MILLISECONDS_PER_WEEK = MILLISECONDS_PER_DAY * 7
4-
export const REAL_LIFE_LOWER_TIMEZONE = -12 * 60
5-
export const REAL_LIFE_UPPER_TIMEZONE = 14 * 60
64

7-
// Local timezone offset from UTC, in minutes
5+
/**
6+
* The farthest timezone offset **behind** UTC time.
7+
*/
8+
export const REAL_LIFE_LOWER_TIMEZONE = -12 * 60 // -12:00
9+
10+
/**
11+
* The farthest timezone offset **ahead of** UTC time.
12+
*/
13+
export const REAL_LIFE_UPPER_TIMEZONE = 14 * 60 // +14:00
14+
15+
/**
16+
* Local timezone offset from UTC, in minutes.
17+
*/
818
export const LOCAL_TZ_OFFSET = (new Date()).getTimezoneOffset() * -1

src/utils/date.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function daysBetween(date, furtherDate) {
3535
* @returns {number}
3636
*/
3737
export function weekNumber(date) {
38-
const dayIndex = getNormalizeDay(date)
38+
const dayIndex = getNormalizedDay(date)
3939

4040
const sameWeekThursday = new Date(date)
4141
sameWeekThursday.setDate(date.getDate() + 4 - dayIndex)
@@ -52,4 +52,4 @@ export function weekNumber(date) {
5252
* @param {Date} date
5353
* @returns {number}
5454
*/
55-
export const getNormalizeDay = date => date.getDay() || 7
55+
export const getNormalizedDay = date => date.getDay() || 7

types/utils/const.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
export const MINUTES_PER_DAY: number;
22
export const MILLISECONDS_PER_DAY: number;
33
export const MILLISECONDS_PER_WEEK: number;
4+
/**
5+
* The farthest timezone offset **behind** UTC time.
6+
*/
47
export const REAL_LIFE_LOWER_TIMEZONE: number;
8+
/**
9+
* The farthest timezone offset **ahead of** UTC time.
10+
*/
511
export const REAL_LIFE_UPPER_TIMEZONE: number;
12+
/**
13+
* Local timezone offset from UTC, in minutes.
14+
*/
615
export const LOCAL_TZ_OFFSET: number;

types/utils/date.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ export function daysBetween(date: Date, furtherDate: Date): number;
1818
* @returns {number}
1919
*/
2020
export function weekNumber(date: Date): number;
21+
export function getNormalizedDay(date: Date): number;

0 commit comments

Comments
 (0)