-
-
Notifications
You must be signed in to change notification settings - Fork 425
Expand file tree
/
Copy pathDateAndTime.class.st
More file actions
1284 lines (1019 loc) · 32 KB
/
DateAndTime.class.st
File metadata and controls
1284 lines (1019 loc) · 32 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
"
I am DateAndTime.
I represent a point in time or timestamp as defined by ISO 8601.
I am a Magnitude.
I have nanosecond precision.
I am TimeZone aware.
I have zero duration.
DateAndTime now.
DateAndTime now asUTC rounded.
DateAndTime fromString: '1969-07-20T20:17:40.123+02:00'.
DateAndTime fromString: '1969-07-20T20:17:40Z'.
My implementation uses three SmallIntegers and a Duration:
julianDayNumber - julian day number (starting at midnight UTC rather than noon GMT).
seconds - number of seconds since midnight UTC. Always positive, between 0 and 86399.
nanos - the number of nanoseconds since the second. Always positive, between 0 and 999999999.
offset - duration from UTC.
The offset is used to print the date and time in a local time zone, but the date and time are handled in UTC internally.
The nanosecond attribute is often zero but it defined for full ISO compliance and is suitable for timestamping.
"
Class {
#name : 'DateAndTime',
#superclass : 'Magnitude',
#instVars : [
'seconds',
'offset',
'julianDayNumber',
'nanos'
],
#classVars : [
'LocalTimeZoneCache'
],
#pools : [
'ChronologyConstants'
],
#category : 'System-Time',
#package : 'System-Time'
}
{ #category : 'private' }
DateAndTime class >> basicYear: year month: month day: day hour: hour minute: minute second: second nanoSecond: nanoCount offset: utcOffset [
"Return a DateAndTime with the values in the given TimeZone (UTCOffset)"
| p q r s julianDayNumber localSeconds utcSeconds|
p := (month - 14) quo: 12.
q := year + 4800 + p.
r := month - 2 - (12 * p).
s := (year + 4900 + p) quo: 100.
julianDayNumber :=
((1461 * q) quo: 4) +
((367 * r) quo: 12) -
((3 * s) quo: 4) +
(day - 32075).
localSeconds := hour * 60 + minute * 60 + second.
utcSeconds := localSeconds - utcOffset asSeconds.
^self basicNew
setJdn: julianDayNumber
seconds: utcSeconds
nano: nanoCount
offset: utcOffset;
yourself
]
{ #category : 'system queries' }
DateAndTime class >> clockPrecision [
"One nanosecond precision"
^ Duration seconds: 0 nanoSeconds: 1
]
{ #category : 'instance creation queries' }
DateAndTime class >> current [
^ self now
]
{ #category : 'instance creation' }
DateAndTime class >> date: aDate time: aTime [
^ self
year: aDate year
month: aDate monthName
day: aDate dayOfMonth
hour: aTime hour
minute: aTime minute
second: aTime second
nanoSecond: aTime nanoSecond
offset: self localOffset
]
{ #category : 'instance creation' }
DateAndTime class >> dosEpoch [
"Answer a DateAndTime representing the DOS epoch (1 January 1980, midnight UTC)"
^ self basicNew
ticks: #(2444240 0 0) offset: Duration zero;
yourself
]
{ #category : 'instance creation queries' }
DateAndTime class >> epoch [
"Answer a DateAndTime representing the epoch: 1 January 1901"
^ (self julianDayNumber: Epoch) offset: 0
]
{ #category : 'instance creation' }
DateAndTime class >> fromDosTimestamp: anInteger [
^ (DosTimestamp on: anInteger) asDateAndTime
]
{ #category : 'instance creation' }
DateAndTime class >> fromInternalTime: anInteger [
"Answer an instance of the receiver with the time represented by anInteger. Pharo internal time is an integer representing time in the local timezone with an epoch of 1 Jan. 1901"
^(self fromSeconds: anInteger offset: 0) translateTo: self localOffset
]
{ #category : 'instance creation' }
DateAndTime class >> fromMethodTimestamp: aString [
"Answer a DateAndTime for the given timestamp read from the changes file."
^ [ self readFromString: aString ] on: Error do: [ nil ]
]
{ #category : 'instance creation' }
DateAndTime class >> fromSeconds: secondsSinceEpochUTC [
"Answer a DateAndTime since the epoch: 1 January 1901 for the seconds in UTC time"
^ self fromSeconds: secondsSinceEpochUTC offset: self localOffset
]
{ #category : 'instance creation' }
DateAndTime class >> fromSeconds: utcSecondsSinceEpoch offset: aUTCOffset [
"Answer a DateAndTime since the epoch: 1 January 1901 for the given timeZone"
| integerSeconds nanos |
integerSeconds := utcSecondsSinceEpoch truncated.
nanos := integerSeconds = utcSecondsSinceEpoch
ifTrue: [ 0 ]
ifFalse: [ ((utcSecondsSinceEpoch - integerSeconds) * NanosInSecond) asInteger ].
^ self basicNew ticks: {Epoch . integerSeconds . nanos} offset: aUTCOffset asDuration
]
{ #category : 'input' }
DateAndTime class >> fromString: aString [
^ self readFrom: aString readStream
]
{ #category : 'instance creation' }
DateAndTime class >> fromUnixTime: anInteger [
^ self fromSeconds: anInteger + 2177452800 "unix epoch constant"
]
{ #category : 'instance creation' }
DateAndTime class >> fuzzyReadFrom: aStream [
| bc year month day hour minute second nanos offset buffer ch |
aStream peek = $- ifTrue: [ aStream next. bc := -1] ifFalse: [bc := 1].
year := (aStream upTo: $-) extractIntegerPart * bc.
month := (aStream upTo: $-) extractIntegerPart ifNil: [1].
day := (aStream upTo: $T) extractIntegerPart ifNil: [1].
hour := (aStream upTo: $:) extractIntegerPart ifNil: [0].
buffer := '00:' copy. ch := nil.
minute := buffer writeStream.
[ aStream atEnd | (ch = $:) | (ch = $+) | (ch = $-) ]
whileFalse: [ ch := minute nextPut: aStream next. ].
(ch isNil or: [ch isDigit]) ifTrue: [ ch := $: ].
minute := (buffer readStream upTo: ch) extractIntegerPart.
buffer := '00.' copy.
second := buffer writeStream.
[ aStream atEnd | (ch = $.) | (ch = $+) | (ch = $-) ]
whileFalse: [ ch := second nextPut: aStream next. ].
(ch isNil or: [ch isDigit]) ifTrue: [ ch := $. ].
second := (buffer readStream upTo: ch) extractIntegerPart.
buffer := '000000000' copy.
(ch = $.) ifTrue: [
nanos := buffer writeStream.
[ aStream atEnd | ((ch := aStream next) = $+) | (ch = $-) ]
whileFalse: [ nanos nextPut: ch. ].
(ch isNil or: [ch isDigit]) ifTrue: [ ch := $+ ].
].
nanos := buffer extractIntegerPart.
aStream atEnd
ifTrue: [ offset := Duration zero ]
ifFalse: [ch := aStream next.
ch = $+ ifTrue: [ch := Character space].
offset := Duration fromString: ch asString, '0:', aStream upToEnd, ':0'].
^ self
year: year
month: month
day: day
hour: hour
minute: minute
second: second
nanoSecond: nanos
offset: offset
]
{ #category : 'instance creation' }
DateAndTime class >> julianDayNumber: aJulianDayNumber [
^ self basicNew
ticks: aJulianDayNumber days ticks offset: Duration new;
yourself
]
{ #category : 'instance creation' }
DateAndTime class >> julianDayNumber: aJulianDayNumber offset: aTimeZoneOffset [
"Return a DateAndTime at midnight local time at the given julian day"
| ticks |
"create a ticks representation in UTC, take the given julian day in local time"
ticks := aJulianDayNumber days ticks.
ticks at: 2 put: aTimeZoneOffset asSeconds negated.
^ self basicNew
ticks: ticks offset: aTimeZoneOffset;
yourself
]
{ #category : 'system queries' }
DateAndTime class >> localOffset [
"Answer the duration we are offset from UTC"
^ self localTimeZone offset
]
{ #category : 'time zones' }
DateAndTime class >> localTimeZone [
"Answer the local time zone"
^ LocalTimeZoneCache ifNil: [ LocalTimeZoneCache := TimeZone default ]
]
{ #category : 'time zones' }
DateAndTime class >> localTimeZone: aTimeZone [
"Set the local time zone"
"
DateAndTime localTimeZone: (TimeZone offset: 0 hours name: 'Universal Time' abbreviation: 'UTC').
DateAndTime localTimeZone: (TimeZone offset: -8 hours name: 'Pacific Standard Time' abbreviation: 'PST').
"
LocalTimeZoneCache := aTimeZone
]
{ #category : 'instance creation' }
DateAndTime class >> midnight [
^ self now midnight
]
{ #category : 'instance creation' }
DateAndTime class >> new [
"Answer a DateAndTime representing the epoch: 1 January 1901"
^ self epoch offset: self localOffset
]
{ #category : 'instance creation' }
DateAndTime class >> noon [
^ self now noon
]
{ #category : 'instance creation' }
DateAndTime class >> now [
"Answer the current date and time expressed in local time.
[ 10000 timesRepeat: [ self now. ] ] timeToRun / 10000.0 . "
| nanoTicks |
nanoTicks := Time microsecondClockValue * 1000.
^ self basicNew
setJdn: Epoch
seconds: 0
nano: nanoTicks
offset: self localOffset
]
{ #category : 'instance creation' }
DateAndTime class >> readFrom: aStream [
"Parse and return a new DateAndTime instance from stream,
as a Date, an optional Time and an optional TimeZone offset.
The time defaults to midnight, the timezone to the local offset"
"self readFrom: '2013-03-04T23:47:52.876+01:00' readStream"
^self readFrom: aStream defaultOffset: self localOffset
]
{ #category : 'instance creation' }
DateAndTime class >> readFrom: aStream defaultOffset: defaultOffset [
"Parse and return a new DateAndTime instance from stream,
as a Date, an optional Time and an optional TimeZone offset.
The time defaults to midnight, the timezone to defaultOffset"
"self readFrom: '2013-03-04T23:47:52.876+01:00' readStream"
| date time offset |
date := (DateParser readingFrom: aStream pattern: 'y-mm-dd')
continueAfterParsing;
parse.
[ aStream atEnd or: [ '0123456789Z+-' includes: aStream peek ] ]
whileFalse: [ aStream next ].
('0123456789' includes: aStream peek)
ifTrue: [ time := Time readFrom: aStream ]
ifFalse: [ time := Time midnight ].
aStream skipSeparators.
offset := self readTimezoneOffsetFrom: aStream default: defaultOffset.
^ self
year: date year
month: date monthIndex
day: date dayOfMonth
hour: time hour
minute: time minute
second: time second
nanoSecond: time nanoSecond
offset: offset
]
{ #category : 'private' }
DateAndTime class >> readOptionalSeparatorFrom: stream [
"Read an optional separator (non decimal digit) from stream and return it.
Return nil if nothing was read"
| isDigit |
stream atEnd
ifTrue: [ ^ nil ].
isDigit := '0123456789' includes: stream peek.
(isDigit or: [ ':' includes: stream peek ])
ifFalse: [ ^ nil ].
isDigit
ifFalse: [ stream next ]
]
{ #category : 'instance creation' }
DateAndTime class >> readSeparateDateAndTimeFrom: stream [
"Read a separate Date and Time from stream to instanciate the receiver.
See also #printSeparateDateAndTimeOn:"
| date time |
stream skipSeparators.
date := (DateParser readingFrom: stream pattern: 'yyyy-mm-dd')
continueAfterParsing;
parse.
stream skipSeparators.
time := Time readFrom: stream.
^ self
date: date
time: time
]
{ #category : 'instance creation' }
DateAndTime class >> readTimezoneOffsetFrom: stream [
"Read and return an optional timezone offset in the form of
[+|-]hh[[separator]mm[[separator]ss]] or Z from stream as a duration.
If there is no offset, return the local offset."
^self readTimezoneOffsetFrom: stream default: self localOffset
]
{ #category : 'instance creation' }
DateAndTime class >> readTimezoneOffsetFrom: stream default: defaultOffset [
"Read and return an optional timezone offset in the form of
[+|-]hh[[separator]mm[[separator]ss]] or Z from stream as a duration.
If there is no offset, return the defaultOffset."
| sign hour minute second |
(stream peekFor: $Z) ifTrue: [ ^ Duration zero ].
hour := minute := second := 0.
^ ('+-' includes: stream peek)
ifTrue: [
sign := stream next = $- ifTrue: [ -1 ] ifFalse: [ 1 ].
hour := self readTwoDigitIntegerFrom: stream.
(self readOptionalSeparatorFrom: stream)
ifNotNil: [
minute := self readTwoDigitIntegerFrom: stream.
(self readOptionalSeparatorFrom: stream)
ifNotNil: [
second := Integer readFrom: stream ] ].
Duration seconds: sign * ((hour * 3600) + (minute * 60) + second) ]
ifFalse: [ defaultOffset ]
]
{ #category : 'instance creation' }
DateAndTime class >> readTwoDigitIntegerFrom: stream [
"Parse and return a decimal number of 2 digits from stream.
Fail if that is not possible"
| integer |
integer := 0.
2 timesRepeat: [ | char |
char := stream next.
('0123456789' includes: char) ifFalse: [ self error: 'Decimal digit expected' ].
integer := (integer * 10) + char digitValue ].
^ integer
]
{ #category : 'instance creation' }
DateAndTime class >> today [
^ self midnight
]
{ #category : 'instance creation' }
DateAndTime class >> tomorrow [
^ self today asDate next asDateAndTime
]
{ #category : 'instance creation queries' }
DateAndTime class >> unixEpoch [
"Answer a DateAndTime representing the Unix epoch (1 January 1970, midnight UTC)"
^ self basicNew
ticks: #(2440588 0 0) offset: Duration zero;
yourself
]
{ #category : 'instance creation' }
DateAndTime class >> year: year day: dayOfYear [
"Return a DateAndTime"
^ self
year: year
day: dayOfYear
hour: 0
minute: 0
second: 0
]
{ #category : 'instance creation' }
DateAndTime class >> year: year day: dayOfYear hour: hour minute: minute second: second [
^ self
year: year
day: dayOfYear
hour: hour
minute: minute
second: second
offset: self localOffset
]
{ #category : 'instance creation' }
DateAndTime class >> year: year day: dayOfYear hour: hour minute: minute second: second offset: offset [
"Return a DataAndTime"
| y d |
y := self
year: year
month: 1
day: 1
hour: hour
minute: minute
second: second
nanoSecond: 0
offset: offset.
d := Duration days: (dayOfYear - 1).
^ y + d
]
{ #category : 'clock provider' }
DateAndTime class >> year: aYearInteger month: aMonthNumber [
"Return a date starting the first day of the month"
^ self
year: aYearInteger
month: aMonthNumber
day: 1
hour: 0
minute: 0
]
{ #category : 'instance creation' }
DateAndTime class >> year: year month: month day: day [
"Return a DateAndTime, midnight local time"
^ self
year: year
month: month
day: day
hour: 0
minute: 0
]
{ #category : 'instance creation' }
DateAndTime class >> year: year month: month day: day hour: hour minute: minute [
"Return a DateAndTime"
^ self
year: year
month: month
day: day
hour: hour
minute: minute
second: 0
]
{ #category : 'instance creation' }
DateAndTime class >> year: year month: month day: day hour: hour minute: minute offset: anOffset [
"Return a DateAndTime"
^ self
year: year
month: month
day: day
hour: hour
minute: minute
second: 0
offset: anOffset
]
{ #category : 'instance creation' }
DateAndTime class >> year: year month: month day: day hour: hour minute: minute second: second [
"Return a DateAndTime"
^ self
year: year
month: month
day: day
hour: hour
minute: minute
second: second
offset: self localOffset
]
{ #category : 'instance creation' }
DateAndTime class >> year: year month: month day: day hour: hour minute: minute second: second nanoSecond: nanoCount offset: utcOffset [
"Return a DateAndTime with the values in the given TimeZone (UTCOffset)"
| monthIndex daysInMonth p q r s julianDayNumber localSeconds utcSeconds|
monthIndex := month isInteger ifTrue: [ month ] ifFalse: [ Month indexOfMonth: month ].
(monthIndex between: 1 and: 12) ifFalse: [ DateError signal: 'There is no ', monthIndex printString, 'th month' ].
daysInMonth := Month
daysInMonth: monthIndex
forYear: year.
day < 1 ifTrue: [ DateError signal: 'day may not be zero or negative' ].
day > daysInMonth ifTrue: [ DateError signal: 'day is after month ends' ].
p := (monthIndex - 14) quo: 12.
q := year + 4800 + p.
r := monthIndex - 2 - (12 * p).
s := (year + 4900 + p) quo: 100.
julianDayNumber :=
((1461 * q) quo: 4) +
((367 * r) quo: 12) -
((3 * s) quo: 4) +
(day - 32075).
localSeconds := hour * 60 + minute * 60 + second.
utcSeconds := localSeconds - utcOffset asSeconds.
^self basicNew
setJdn: julianDayNumber
seconds: utcSeconds
nano: nanoCount
offset: utcOffset;
yourself
]
{ #category : 'instance creation' }
DateAndTime class >> year: year month: month day: day hour: hour minute: minute second: second offset: offset [
^ self
year: year
month: month
day: day
hour: hour
minute: minute
second: second
nanoSecond: 0
offset: offset
]
{ #category : 'instance creation' }
DateAndTime class >> year: year month: month day: day offset: anOffset [
"Return a DateAndTime, midnight in the timezone with the given offset"
^ self
year: year
month: month
day: day
hour: 0
minute: 0
offset: anOffset
]
{ #category : 'instance creation' }
DateAndTime class >> yesterday [
^ self today asDate previous asDateAndTime
]
{ #category : 'arithmetic' }
DateAndTime >> + operand [
"operand conforms to protocol Duration"
| durationTicks |
durationTicks := operand asDuration ticks.
^ self class basicNew
setJdn: julianDayNumber + durationTicks first
seconds: seconds + durationTicks second
nano: nanos + durationTicks third
offset: self offset;
yourself
]
{ #category : 'arithmetic' }
DateAndTime >> - operand [
"operand conforms to protocol DateAndTime or protocol Duration"
^ (operand respondsTo: #asDateAndTime)
ifTrue: [
| other |
other := operand asDateAndTime.
Duration
seconds: (SecondsInDay * (julianDayNumber - other julianDayNumberUTC))
+ (seconds - other secondsSinceMidnightUTC)
nanoSeconds: nanos - other nanoSecond ]
ifFalse: [ self + operand negated ]
]
{ #category : 'arithmetic' }
DateAndTime >> < comparand [
"comparand conforms to protocol DateAndTime,
or can be converted into something that conforms."
| other |
other := comparand asDateAndTime.
^ julianDayNumber = other julianDayNumberUTC
ifTrue: [
seconds = other secondsSinceMidnightUTC
ifTrue: [ nanos < other nanoSecond ]
ifFalse: [ seconds < other secondsSinceMidnightUTC ] ]
ifFalse: [ julianDayNumber < other julianDayNumberUTC ]
]
{ #category : 'comparing' }
DateAndTime >> = other [
self == other ifTrue: [ ^ true ].
(self species = other species) ifFalse: [ ^ false ].
^ self hasEqualTicks: other
]
{ #category : 'converting' }
DateAndTime >> asDate [
"Convert the receiver in a date object."
"(DateAndTime fromString: '2019-08-17T13:33:00+02:00') asDate printString >>> (Date newDay: 17 month: 8 year: 2019) printString"
^ Date starting: self
]
{ #category : 'converting' }
DateAndTime >> asDateAndTime [
^ self
]
{ #category : 'converting' }
DateAndTime >> asDosTimestamp [
^ (DosTimestamp fromDateAndTime: self) value
]
{ #category : 'converting' }
DateAndTime >> asDuration [
"Answer the duration since midnight."
^ Duration seconds: self secondsSinceMidnightLocalTime nanoSeconds: nanos
]
{ #category : 'converting' }
DateAndTime >> asFileNameCompatibleString [
| stream |
stream := String new writeStream.
self printYMDOn: stream.
stream << '.'.
self printHMSWithDashesOn: stream.
^ stream contents
]
{ #category : 'converting' }
DateAndTime >> asLocal [
^ (self offset = self class localOffset)
ifTrue: [self]
ifFalse: [self offset: self class localOffset]
]
{ #category : 'converting' }
DateAndTime >> asMonth [
^ Month starting: self
]
{ #category : 'converting' }
DateAndTime >> asNanoSeconds [
"Answer the number of nanoseconds since midnight"
^ self asDuration asNanoSeconds
]
{ #category : 'converting' }
DateAndTime >> asSeconds [
"Return the number of seconds since the epoch"
^ (self - (self class epoch)) asSeconds
]
{ #category : 'converting' }
DateAndTime >> asTime [
^ Time seconds: self secondsSinceMidnightLocalTime nanoSeconds: nanos
]
{ #category : 'converting' }
DateAndTime >> asTimeUTC [
^ Time seconds: self secondsSinceMidnightUTC nanoSeconds: nanos
]
{ #category : 'converting' }
DateAndTime >> asUTC [
^ offset isZero
ifTrue: [ self ]
ifFalse: [ self offset: 0 ]
]
{ #category : 'converting' }
DateAndTime >> asUnixTime [
"answer number of seconds since unix epoch (midnight Jan 1, 1970, UTC)"
^((self offset: Duration zero) - self class unixEpoch) asSeconds
]
{ #category : 'converting' }
DateAndTime >> asWeek [
^ Week starting: self
]
{ #category : 'converting' }
DateAndTime >> asYear [
^ Year starting: self
]
{ #category : 'accessing' }
DateAndTime >> day [
^ self dayOfYear
]
{ #category : 'accessing' }
DateAndTime >> dayMonthYearDo: aBlock [
"Return the value of executing block with the Gregorian Calender day, month and year as arguments,
as computed from my Julian Day Number, julianDayNumber.
See http://en.wikipedia.org/wiki/Julian_date#Gregorian_calendar_from_Julian_day_number
A short Description for the Constants used below:
- 400 years span 146097 days in gregorian calendar.
- 100 years span 36524 days, except every 400 years.
- 4 years span 1461 days, except every 100 years.
- 1 year spans 365 days, except every four years
"
| l n i j monthDay month fullYear |
l := self julianDayNumber + 68569.
n := 4 * l // 146097.
l := l - (146097 * n + 3 // 4).
i := 4000 * (l + 1) // 1461001.
l := l - (1461 * i // 4) + 31.
j := 80 * l // 2447.
monthDay := l - (2447 * j // 80).
l := j // 11.
month := j + 2 - (12 * l).
fullYear := 100 * (n - 49) + i + l.
^ aBlock
value: monthDay
value: month
value: fullYear
]
{ #category : 'accessing' }
DateAndTime >> dayOfMonth [
"Answer which day of the month is represented by the receiver."
^ self dayMonthYearDo: [ :d :m :y | d ]
]
{ #category : 'accessing' }
DateAndTime >> dayOfWeek [
"Sunday=1, ... , Saturday=7"
^ (self julianDayNumber + 1 rem: 7) + 1
]
{ #category : 'accessing' }
DateAndTime >> dayOfWeekAbbreviation [
^ self dayOfWeekName copyFrom: 1 to: 3
]
{ #category : 'accessing' }
DateAndTime >> dayOfWeekName [
^ Week nameOfDay: self dayOfWeek
]
{ #category : 'accessing' }
DateAndTime >> dayOfYear [
"This code was contributed by Dan Ingalls. It is equivalent to the terser
^ jdn - (Year year: self year) start julianDayNumber + 1 but much quicker."
^ self dayMonthYearDo:
[ :d :m :y | | monthStart |
monthStart := #(1 32 60 91 121 152 182 213 244 274 305 335) at: m.
(m > 2 and: [ Year isLeapYear: y ])
ifTrue: [ monthStart + d ]
ifFalse: [ monthStart + d - 1 ]]
]
{ #category : 'accessing' }
DateAndTime >> daysInMonth [
"Answer the number of days in the month represented by the receiver."
^ self asMonth daysInMonth
]
{ #category : 'accessing' }
DateAndTime >> daysInYear [
"Answer the number of days in the year represented by the receiver."
^ self asYear daysInYear
]
{ #category : 'accessing' }
DateAndTime >> daysLeftInYear [
"Answer the number of days in the year after the date of the receiver."
^ self daysInYear - self dayOfYear
]
{ #category : 'accessing' }
DateAndTime >> duration [
^ Duration zero
]
{ #category : 'accessing' }
DateAndTime >> firstDayOfMonth [
^ self asMonth start day
]
{ #category : 'private' }
DateAndTime >> hasEqualTicks: aDateAndTime [
^ (self julianDayNumberUTC = aDateAndTime julianDayNumberUTC)
and: [ (seconds = aDateAndTime secondsSinceMidnightUTC)
and: [ nanos = aDateAndTime nanoSecond ] ]
]
{ #category : 'comparing' }
DateAndTime >> hash [
^ (julianDayNumber hashMultiply bitXor: seconds) bitXor: nanos
]
{ #category : 'accessing' }
DateAndTime >> hour [
^ self hour24
]
{ #category : 'accessing' }
DateAndTime >> hour12 [
"Answer an <integer> between 1 and 12, inclusive, representing the hour
of the day in the 12-hour clock of the local time of the receiver."
^ self hour24 - 1 \\ 12 + 1
]
{ #category : 'accessing' }
DateAndTime >> hour24 [
"Answer a number that represents the number of complete hours in the receiver's time part,
after the number of complete days has been removed."
^ self localSeconds // SecondsInHour \\ 24
]
{ #category : 'accessing' }
DateAndTime >> hours [
^ self hour
]
{ #category : 'accessing' }
DateAndTime >> isLeapYear [
^ Year isLeapYear: self year
]
{ #category : 'accessing' }
DateAndTime >> julianDayNumber [
^ julianDayNumber + self julianDayOffset
]
{ #category : 'accessing' }
DateAndTime >> julianDayNumberUTC [
^ julianDayNumber
]
{ #category : 'accessing' }
DateAndTime >> julianDayOffset [
"Return the offset in julian days possibly introduced by the timezone offset"
^ ((seconds + self offset asSeconds) / SecondsInDay) floor
]
{ #category : 'accessing' }
DateAndTime >> localSeconds [
" Return the seconds since the epoch in local time."
^ seconds + self offset asSeconds
]
{ #category : 'accessing' }
DateAndTime >> meridianAbbreviation [
^ self asTime meridianAbbreviation
]
{ #category : 'accessing' }
DateAndTime >> middleOf: aDuration [
"Return a Timespan where the receiver is the middle of the Duration"
| duration |
duration := aDuration asDuration.
^ Timespan starting: (self - (duration / 2)) duration: duration
]
{ #category : 'accessing' }
DateAndTime >> midnight [
"Answer a DateAndTime starting at midnight (towards the end of the day) local time"
self dayMonthYearDo: [ :day :month :year |
^ self class
basicYear: year
month: month
day: day
hour: 0
minute: 0
second: 0
nanoSecond: 0
offset: offset ]
]
{ #category : 'accessing' }
DateAndTime >> minute [
"Answer a number that represents the number of complete minutes in the receiver' time part,
after the number of complete hours has been removed."
"(DateAndTime fromString: '2004-02-29T13:33:00+02:00') minute >>> 33"
^ self localSeconds // SecondsInMinute \\ 60
]
{ #category : 'accessing' }
DateAndTime >> minutes [
^ self minute
]
{ #category : 'accessing' }
DateAndTime >> month [
^ self dayMonthYearDo: [ :d :m :y | m ]
]
{ #category : 'accessing' }
DateAndTime >> monthAbbreviation [
^ self monthName copyFrom: 1 to: 3
]
{ #category : 'accessing' }
DateAndTime >> monthIndex [