Skip to content

Commit 808726b

Browse files
refactor: date comparition utils
1 parent c468624 commit 808726b

5 files changed

Lines changed: 47 additions & 62 deletions

File tree

Methods.md

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
11
# What API methods do we need?
22

3-
[x] startOf(ZonedDateTime, unit): ZonedDateTime ✅
4-
[x] endOf(ZonedDateTime, unit): ZonedDateTime ✅
5-
[x] add(ZonedDateTime, duration): ZonedDateTime ✅
6-
[x] subtract(ZonedDateTime, duration): ZonedDateTime ✅
7-
[x] until(ZonedDateTime, ZonedDateTime): Duration ✅
8-
[x] since(ZonedDateTime, ZonedDateTime): Duration ✅
9-
[x] equals(ZonedDateTime, ZonedDateTime, unit): boolean ✅
10-
[x] isBefore(ZonedDateTime, ZonedDateTime): boolean ✅
11-
[x] isAfter(ZonedDateTime, ZonedDateTime): boolean ✅
12-
[x] round(unit): ZonedDateTime ✅
13-
[x] isSameOrBefore(ZonedDateTime, ZonedDateTime, unit): boolean ✅
14-
[x] isSameOrAfter(ZonedDateTime, ZonedDateTime, unit): boolean ✅
15-
[x] isBetween(ZonedDateTime, Range): boolean ✅
16-
[x] intersects(ZonedDateTime, Range): boolean ✅
17-
[ ] time(PlainTime): ZonedDateTime (use getter/setter?)
18-
[x] timeZone(IANATimeZoneId): ZonedDateTime (use getter/setter?) ✅
19-
[x] calendar(Calendar): ZonedDateTime (use getter/setter?) ✅
20-
[x] asEpoch ✅
21-
[x] asString ✅
22-
[x] asZonedDateTime ✅
3+
## Date Operations
4+
5+
- [x] `startOf(input: DateInput, options: StartOfOptions): DateOperationResult`
6+
- [x] `endOf(input: DateInput, options: EndOfOptions): DateOperationResult`
7+
- [x] `add(input: DateInput, options: AddOptions): DateOperationResult`
8+
- [x] `subtract(input: DateInput, options: SubtractOptions): DateOperationResult`
9+
- [x] `round(input: DateInput, options: RoundOptions): DateOperationResult`
10+
11+
## Duration Calculations
12+
13+
- [x] `until(start: DateInput, end: DateInput, options: UntilOptions): number`
14+
- [x] `since(start: DateInput, end: DateInput, options: SinceOptions): number`
15+
16+
## Comparisons
17+
18+
- [x] `equals(date1: DateInput, date2: DateInput, options: EqualsOptions): boolean`
19+
- [x] `isBefore(date1: DateInput, date2: DateInput, options?: IsBeforeOptions): boolean`
20+
- [x] `isAfter(date1: DateInput, date2: DateInput, options?: IsAfterOptions): boolean`
21+
- [x] `isSameOrBefore(date1: DateInput, date2: DateInput, options: IsSameOrBeforeOptions): boolean`
22+
- [x] `isSameOrAfter(date1: DateInput, date2: DateInput, options: IsSameOrAfterOptions): boolean`
23+
24+
## Range Operations
25+
26+
- [x] `isBetween(date: DateInput, options: IsBetweenOptions): boolean`
27+
- [x] `intersects(date: DateInput, options: IntersectsOptions): boolean`
28+
29+
## Type Definitions
30+
31+
- `DateInput = string | number | Date | Temporal.ZonedDateTime`
32+
- `DateOperationResult` - Object with `value`, `asDate()`, `asEpoch()`, `asString()`, `asZonedDateTime()`, etc.
33+
- `Range = { start: DateInput, end: DateInput }`
34+
35+
## Future Considerations
36+
37+
- [ ] `time(PlainTime): ZonedDateTime` (use getter/setter?)
38+
- [x] `timeZone(IANATimeZoneId): ZonedDateTime` (use getter/setter?) ✅
39+
- [x] `calendar(Calendar): ZonedDateTime` (use getter/setter?) ✅

