Repository navigation
Expand file tree
/
Copy pathDateTime.ts
More file actions
2874 lines (2754 loc) · 79.6 KB
/
Copy pathDateTime.ts
File metadata and controls
2874 lines (2754 loc) · 79.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Works with absolute instants, UTC date-times, zoned date-times, and time
* zones.
*
* A `DateTime` always represents an absolute point in time with epoch
* milliseconds. It may also carry a `TimeZone` for calendar parts, formatting,
* and zone-aware transformations. This module includes constructors, time-zone
* helpers, comparisons, date arithmetic, current-time effects, and formatting
* functions.
*
* @since 3.6.0
*/
import type { IllegalArgumentError } from "./Cause.ts"
import * as Context from "./Context.ts"
import type * as Duration from "./Duration.ts"
import * as Effect from "./Effect.ts"
import type * as Equ from "./Equivalence.ts"
import { dual, flow, type LazyArg } from "./Function.ts"
import type { Inspectable } from "./Inspectable.ts"
import * as Internal from "./internal/dateTime.ts"
import { provideService } from "./internal/effect.ts"
import * as Layer from "./Layer.ts"
import type * as Option from "./Option.ts"
import type * as order from "./Order.ts"
import type { Pipeable } from "./Pipeable.ts"
const TypeId = Internal.TypeId
const TimeZoneTypeId = Internal.TimeZoneTypeId
/**
* A `DateTime` represents a point in time. It can optionally have a time zone
* associated with it.
*
* @category models
* @since 3.6.0
*/
export type DateTime = Utc | Zoned
/**
* Represents a `DateTime` stored as an absolute UTC instant with no associated
* time zone.
*
* **Details**
*
* Use `DateTime.isUtc` to narrow a `DateTime` to this variant.
*
* @category models
* @since 3.6.0
*/
export interface Utc extends DateTime.Proto {
readonly _tag: "Utc"
readonly epochMilliseconds: number
partsUtc: DateTime.PartsWithWeekday | undefined
}
/**
* Represents a `DateTime` with an associated `TimeZone`.
*
* **Details**
*
* A zoned value still represents an absolute instant through
* `epochMilliseconds`, while the time zone is used for wall-clock parts,
* formatting, and zone-aware transformations.
*
* @category models
* @since 3.6.0
*/
export interface Zoned extends DateTime.Proto {
readonly _tag: "Zoned"
readonly epochMilliseconds: number
readonly zone: TimeZone
adjustedEpochMilliseconds: number | undefined
partsAdjusted: DateTime.PartsWithWeekday | undefined
partsUtc: DateTime.PartsWithWeekday | undefined
}
/**
* Companion namespace containing the public helper types used by `DateTime`
* constructors, parts APIs, formatting, and date/time arithmetic.
*
* @since 3.6.0
*/
export declare namespace DateTime {
/**
* Input accepted by `DateTime.make`, `DateTime.makeUnsafe`, and the zoned
* constructors.
*
* **Details**
*
* Includes existing `DateTime` values, partial date parts, epoch-millisecond
* objects, epoch milliseconds, JavaScript `Date` instances, and parseable date
* strings.
*
* @category models
* @since 3.6.0
*/
export type Input = DateTime | Partial<Parts> | Instant | InstantWithZone | Date | number | string
/**
* Type-level helper used by constructors to preserve a zoned input.
*
* **Details**
*
* When the input type is `DateTime.Zoned`, the result type is
* `DateTime.Zoned`; otherwise the result type is `DateTime.Utc`.
*
* @category models
* @since 3.6.0
*/
export type PreserveZone<A extends DateTime.Input> = A extends Zoned ? Zoned : Utc
/**
* Date and time unit name accepted by `DateTime` rounding and arithmetic
* APIs.
*
* **Details**
*
* Includes both singular units, such as `"day"`, and plural units, such as
* `"days"`.
*
* @category models
* @since 3.6.0
*/
export type Unit = UnitSingular | UnitPlural
/**
* Singular date and time unit names used by rounding APIs such as
* `DateTime.startOf`, `DateTime.endOf`, and `DateTime.nearest`.
*
* @category models
* @since 3.6.0
*/
export type UnitSingular =
| "millisecond"
| "second"
| "minute"
| "hour"
| "day"
| "week"
| "month"
| "year"
/**
* Plural date and time unit names used by `DateTime.PartsForMath` for
* amount-based arithmetic.
*
* @category models
* @since 3.6.0
*/
export type UnitPlural =
| "milliseconds"
| "seconds"
| "minutes"
| "hours"
| "days"
| "weeks"
| "months"
| "years"
/**
* Calendar and time components of a `DateTime`, including the weekday.
*
* **Details**
*
* `month` is one-based (`1` for January through `12` for December), and
* `weekDay` follows JavaScript `Date#getUTCDay` numbering (`0` for Sunday
* through `6` for Saturday).
*
* @category models
* @since 3.6.0
*/
export interface PartsWithWeekday {
readonly millisecond: number
readonly second: number
readonly minute: number
readonly hour: number
readonly day: number
readonly weekDay: number
readonly month: number
readonly year: number
}
/**
* Calendar and time components of a `DateTime`, without weekday information.
*
* **Details**
*
* `month` is one-based (`1` for January through `12` for December).
*
* @category models
* @since 3.6.0
*/
export interface Parts {
readonly millisecond: number
readonly second: number
readonly minute: number
readonly hour: number
readonly day: number
readonly month: number
readonly year: number
}
/**
* Plural amount fields accepted by `DateTime.add` and `DateTime.subtract`.
*
* **Details**
*
* Each field represents the number of units to add or subtract for that part.
*
* @category models
* @since 3.6.0
*/
export interface PartsForMath {
readonly milliseconds: number
readonly seconds: number
readonly minutes: number
readonly hours: number
readonly days: number
readonly weeks: number
readonly months: number
readonly years: number
}
/**
* Object input representing an absolute instant as milliseconds since the Unix
* epoch.
*
* @category models
* @since 4.0.0
*/
export interface Instant {
readonly epochMilliseconds: number
}
/**
* Object input representing an absolute instant plus a time zone identifier.
*
* **Details**
*
* `DateTime.makeZoned` and `DateTime.makeZonedUnsafe` use `timeZoneId` when
* no explicit `timeZone` option is supplied.
*
* @category models
* @since 4.0.0
*/
export interface InstantWithZone {
readonly timeZoneId: string
readonly epochMilliseconds: number
}
/**
* Shared protocol implemented by all `DateTime` values.
*
* **Details**
*
* Provides the `DateTime` type identifier along with pipe and inspection
* support.
*
* @category models
* @since 3.6.0
*/
export interface Proto extends Pipeable, Inspectable {
readonly [TypeId]: typeof TypeId
}
}
/**
* Represents a time zone used by `DateTime.Zoned`.
*
* **Details**
*
* A `TimeZone` is either a fixed offset from UTC or a named IANA time zone.
*
* @category models
* @since 3.6.0
*/
export type TimeZone = TimeZone.Offset | TimeZone.Named
/**
* Companion namespace containing the public variant and protocol types for
* `TimeZone`.
*
* @since 3.6.0
*/
export declare namespace TimeZone {
/**
* Shared protocol implemented by all `TimeZone` values.
*
* **Details**
*
* Provides the `TimeZone` type identifier and inspection support.
*
* @category models
* @since 3.6.0
*/
export interface Proto extends Inspectable {
readonly [TimeZoneTypeId]: typeof TimeZoneTypeId
}
/**
* Fixed-offset time zone.
*
* **Details**
*
* The `offset` is measured in milliseconds from UTC. Positive offsets are
* ahead of UTC, and negative offsets are behind UTC.
*
* @category models
* @since 3.6.0
*/
export interface Offset extends Proto {
readonly _tag: "Offset"
readonly offset: number
}
/**
* Named IANA time zone.
*
* **Details**
*
* The `id` field contains the resolved time zone identifier, such as
* `"Europe/London"` or `"America/New_York"`.
*
* @category models
* @since 3.6.0
*/
export interface Named extends Proto {
readonly _tag: "Named"
readonly id: string
/** @internal */
readonly format: Intl.DateTimeFormat
}
}
/**
* A `Disambiguation` is used to resolve ambiguities when a `DateTime` is
* ambiguous, such as during a daylight saving time transition.
*
* **Details**
*
* For more information, see the [Temporal documentation](https://tc39.es/proposal-temporal/docs/timezone.html#ambiguity-due-to-dst-or-other-time-zone-offset-changes)
*
* - `"compatible"`: (default) Behavior matching Temporal API and legacy JavaScript Date and moment.js.
* For repeated times, chooses the earlier occurrence. For gap times, chooses the later interpretation.
*
* - `"earlier"`: For repeated times, always choose the earlier occurrence.
* For gap times, choose the time before the gap.
*
* - `"later"`: For repeated times, always choose the later occurrence.
* For gap times, choose the time after the gap.
*
* - `"reject"`: Throw an `RangeError` when encountering ambiguous or non-existent times.
*
* **Example** (Resolving ambiguous local times)
*
* ```ts import.meta.vitest
* import { DateTime, Option } from "effect"
*
* // Fall-back example: 01:30 on Nov 2, 2025 in New York happens twice
* const ambiguousTime = { year: 2025, month: 11, day: 2, hour: 1, minute: 30 }
* const timeZone = DateTime.zoneMakeNamedUnsafe("America/New_York")
*
* const earlier = DateTime.makeZoned(ambiguousTime, {
* timeZone,
* adjustForTimeZone: true,
* disambiguation: "earlier"
* })
* // Earlier occurrence (DST time): 2025-11-02T05:30:00.000Z
*
* const later = DateTime.makeZoned(ambiguousTime, {
* timeZone,
* adjustForTimeZone: true,
* disambiguation: "later"
* })
* // Later occurrence (standard time): 2025-11-02T06:30:00.000Z
*
* // Gap example: 02:30 on Mar 9, 2025 in New York doesn't exist
* const gapTime = { year: 2025, month: 3, day: 9, hour: 2, minute: 30 }
*
* const beforeGap = DateTime.makeZoned(gapTime, {
* timeZone,
* adjustForTimeZone: true,
* disambiguation: "earlier"
* })
* // Time before gap: 2025-03-09T06:30:00.000Z (01:30 EST)
*
* const afterGap = DateTime.makeZoned(gapTime, {
* timeZone,
* adjustForTimeZone: true,
* disambiguation: "later"
* })
* // Time after gap: 2025-03-09T07:30:00.000Z (03:30 EDT)
*
* earlier.pipe(Option.getOrThrow, DateTime.formatIso) // => "2025-11-02T05:30:00.000Z"
* later.pipe(Option.getOrThrow, DateTime.formatIso) // => "2025-11-02T06:30:00.000Z"
* beforeGap.pipe(Option.getOrThrow, DateTime.formatIso) // => "2025-03-09T06:30:00.000Z"
* afterGap.pipe(Option.getOrThrow, DateTime.formatIso) // => "2025-03-09T07:30:00.000Z"
* ```
*
* @category models
* @since 3.18.0
*/
export type Disambiguation = "compatible" | "earlier" | "later" | "reject"
// =============================================================================
// guards
// =============================================================================
/**
* Checks whether a value is a `DateTime`.
*
* **When to use**
*
* Use to narrow an unknown value before treating it as a `DateTime`.
*
* @see {@link isUtc} for narrowing a known `DateTime` to UTC
* @see {@link isZoned} for narrowing a known `DateTime` to zoned
*
* @category guards
* @since 3.6.0
*/
export const isDateTime: (u: unknown) => u is DateTime = Internal.isDateTime
/**
* Checks whether a value is a `TimeZone`.
*
* **When to use**
*
* Use to narrow unknown input to any `TimeZone` before passing it to APIs that
* accept either fixed-offset or named time zones.
*
* @see {@link isTimeZoneOffset} for narrowing to fixed-offset time zones
* @see {@link isTimeZoneNamed} for narrowing to named time zones
*
* @category guards
* @since 3.6.0
*/
export const isTimeZone: (u: unknown) => u is TimeZone = Internal.isTimeZone
/**
* Checks whether a value is an offset-based `TimeZone`.
*
* **When to use**
*
* Use when you need to narrow an unknown or union `TimeZone` value to the
* fixed-offset variant before reading its offset in milliseconds.
*
* @see {@link isTimeZone} for checking either time zone variant
* @see {@link isTimeZoneNamed} for narrowing to named time zones
*
* @category guards
* @since 3.6.0
*/
export const isTimeZoneOffset: (u: unknown) => u is TimeZone.Offset = Internal.isTimeZoneOffset
/**
* Checks whether a value is a named `TimeZone` (IANA time zone).
*
* **When to use**
*
* Use to narrow an unknown value to the `TimeZone.Named` variant before
* reading named-zone fields such as `id`.
*
* @see {@link isTimeZone} for checking either time zone variant
* @see {@link isTimeZoneOffset} for narrowing to fixed-offset time zones
*
* @category guards
* @since 3.6.0
*/
export const isTimeZoneNamed: (u: unknown) => u is TimeZone.Named = Internal.isTimeZoneNamed
/**
* Checks whether a `DateTime` is a UTC `DateTime` (no time zone information).
*
* **When to use**
*
* Use to narrow a `DateTime` before passing it to code that requires a UTC
* value without an associated time zone.
*
* @see {@link isZoned} for narrowing to zoned date-times
* @see {@link match} for handling both UTC and zoned cases
*
* @category guards
* @since 3.6.0
*/
export const isUtc: (self: DateTime) => self is Utc = Internal.isUtc
/**
* Checks whether a `DateTime` is a zoned `DateTime` (has time zone information).
*
* **When to use**
*
* Use to narrow a known `DateTime` before reading its zone or passing it to
* APIs that require `DateTime.Zoned`.
*
* @see {@link isUtc} for narrowing to UTC date-times
* @see {@link match} for handling both UTC and zoned cases
*
* @category guards
* @since 3.6.0
*/
export const isZoned: (self: DateTime) => self is Zoned = Internal.isZoned
// =============================================================================
// instances
// =============================================================================
/**
* Provides an `Equivalence` for comparing two `DateTime` values for equality.
*
* **Details**
*
* Two `DateTime` values are considered equivalent if they represent the same
* point in time, regardless of their time zone.
*
* **Example** (Comparing DateTime values for equivalence)
*
* ```ts import.meta.vitest
* import { DateTime } from "effect"
*
* const utc = DateTime.makeUnsafe("2024-01-01T12:00:00Z")
* const zoned = DateTime.makeZonedUnsafe("2024-01-01T12:00:00Z", {
* timeZone: "Europe/London"
* })
*
* DateTime.Equivalence(utc, zoned) // => true
* ```
*
* @category instances
* @since 3.6.0
*/
export const Equivalence: Equ.Equivalence<DateTime> = Internal.Equivalence
/**
* Provides an `Order` for comparing and sorting `DateTime` values.
*
* **Details**
*
* `DateTime` values are ordered by their epoch milliseconds, so earlier times
* come before later times regardless of time zone.
*
* **Example** (Sorting DateTime values chronologically)
*
* ```ts import.meta.vitest
* import { Array, DateTime } from "effect"
*
* const dates = [
* DateTime.makeUnsafe("2024-03-01"),
* DateTime.makeUnsafe("2024-01-01"),
* DateTime.makeUnsafe("2024-02-01")
* ]
*
* Array.sort(dates, DateTime.Order).map(DateTime.formatIsoDateUtc) // => ["2024-01-01", "2024-02-01", "2024-03-01"]
* ```
*
* @category instances
* @since 3.6.0
*/
export const Order: order.Order<DateTime> = Internal.Order
/**
* Returns a `DateTime` constrained between a minimum and maximum value.
*
* **Details**
*
* If the `DateTime` is before the minimum, the minimum is returned.
* If the `DateTime` is after the maximum, the maximum is returned.
* Otherwise, the original `DateTime` is returned.
*
* **Example** (Clamping DateTime values)
*
* ```ts import.meta.vitest
* import { DateTime } from "effect"
*
* const min = DateTime.makeUnsafe("2024-01-01")
* const max = DateTime.makeUnsafe("2024-12-31")
* const date = DateTime.makeUnsafe("2025-06-15")
*
* DateTime.clamp(date, { minimum: min, maximum: max }) // => DateTime.makeUnsafe("2024-12-31")
* ```
*
* @category ordering
* @since 3.6.0
*/
export const clamp: {
<Min extends DateTime, Max extends DateTime>(
options: { readonly minimum: Min; readonly maximum: Max }
): <A extends DateTime>(self: A) => A | Min | Max
<A extends DateTime, Min extends DateTime, Max extends DateTime>(
self: A,
options: { readonly minimum: Min; readonly maximum: Max }
): A | Min | Max
} = Internal.clamp
// =============================================================================
// constructors
// =============================================================================
/**
* Create a `DateTime` from a `Date`.
*
* **Details**
*
* If the `Date` is invalid, an `IllegalArgumentError` will be thrown.
*
* **Example** (Creating DateTime values from Dates)
*
* ```ts import.meta.vitest
* import { DateTime } from "effect"
*
* DateTime.fromDateUnsafe(new Date("2024-01-01T12:00:00Z")) // => DateTime.makeUnsafe("2024-01-01T12:00:00Z")
* ```
*
* @category constructors
* @since 4.0.0
*/
export const fromDateUnsafe: (date: Date) => Utc = Internal.fromDateUnsafe
/**
* Create a `DateTime` from supported input values.
*
* **When to use**
*
* Use when creating a `DateTime` from trusted input and construction failures
* should throw an `IllegalArgumentError` instead of returning `Option.none`.
*
* **Details**
*
* - A `DateTime`
* - A `Date` instance (invalid dates will throw an `IllegalArgumentError`)
* - The `number` of milliseconds since the Unix epoch
* - An object with the parts of a date
* - A `string` that can be parsed by `Date.parse`
*
* **Example** (Creating DateTime values unsafely)
*
* ```ts import.meta.vitest
* import { DateTime } from "effect"
*
* // from Date
* DateTime.makeUnsafe(new Date("2024-01-01T12:00:00Z")) // => DateTime.makeUnsafe("2024-01-01T12:00:00Z")
*
* // from parts
* DateTime.makeUnsafe({ year: 2024 }) // => DateTime.makeUnsafe("2024-01-01T00:00:00Z")
*
* // from string
* DateTime.makeUnsafe("2024-01-01") // => DateTime.makeUnsafe("2024-01-01T00:00:00Z")
* ```
*
* @category constructors
* @since 4.0.0
*/
export const makeUnsafe: <A extends DateTime.Input>(input: A) => DateTime.PreserveZone<A> = Internal.makeUnsafe
/**
* Creates a `DateTime.Utc` from the number of seconds since the Unix epoch.
*
* **Example** (Creating from epoch seconds)
*
* ```ts import.meta.vitest
* import { DateTime } from "effect"
*
* DateTime.fromEpochSeconds(1704067200).toJSON() // => "2024-01-01T00:00:00.000Z"
* ```
*
* @category constructors
* @since 4.0.0
*/
export const fromEpochSeconds: (seconds: number) => Utc = Internal.fromEpochSeconds
/**
* Create a `DateTime.Zoned` using `DateTime.makeUnsafe` and a time zone.
*
* **When to use**
*
* Use when the date/time input and zone options are trusted and invalid or
* rejected ambiguous times should throw instead of returning `Option.none`.
*
* **Details**
*
* The input is treated as UTC and then the time zone is attached, unless
* `adjustForTimeZone` is set to `true`. In that case, the input is treated as
* already in the time zone.
*
* When `adjustForTimeZone` is true and ambiguous times occur during DST transitions,
* the `disambiguation` option controls how to resolve the ambiguity:
* - `compatible` (default): Choose earlier time for repeated times, later for gaps
* - `earlier`: Always choose the earlier of two possible times
* - `later`: Always choose the later of two possible times
* - `reject`: Throw an error when ambiguous times are encountered
*
* **Example** (Creating zoned DateTime values unsafely)
*
* ```ts import.meta.vitest
* import { DateTime } from "effect"
*
* const zoned = DateTime.makeZonedUnsafe("2024-06-15T14:30:00Z", {
* timeZone: "Europe/London"
* })
*
* DateTime.formatIsoZoned(zoned) // => "2024-06-15T15:30:00.000+01:00[Europe/London]"
* ```
*
* @category constructors
* @since 4.0.0
*/
export const makeZonedUnsafe: (input: DateTime.Input, options?: {
readonly timeZone?: number | string | TimeZone | undefined
readonly adjustForTimeZone?: boolean | undefined
readonly disambiguation?: Disambiguation | undefined
}) => Zoned = Internal.makeZonedUnsafe
/**
* Creates a `DateTime.Zoned` safely from an input and a time zone.
*
* **Details**
*
* By default, the input is interpreted as a UTC instant and the time zone is
* attached without changing that instant. When `adjustForTimeZone` is `true`,
* the input is interpreted as wall-clock time in the target zone.
*
* When `adjustForTimeZone` is `true`, `disambiguation` controls
* daylight-saving gaps and repeated times:
*
* - `"compatible"` (default): chooses the earlier occurrence for repeated
* times and the later interpretation for gaps
* - `"earlier"`: chooses the earlier possible instant
* - `"later"`: chooses the later possible instant
* - `"reject"`: rejects ambiguous or nonexistent wall-clock times
*
* Returns `Some` when construction succeeds, or `None` when the input, time
* zone, or disambiguation cannot be resolved.
*
* **Example** (Creating optional zoned DateTime values)
*
* ```ts import.meta.vitest
* import { DateTime, Option } from "effect"
*
* const result = DateTime.makeZoned("2024-06-15T14:30:00Z", {
* timeZone: "Europe/London"
* })
*
* result.pipe(Option.map(DateTime.formatIsoZoned)) // => Option.some("2024-06-15T15:30:00.000+01:00[Europe/London]")
* ```
*
* @category constructors
* @since 3.6.0
*/
export const makeZoned: (
input: DateTime.Input,
options?: {
readonly timeZone?: number | string | TimeZone | undefined
readonly adjustForTimeZone?: boolean | undefined
readonly disambiguation?: Disambiguation | undefined
}
) => Option.Option<Zoned> = Internal.makeZoned
/**
* Creates a `DateTime` safely from supported input values.
*
* **Details**
*
* - A `DateTime`
* - A JavaScript `Date`
* - The number of milliseconds since the Unix epoch
* - An object with date and time parts
* - A string that can be parsed as a date
*
* Returns `Some` with the constructed `DateTime` when the input is valid, or
* `None` when construction would fail, including invalid `Date` instances or
* unparseable strings.
*
* **Example** (Creating optional DateTime values)
*
* ```ts import.meta.vitest
* import { DateTime, Option } from "effect"
*
* // from Date
* DateTime.make(new Date("2024-01-01T12:00:00Z")) // => Option.some(DateTime.makeUnsafe("2024-01-01T12:00:00Z"))
*
* // from parts
* DateTime.make({ year: 2024 }) // => Option.some(DateTime.makeUnsafe("2024-01-01T00:00:00Z"))
*
* // from string
* DateTime.make("2024-01-01") // => Option.some(DateTime.makeUnsafe("2024-01-01T00:00:00Z"))
*
* DateTime.make("not a date") // => Option.none()
* ```
*
* @category constructors
* @since 3.6.0
*/
export const make: <A extends DateTime.Input>(input: A) => Option.Option<DateTime.PreserveZone<A>> = Internal.make
/**
* Parses an ISO zoned date-time string into a `DateTime.Zoned` safely.
*
* **Details**
*
* Accepts named-zone strings such as
* `YYYY-MM-DDTHH:mm:ss.sss+HH:MM[Time/Zone]` and offset-only strings such as
* `YYYY-MM-DDTHH:mm:ss.sss+HH:MM`. Returns `None` when the input cannot be
* parsed.
*
* **Example** (Parsing zoned DateTime strings)
*
* ```ts import.meta.vitest
* import { DateTime, Option } from "effect"
*
* DateTime.makeZonedFromString(
* "2024-01-01T12:00:00+02:00[Europe/Berlin]"
* ).pipe(Option.map(DateTime.formatIsoZoned)) // => Option.some("2024-01-01T11:00:00.000+01:00[Europe/Berlin]")
*
* DateTime.makeZonedFromString("2024-01-01T12:00:00Z") // => Option.none()
* DateTime.makeZonedFromString("invalid") // => Option.none()
* ```
*
* @category constructors
* @since 3.6.0
*/
export const makeZonedFromString: (input: string) => Option.Option<Zoned> = Internal.makeZonedFromString
/**
* Gets the current time using the `Clock` service and converts it to a `DateTime`.
*
* **Example** (Getting the current DateTime)
*
* ```ts import.meta.vitest
* import { DateTime, Effect } from "effect"
* import { TestClock } from "effect/testing"
*
* await Effect.runPromise(Effect.map(DateTime.now, DateTime.isDateTime)) // => true
* ```
*
* @category constructors
* @since 3.6.0
*/
export const now: Effect.Effect<Utc> = Internal.now
/**
* Gets the current time from the `Clock` service and returns it as a
* JavaScript `Date`.
*
* **Example** (Getting the current Date)
*
* ```ts import.meta.vitest
* import { DateTime, Effect } from "effect"
* import { TestClock } from "effect/testing"
*
* await Effect.runPromise(Effect.map(DateTime.nowAsDate, (now) => now instanceof Date)) // => true
* ```
*
* @category constructors
* @since 3.14.0
*/
export const nowAsDate: Effect.Effect<Date> = Internal.nowAsDate
/**
* Gets the current time using `Date.now`.
*
* **When to use**
*
* Use when synchronous wall-clock access outside an Effect program is
* acceptable and testability through the `Clock` service is not needed.
*
* **Details**
*
* This is a synchronous version of `now` that directly uses `Date.now()`
* instead of the Effect `Clock` service.
*
* **Example** (Getting the current DateTime unsafely)
*
* ```ts import.meta.vitest
* import { DateTime } from "effect"
*
* Number.isFinite(DateTime.toEpochMillis(DateTime.nowUnsafe())) // => true
* ```
*
* @category constructors
* @since 4.0.0
*/
export const nowUnsafe: LazyArg<Utc> = Internal.nowUnsafe
// =============================================================================
// time zones
// =============================================================================
/**
* Converts a `DateTime` to a UTC `DateTime`.
*
* **When to use**
*
* Use to represent the same instant in UTC instead of its current time zone.
*
* **Details**
*
* The returned value keeps the same epoch milliseconds and changes only the
* `DateTime` representation to UTC.
*
* **Example** (Converting DateTime values to UTC)
*
* ```ts import.meta.vitest
* import { DateTime } from "effect"
*
* const now = DateTime.makeZonedUnsafe({ year: 2024 }, {
* timeZone: "Europe/London"
* })
*
* // set as UTC
* const utc: DateTime.Utc = DateTime.toUtc(now)
* utc // => DateTime.makeUnsafe("2024-01-01T00:00:00Z")
* ```
*
* @category converting
* @since 3.13.0
*/
export const toUtc: (self: DateTime) => Utc = Internal.toUtc
/**
* Sets the time zone of a `DateTime`, returning a new `DateTime.Zoned`.
*
* **Example** (Setting time zones)
*
* ```ts import.meta.vitest
* import { DateTime } from "effect"
*
* const zone = DateTime.zoneMakeNamedUnsafe("Europe/London")
* const zoned: DateTime.Zoned = DateTime.setZone(DateTime.makeUnsafe("2024-01-01"), zone)
*
* DateTime.isZoned(zoned) // => true
* ```
*
* @category transforming
* @since 3.6.0
*/
export const setZone: {
(zone: TimeZone, options?: {
readonly adjustForTimeZone?: boolean | undefined
readonly disambiguation?: Disambiguation | undefined
}): (self: DateTime) => Zoned
(self: DateTime, zone: TimeZone, options?: {
readonly adjustForTimeZone?: boolean | undefined
readonly disambiguation?: Disambiguation | undefined
}): Zoned
} = Internal.setZone
/**
* Adds a fixed offset time zone to a `DateTime`.
*
* **Details**
*
* The offset is in milliseconds.
*
* **Example** (Setting fixed-offset time zones)
*
* ```ts import.meta.vitest
* import { DateTime } from "effect"
*
* const dateTime = DateTime.makeUnsafe("2024-01-01")
* const zoned: DateTime.Zoned = DateTime.setZoneOffset(dateTime, 3 * 60 * 60 * 1000)
*
* DateTime.zoneToString(zoned.zone) // => "+03:00"
* ```
*
* @category transforming
* @since 3.6.0
*/
export const setZoneOffset: {
(offset: number, options?: {
readonly adjustForTimeZone?: boolean | undefined
readonly disambiguation?: Disambiguation | undefined
}): (self: DateTime) => Zoned
(self: DateTime, offset: number, options?: {
readonly adjustForTimeZone?: boolean | undefined
readonly disambiguation?: Disambiguation | undefined
}): Zoned
} = Internal.setZoneOffset
/**
* Attempts to create a named time zone from an IANA time zone identifier.
*
* **When to use**
*
* Use when the IANA zone id is trusted and invalid zones should throw instead
* of returning `Option.none` or failing in `Effect`.
*
* **Details**
*
* If the time zone is invalid, an `IllegalArgumentError` will be thrown.
*
* **Example** (Creating named time zones unsafely)
*
* ```ts import.meta.vitest
* import { DateTime } from "effect"
*
* DateTime.zoneToString(DateTime.zoneMakeNamedUnsafe("Europe/London")) // => "Europe/London"
* DateTime.zoneToString(DateTime.zoneMakeNamedUnsafe("Asia/Tokyo")) // => "Asia/Tokyo"
*
* // This would throw an IllegalArgumentError:
* // DateTime.zoneMakeNamedUnsafe("Invalid/Zone")