/usr/share/gnu-smalltalk/kernel/AnsiDates.st is in gnu-smalltalk-common 3.2.5-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | "======================================================================
|
| DateTime and Duration Method Definitions
|
|
======================================================================"
"======================================================================
|
| Copyright 2000, 2001, 2002, 2008, 2009 Free Software Foundation, Inc.
| Written by Paolo Bonzini.
|
| This file is part of the GNU Smalltalk class library.
|
| The GNU Smalltalk class library is free software; you can redistribute it
| and/or modify it under the terms of the GNU Lesser General Public License
| as published by the Free Software Foundation; either version 2.1, or (at
| your option) any later version.
|
| The GNU Smalltalk class library is distributed in the hope that it will be
| useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
| General Public License for more details.
|
| You should have received a copy of the GNU Lesser General Public License
| along with the GNU Smalltalk class library; see the file COPYING.LIB.
| If not, write to the Free Software Foundation, 59 Temple Place - Suite
| 330, Boston, MA 02110-1301, USA.
|
======================================================================"
Date subclass: DateTime [
| seconds offset |
<category: 'Language-Data types'>
<comment: 'My instances represent timestamps.'>
ClockPrecision := nil.
DateTime class >> initialize [
"Initialize the receiver's class variables"
<category: 'information'>
ClockPrecision := Duration seconds: 1
]
DateTime class >> clockPrecision [
<category: 'information'>
^ClockPrecision
]
DateTime class >> fromSeconds: secs offset: ofs [
"Answer a DateTime denoting the given date and time (as seconds
since January 1, 1901 midnight). Set the offset field to ofs (a
Duration)."
<category: 'instance creation (non-ANSI)'>
^self fromDays: 0 seconds: secs offset: ofs
]
DateTime class >> fromSeconds: secs [
"Answer a DateTime denoting the given date and time (as seconds
since January 1, 1901 midnight UTC)."
<category: 'instance creation (non-ANSI)'>
^self fromDays: 0 seconds: secs offset: Duration zero
]
DateTime class >> fromDays: days seconds: secs [
"Answer a DateTime denoting the given date (as days since
January 1, 1901) and time (as seconds since UTC midnight)."
<category: 'instance creation (non-ANSI)'>
^self fromDays: days seconds: secs offset: Duration zero
]
DateTime class >> fromDays: days seconds: secs offset: ofs [
"Answer a DateTime denoting the given date (as days since
January 1, 1901) and time (as seconds since midnight). Set the
offset field to ofs (a Duration)."
<category: 'instance creation (non-ANSI)'>
^(self fromDays: days + (secs // 86400))
setSeconds: secs \\ 86400;
setOffset: ofs
]
DateTime class >> date: aDate time: aTime offset: ofs [
"Answer a DateTime denoting the given date and time. Set the
offset field to ofs (a Duration)."
<category: 'instance creation (non-ANSI)'>
^(self fromDays: aDate days)
setSeconds: aTime asSeconds;
setOffset: ofs
]
DateTime class >> date: aDate time: aTime [
"Answer a DateTime denoting the given date and time. Set the
offset field to ofs (a Duration)."
<category: 'instance creation (non-ANSI)'>
^(self fromDays: aDate days)
setSeconds: aTime asSeconds;
setOffset: Duration zero
]
DateTime class >> readFrom: aStream [
"Parse an instance of the receiver from aStream"
<category: 'instance creation'>
| date time ofs ch |
date := super readFrom: aStream.
(aStream peekFor: $T) ifFalse: [aStream skipSeparators].
time := (aStream atEnd or: [aStream peek isDigit])
ifTrue: [Duration readFrom: aStream]
ifFalse: [Duration zero].
aStream skipSeparators.
ch := aStream peek.
(ch = $+ or: [ch = $-]) ifFalse: [^date + time].
ofs := Duration readFrom: aStream.
^(date + time) setOffset: ofs
]
DateTime class >> today [
"Answer an instance of the receiver referring to midnight of today
in local time."
<category: 'instance creation'>
| t seconds secondsAtMidnight biasNow biasAtMidnight |
t := self now.
seconds := t asSeconds.
secondsAtMidnight := seconds - t seconds.
biasAtMidnight := Time timezoneBias: secondsAtMidnight.
^DateTime
fromSeconds: secondsAtMidnight
offset: (Duration fromSeconds: biasAtMidnight)
]
DateTime class >> now [
"Answer an instance of the receiver referring to the current
date and time."
<category: 'instance creation'>
^self dateAndTimeNow
]
DateTime class >> year: y month: m day: d hour: h minute: min second: s [
"Answer a DateTime denoting the d-th day of the given (as a number)
month and year, setting the time part to the given hour, minute,
and second"
<category: 'instance creation'>
^(super
year: y
month: m
day: d
hour: h
minute: min
second: s) setSeconds: (h * 60 + min) * 60 + s
]
DateTime class >> year: y day: d hour: h minute: min second: s [
"Answer a DateTime denoting the d-th day of the given year, and
setting the time part to the given hour, minute, and second"
<category: 'instance creation'>
^(super
year: y
day: d
hour: h
minute: min
second: s) setSeconds: (h * 60 + min) * 60 + s
]
DateTime class >> year: y month: m day: d hour: h minute: min second: s offset: ofs [
"Answer a DateTime denoting the d-th day of the given (as a number)
month and year. Set the offset field to ofs (a Duration), and
the the time part to the given hour, minute, and second"
<category: 'instance creation'>
^(super
year: y
month: m
day: d
hour: h
minute: min
second: s)
setSeconds: (h * 60 + min) * 60 + s;
setOffset: ofs
]
DateTime class >> year: y day: d hour: h minute: min second: s offset: ofs [
"Answer a DateTime denoting the d-th day of the given year.
Set the offset field to ofs (a Duration), and the time part
to the given hour, minute, and second"
<category: 'instance creation'>
^(super
year: y
day: d
hour: h
minute: min
second: s)
setSeconds: (h * 60 + min) * 60 + s;
setOffset: ofs
]
< aDateTime [
"Answer whether the receiver indicates a date preceding aDate"
<category: 'testing'>
self offset = aDateTime offset ifFalse: [^self asUTC < aDateTime asUTC].
^super < aDateTime
or: [super = aDateTime and: [seconds < aDateTime seconds]]
]
= aDateTime [
"Answer whether the receiver indicates the same date as aDate"
<category: 'testing'>
self class == aDateTime class ifFalse: [^false].
self offset = aDateTime offset ifFalse: [^self asUTC = aDateTime asUTC].
^super = aDateTime and: [seconds = aDateTime seconds]
]
hash [
"Answer an hash value for the receievr"
<category: 'testing'>
^super hash * 37 + (self seconds - self offset seconds)
]
+ aDuration [
"Answer a new Date pointing aDuration time past the receiver"
<category: 'basic'>
| newSecs |
newSecs := self seconds + (aDuration asSeconds rem: 86400).
^newSecs > 86400
ifTrue:
[DateTime
fromDays: self days + aDuration days + 1
seconds: newSecs - 86400
offset: self offset]
ifFalse:
[DateTime
fromDays: self days + aDuration days
seconds: newSecs
offset: self offset]
]
- aDateTimeOrDuration [
"Answer a new Date pointing dayCount before the receiver"
<category: 'basic'>
| newSecs resultClass |
aDateTimeOrDuration class == self class
ifTrue:
[self offset = aDateTimeOrDuration offset
ifFalse: [^self asUTC - aDateTimeOrDuration asUTC].
resultClass := Duration.
newSecs := self seconds - aDateTimeOrDuration seconds]
ifFalse:
[resultClass := DateTime.
newSecs := self seconds - (aDateTimeOrDuration asSeconds rem: 86400)].
^newSecs < 0
ifTrue:
[resultClass
fromDays: self days - aDateTimeOrDuration days - 1
seconds: newSecs + 86400
offset: self offset]
ifFalse:
[resultClass
fromDays: self days - aDateTimeOrDuration days
seconds: newSecs
offset: self offset]
]
asSeconds [
"Answer the date as the number of seconds from 1/1/1901."
<category: 'computations'>
^super asSeconds + seconds
]
dayOfWeek [
"Answer the day of week of the receiver. Unlike Dates, DateAndTimes
have 1 = Sunday, 7 = Saturday"
<category: 'computations'>
^#(2 3 4 5 6 7 1) at: super dayOfWeek
]
hour [
"Answer the hour in a 24-hour clock"
<category: 'computations'>
^seconds // 3600
]
hour12 [
"Answer the hour in a 12-hour clock"
<category: 'computations'>
| h |
h := self hour \\ 12.
^h = 0 ifTrue: [12] ifFalse: [h]
]
hour24 [
"Answer the hour in a 24-hour clock"
<category: 'computations'>
^self hour
]
meridianAbbreviation [
"Answer either #AM (for anti-meridian) or #PM (for post-meridian)"
<category: 'computations'>
^self hour < 12 ifTrue: [#AM] ifFalse: [#PM]
]
minute [
"Answer the minute"
<category: 'computations'>
^seconds // 60 \\ 60
]
second [
"Answer the month represented by the receiver"
<category: 'computations'>
^seconds \\ 60
]
at: anIndex [
"Since in the past timestamps were referred to as Arrays containing
a Date and a Time (in this order), this method provides access to
DateTime objects like if they were two-element Arrays."
<category: 'splitting in dates & times'>
anIndex = 1 ifTrue: [^self asDate].
anIndex = 2 ifTrue: [^self asTime].
SystemExceptions.IndexOutOfRange signalOn: self withIndex: anIndex
]
asDate [
"Answer a Date referring to the same day as the receiver"
<category: 'splitting in dates & times'>
^Date fromDays: self days
]
asTime [
"Answer a Time referring to the same time (from midnight) as the receiver"
<category: 'splitting in dates & times'>
^Time fromSeconds: seconds
]
asLocal [
"Answer the receiver, since DateTime objects store themselves
in Local time"
<category: 'time zones'>
| utcSecs offset |
utcSecs := self asSeconds - self offset asSeconds.
offset := Time timezoneBias: utcSecs.
^DateTime
fromSeconds: utcSecs + offset
offset: (Duration fromSeconds: offset)
]
asUTC [
"Convert the receiver to UTC time, and answer a new DateTime object."
<category: 'time zones'>
| newSecs |
self offset asSeconds = 0 ifTrue: [ ^self ].
newSecs := self seconds - self offset asSeconds.
^newSecs < 0
ifTrue:
[DateTime
fromDays: self days + offset days - 1
seconds: newSecs + 86400
offset: Duration zero]
ifFalse:
[DateTime
fromDays: self days + offset days
seconds: newSecs
offset: Duration zero]
]
offset [
"Answer the receiver's offset from UTC to local time (e.g. +3600 seconds
for Central Europe Time, -3600*6 seconds for Eastern Standard Time).
The offset is expressed as a Duration"
<category: 'time zones'>
^offset
]
offset: anOffset [
"Answer a copy of the receiver with the offset from UTC to local time
changed to anOffset (a Duration)."
<category: 'time zones'>
anOffset = offset ifTrue: [^self].
^(self copy)
setOffset: anOffset;
yourself
]
timeZoneAbbreviation [
"Answer an abbreviated indication of the receiver's offset, expressed
as `shhmm', where `hh' is the number of hours and `mm' is the number
of minutes between UTC and local time, and `s' can be `+' for the
Eastern hemisphere and `-' for the Western hemisphere."
<category: 'time zones'>
^String
with: (self offset positive ifTrue: [$+] ifFalse: [$-])
with: (self offset hour // 10) digitValue
with: (self offset hour \\ 10) digitValue
with: (self offset minute // 10) digitValue
with: (self offset minute \\ 10) digitValue
]
timeZoneName [
"Answer the time zone name for the receiver (currently, it is
simply `GMT +xxxx', where `xxxx' is the receiver's
#timeZoneAbbreviation)."
<category: 'time zones'>
^'GMT ' , self timeZoneAbbreviation
]
printOn: aStream [
"Print a representation for the receiver on aStream"
<category: 'printing'>
aStream
nextPut: (self year < 0 ifTrue: [$-] ifFalse: [Character space]);
next: 3 - (self year abs log: 10) floor put: $0;
print: self year abs;
nextPut: $-;
next: (self month < 10 ifTrue: [1] ifFalse: [0]) put: $0;
print: self month;
nextPut: $-;
next: (self day < 10 ifTrue: [1] ifFalse: [0]) put: $0;
print: self day;
nextPut: $T;
next: (self hour < 10 ifTrue: [1] ifFalse: [0]) put: $0;
print: self hour;
nextPut: $:;
next: (self minute < 10 ifTrue: [1] ifFalse: [0]) put: $0;
print: self minute;
nextPut: $:;
next: (self second < 10 ifTrue: [1] ifFalse: [0]) put: $0;
print: self second;
nextPut: (self offset negative ifTrue: [$-] ifFalse: [$+]);
next: (self offset hours abs < 10 ifTrue: [1] ifFalse: [0]) put: $0;
print: self offset hours abs;
nextPut: $:;
next: (self offset minutes abs < 10 ifTrue: [1] ifFalse: [0]) put: $0;
print: self offset minutes abs.
self offset seconds = 0 ifTrue: [^self].
aStream
nextPut: $:;
print: self offset seconds
]
storeOn: aStream [
"Store on aStream Smalltalk code compiling to the receiver"
<category: 'storing'>
aStream
nextPut: $(;
nextPutAll: self class storeString;
nextPutAll: ' year: ';
store: self year;
nextPutAll: ' month: ';
store: self month;
nextPutAll: ' day: ';
store: self day;
nextPutAll: ' hour: ';
store: self hour;
nextPutAll: ' minute: ';
store: self minute;
nextPutAll: ' second: ';
store: self second.
self offset = Duration zero ifFalse: [
aStream
nextPutAll: ' offset: ';
store: self offset ].
aStream
nextPut: $)
]
setDay: dayOfMonth monthIndex: monthIndex year: yearInteger [
"Private - Set the receiver to the given date parts"
<category: 'private'>
seconds := 0.
offset := Duration zero.
^super
setDay: dayOfMonth
monthIndex: monthIndex
year: yearInteger
]
setDays: dayCount [
"Private - Compute the date parts from the given dayCount and initialize
the receiver"
<category: 'private'>
seconds := 0.
offset := Duration zero.
^super setDays: dayCount
]
seconds [
<category: 'private'>
^seconds
]
setSeconds: secondsCount [
<category: 'private'>
seconds := secondsCount
]
setOffset: offsetDuration [
<category: 'private'>
offset := offsetDuration
]
]
Time subclass: Duration [
<category: 'Language-Data types'>
<comment: 'My instances represent differences between timestamps.'>
Zero := nil.
Duration class >> fromDays: days seconds: secs offset: unused [
"Answer a duration of `d' days and `secs' seconds. The last
parameter is unused; this message is available for interoperability
with the DateTime class."
<category: 'instance creation (non ANSI)'>
^self fromSeconds: days * 86400 + secs
]
Duration class >> milliseconds: msec [
"Answer a duration of `msec' milliseconds"
<category: 'instance creation'>
^self fromSeconds: msec / 1000
]
Duration class >> weeks: w [
"Answer a duration of `w' weeks"
<category: 'instance creation'>
^self fromSeconds: w * ##(86400 * 7)
]
Duration class >> days: d [
"Answer a duration of `d' days"
<category: 'instance creation'>
^self fromSeconds: d * 86400
]
Duration class >> days: d hours: h minutes: m seconds: s [
"Answer a duration of `d' days and the given number of hours,
minutes, and seconds."
<category: 'instance creation'>
^self fromSeconds: ((d * 24 + h) * 60 + m) * 60 + s
]
Duration class >> readFrom: aStream [
"Parse an instance of the receiver (hours/minutes/seconds) from
aStream"
<category: 'instance creation'>
| sign sec hms i ch ws |
hms := {0. 0. 0}.
sign := (aStream peekFor: $-)
ifTrue: [-1]
ifFalse: [aStream peekFor: $+. 1].
i := 1.
ch := $:.
[aStream atEnd not and: [ch isSeparator not and: [
ch ~= $+ and: [ch ~= $- and: [
i > 1 ifTrue: [aStream next].
i <= 4 and: [(ch := aStream peek) isDigit]]]]]] whileTrue: [
ws := WriteStream on: (String new: 10).
[ws nextPut: aStream next.
aStream atEnd not and: [(ch := aStream peek) isDigit]] whileTrue.
i = 4
ifTrue: [
hms := {
(hms at: 1) * 24 + (hms at: 2).
hms at: 3.
ws contents asNumber}]
ifFalse: [
hms at: i put: ws contents asNumber].
i := i + 1].
sec := ((hms at: 1) * 3600 + ((hms at: 2) * 60) + (hms at: 3)) * sign.
^self fromSeconds: sec
]
Duration class >> initialize [
"Initialize the receiver's instance variables"
<category: 'instance creation'>
Zero := self new
]
Duration class >> zero [
"Answer a duration of zero seconds."
<category: 'instance creation'>
^Zero
]
* factor [
"Answer a Duration that is `factor' times longer than the receiver"
<category: 'arithmetics'>
^Duration fromSeconds: self asSeconds * factor
]
/ factorOrDuration [
"If the parameter is a Duration, answer the ratio between the receiver
and factorOrDuration. Else divide the receiver by factorOrDuration (a
Number) and answer a new Duration that is correspondingly shorter."
<category: 'arithmetics'>
^factorOrDuration isNumber
ifFalse: [self asSeconds / factorOrDuration asSeconds]
ifTrue: [Duration fromSeconds: self asSeconds / factorOrDuration]
]
+ aDuration [
"Answer a Duration that is the sum of the receiver and aDuration's
lengths."
<category: 'arithmetics'>
^Duration fromSeconds: self asSeconds + aDuration asSeconds
]
- aDuration [
"Answer a Duration that is the difference of the receiver and aDuration's
lengths."
<category: 'arithmetics'>
^Duration fromSeconds: self asSeconds - aDuration asSeconds
]
isZero [
"Answer whether the receiver correspond to a duration of zero seconds."
<category: 'arithmetics'>
^self asSeconds = 0
]
abs [
"Answer a Duration that is as long as the receiver, but always in
the future."
<category: 'arithmetics'>
^Duration fromSeconds: self asSeconds abs
]
days [
"Answer the number of days in the receiver"
<category: 'arithmetics'>
^self asSeconds quo: 86400
]
negated [
"Answer a Duration that is as long as the receiver, but with past and
future exchanged."
<category: 'arithmetics'>
^Duration fromSeconds: self asSeconds negated
]
storeOn: aStream [
"Store on aStream Smalltalk code compiling to the receiver"
<category: 'storing'>
aStream
nextPut: $(;
nextPutAll: self class storeString;
nextPutAll: ' days: ';
store: self days;
nextPutAll: ' hours: ';
store: self hours;
nextPutAll: ' minutes: ';
store: self minutes;
nextPutAll: ' seconds: ';
store: self seconds;
nextPut: $)
]
negative [
"Answer whether the receiver is in the past."
<category: 'arithmetics'>
^self asSeconds < 0
]
positive [
"Answer whether the receiver is a zero-second duration or is
in the future."
<category: 'arithmetics'>
^self asSeconds >= 0
]
printOn: aStream [
"Print a represention of the receiver on aStream."
<category: 'arithmetics'>
self negative
ifTrue:
[aStream
nextPut: $-;
print: self negated.
^self].
aStream
print: self days;
nextPut: $:;
next: (self hours < 10 ifTrue: [1] ifFalse: [0]) put: $0;
print: self hours;
nextPut: $:;
next: (self minutes < 10 ifTrue: [1] ifFalse: [0]) put: $0;
print: self minutes;
nextPut: $:;
next: (self seconds < 10 ifTrue: [1] ifFalse: [0]) put: $0;
print: self seconds
]
setSeconds: secs [
<category: 'private'>
seconds := secs
]
wait [
"Answer a Delay waiting for the amount of time represented
by the receiver and start waiting on it."
<category: 'processes'>
^(Delay forMilliseconds: self asSeconds * 1000) wait
]
]
Eval [
Duration initialize
]
|