packages/time/src/date/isSameOrBefore/isSameOrBefore.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ export function isSameOrBefore(
3838
if (options.unit === 'week') {
3939
const normalized1 = normalizeWeek(zdt1)
4040
const normalized2 = normalizeWeek(zdt2)
41-
return Temporal.ZonedDateTime.compare(normalized1, normalized2) >= 0
41+
return Temporal.ZonedDateTime.compare(normalized1, normalized2) <= 0
4242
}
4343

44-
return Temporal.ZonedDateTime.compare(zdt1, zdt2) >= 0
44+
return Temporal.ZonedDateTime.compare(zdt1, zdt2) <= 0
4545
}

packages/time/src/date/round/round.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Temporal } from '@js-temporal/polyfill'
2+
import { startOf } from '../startOf/startOf'
23
import { withDateOperation } from '../withDateOperation'
3-
import type { DateOperationOptions } from '../withDateOperation'
44
import type { DateInput } from '../types'
5-
import { startOf } from '../startOf/startOf'
5+
import type { DateOperationOptions } from '../withDateOperation'
66

77
export type RoundUnit =
88
| 'year'

packages/time/src/date/withDateOperation/withDateOperation.test.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,29 +74,6 @@ describe('withDateOperation', () => {
7474
expect(new Date(epoch).toISOString()).toContain('2024-03-15')
7575
})
7676

77-
test('should have asString method', () => {
78-
const result = mockOperation('2024-03-15T14:42:12.789Z', {
79-
unit: 'test',
80-
timeZone: 'UTC',
81-
})
82-
const str = result.asString()
83-
expect(typeof str).toBe('string')
84-
expect(str).toContain('2024-03-15')
85-
})
86-
87-
test('should have asLong method', () => {
88-
const result = mockOperation('2024-03-15T14:42:12.789Z', {
89-
unit: 'test',
90-
timeZone: 'America/New_York',
91-
calendar: 'gregory',
92-
})
93-
const long = result.asLong()
94-
expect(typeof long).toBe('string')
95-
expect(long).toContain('2024-03-15')
96-
expect(long).toContain('[America/New_York]')
97-
expect(long).toContain('[u-ca=gregory]')
98-
})
99-
10077
test('should have asZonedDateTime method', () => {
10178
const result = mockOperation('2024-03-15T14:42:12.789Z', {
10279
unit: 'test',

packages/time/src/date/withDateOperation/withDateOperation.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ export interface DateOperationOptions extends DateOptions {
1111

1212
function createDateOperationResult(
1313
zdt: Temporal.ZonedDateTime,
14-
options: Required<DateOptions>,
15-
returnFormat: ReturnFormat = 'standard',
14+
options: DateOperationOptions,
1615
) {
16+
const { returnFormat } = options
1717
const getValue = (): string => {
1818
switch (returnFormat) {
1919
case 'standard':
@@ -35,12 +35,6 @@ function createDateOperationResult(
3535
asEpoch: () => {
3636
return Number(zdt.epochNanoseconds / 1_000_000n)
3737
},
38-
asString: () => {
39-
return zdt.toInstant().toString()
40-
},
41-
asLong: () => {
42-
return `${zdt.toInstant().toString()}[${zdt.timeZoneId}][u-ca=${zdt.calendarId}]`
43-
},
4438
asZonedDateTime: () => {
4539
return zdt
4640
},
@@ -67,13 +61,10 @@ export function withDateOperation<TArgs>(
6761
const inputZdt = toZonedDateTime(input, timeZone, calendar)
6862
const resultZdt = fn(inputZdt, options)
6963

70-
return createDateOperationResult(
71-
resultZdt,
72-
{
73-
timeZone,
74-
calendar,
75-
},
64+
return createDateOperationResult(resultZdt, {
65+
timeZone,
66+
calendar,
7667
returnFormat,
77-
)
68+
})
7869
}
7970
}

0 commit comments

Comments
 (0)