Skip to content

Commit 1bb7a0c

Browse files
committed
Initial checkin from private development repository
0 parents  commit 1bb7a0c

File tree

112 files changed

+891
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+891
-0
lines changed

README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# ZTimestamp
2+
3+
I am ZTimestamp.
4+
5+
6+
I am a Magnitude.
7+
8+
9+
I represent a point in time, a combination of a date and a time.
10+
11+
12+
I am an alternative for DateAndTime and TimeStamp.
13+
I have second precision and live in the UTC/GMT/Zulu timezone.
14+
I use ISO/International conventions and protocols only.
15+
I support some essential arithmetic.
16+
17+
I have an efficient internal representation:
18+
19+
20+
jnd - the julian day number <SmallInteger>
21+
secs - the number of seconds since midnight, the beginning of the day <SmallInteger>
22+
23+
24+
Examples:
25+
26+
27+
ZTimestamp now.
28+
ZTimestamp fromString: '1969-07-20T20:17:40Z'.
29+
30+
31+
There is some compatibility with existing, standard Chronology objects.
32+
I correctly parse representations with a timezone designator
33+
and can print a representation in arbitrary timezones.
34+
35+
36+
(ZTimestamp fromString: DateAndTime now truncated printString) localPrintString.
37+
38+
39+
Sven Van Caekenberghe, April, 2012. MIT Licensed.

license.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
MIT License
2+
3+
Copyright (C) 2012 Sven Van Caekenberghe
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10+

repository/.filetree

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "packageExtension" : ".package" }
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"noMethodMetaData" : true,
3+
"separateMethodMetaAndSource" : false,
4+
"useCypressPropertiesFile" : true }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*ztimestamp
2+
asZTimestamp
3+
^ ZTimestamp new
4+
jdn: jdn
5+
secs: seconds - offset asSeconds
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
"name" : "DateAndTime" }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
I am ZTimestamp.
2+
I am a Magnitude.
3+
I represent a point in time, a combination of a date and a time.
4+
5+
I am an alternative for DateAndTime and TimeStamp.
6+
I have second precision and live in the UTC/GMT/Zulu timezone.
7+
I use ISO/International conventions and protocols only.
8+
I support some essential arithmetic.
9+
10+
I have an efficient internal representation:
11+
12+
jnd - the julian day number <SmallInteger>
13+
secs - the number of seconds since midnight, the beginning of the day <SmallInteger>
14+
15+
Examples:
16+
17+
ZTimestamp now.
18+
ZTimestamp fromString: '1969-07-20T20:17:40Z'.
19+
20+
There is some compatibility with existing, standard Chronology objects.
21+
I correctly parse representations with a timezone designator
22+
and can print a representation in arbitrary timezones.
23+
24+
(ZTimestamp fromString: DateAndTime now truncated printString) localPrintString.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
private
2+
clockEpoch
3+
"Return the epoch of the clock that we use as an instance (cached), see #now"
4+
5+
^ ClockEpoch ifNil: [
6+
ClockEpoch := self year: 1901 month: 1 day: 1 ]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
private
2+
clockSeconds
3+
"Return the total seconds count of the clock that we use as an Integer, see #now"
4+
5+
^ Time totalSeconds
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
private
2+
clockTimezoneOffset
3+
"Return the timezone offset of the clock that we use as an Integer seconds (cached), see #now"
4+
5+
^ ClockTimezoneOffset ifNil: [
6+
ClockTimezoneOffset := DateAndTime localTimeZone offset asSeconds ]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
accessing
2+
epoch
3+
"Return the epoch of my instances, the point in time from which they start counting, as an instance"
4+
5+
^ self zero
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
private
2+
fromClockSeconds: seconds
3+
"Create and return a new instance from a specific number of seconds since our clock epoch, see #now"
4+
5+
^ self new
6+
jdn: self clockEpoch julianDayNumber + (seconds // 86400)
7+
secs: self clockEpoch secondsSinceMidnight + (seconds \\ 86400)
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
instance creation
2+
fromJulianDate: julianDate
3+
"Create and return a new ZTimestamp instance from the scientific Julian Date, a Float number of Julian Days"
4+
5+
^ self new
6+
jdn: julianDate truncated
7+
secs: (julianDate fractionPart * 86400) truncated
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ston
2+
fromSton: stonReader
3+
"Read our ISO representation"
4+
5+
^ self readFrom: stonReader parseListSingleton readStream
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
instance creation
2+
fromString: string
3+
"Parse and return an instance from a flexible ISO 8601 string representation, see #readFrom:"
4+
5+
^ self readFrom: string readStream
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
instance creation
2+
fromUnixTime: integer
3+
"Create and return a new ZTimestamp from the integer number of seconds since the Unix or POSIX epoch"
4+
5+
^ self unixEpoch + integer seconds
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
accessing
2+
jdnFromYear: year month: month day: day
3+
"Compute and return the Julian Day Number from the Gregorian Calender date described by year, month and day.
4+
See http://en.wikipedia.org/wiki/Julian_date#Converting_Gregorian_calendar_date_to_Julian_Day_Number"
5+
6+
| a y m |
7+
a := (14 - month) // 12.
8+
y := year + 4800 - a.
9+
m := month + (12 * a) - 3.
10+
^ day
11+
+ (((153 * m) + 2) // 5)
12+
+ (365 * y)
13+
+ (y // 4)
14+
- (y // 100)
15+
+ (y // 400)
16+
- 32045
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
accessing
2+
localTimeZone
3+
"Return the current machine's local timezone offset as a Duration"
4+
5+
^ DateAndTime localTimeZone
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
instance creation
2+
now
3+
"Create and return a new ZTimestamp equal to the current system time"
4+
5+
^ self fromClockSeconds: (self clockSeconds - self clockTimezoneOffset)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
instance creation
2+
readFrom: stream
3+
"Parse a ISO 8601 representation from stream, in the format
4+
year[-]month[-]day[T]hour[:]minute[:]second[[+|-]hh[:]mm]
5+
where the whole part after T as well as the TZ offset is optional.
6+
TZ offset can be Z, all separators can be any non digit"
7+
8+
| year month day hour minute second timestamp offset |
9+
year := self readInteger: 4 from: stream.
10+
self readOptionalSeparatorFrom: stream.
11+
month := self readInteger: 2 from: stream.
12+
self readOptionalSeparatorFrom: stream.
13+
day := self readInteger: 2 from: stream.
14+
(self readOptionalSeparatorFrom: stream)
15+
ifNotNil: [
16+
hour := self readInteger: 2 from: stream.
17+
self readOptionalSeparatorFrom: stream.
18+
minute := self readInteger: 2 from: stream.
19+
self readOptionalSeparatorFrom: stream.
20+
second := self readInteger: 2 from: stream.
21+
offset := self readTimezoneOffsetFrom: stream ]
22+
ifNil: [
23+
hour := minute := second := offset := 0 ].
24+
timestamp := self
25+
year: year
26+
month: month
27+
day: day
28+
hour: hour
29+
minute: minute
30+
second: second.
31+
^ offset = 0
32+
ifTrue: [ timestamp ]
33+
ifFalse: [ timestamp - offset seconds ]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
private
2+
readInteger: numberOfDigits from: stream
3+
"Parse and return a decimal number of numberOfDigits from stream.
4+
Fail if that is not possible"
5+
6+
| integer |
7+
integer := 0.
8+
numberOfDigits timesRepeat: [ | char |
9+
char := stream next.
10+
self assert: ('0123456789' includes: char).
11+
integer := (integer * 10) + char digitValue ].
12+
^ integer
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
private
2+
readOptionalSeparatorFrom: stream
3+
"Read an optional separator (non decimal digit) from stream and return it.
4+
Return nil if nothing was read"
5+
6+
^ (stream atEnd or: [ '0123456789' includes: stream peek])
7+
ifTrue: [ nil ]
8+
ifFalse: [ stream next ]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
private
2+
readTimezoneOffsetFrom: stream
3+
"Read and return an optional timezone offset in the form of
4+
[+|-]hh[separator]mm from stream as an Integer seconds.
5+
If there is no offset, just return 0"
6+
7+
| sign hour minute |
8+
stream atEnd ifTrue: [ ^ 0 ].
9+
stream peek = $Z ifTrue: [ stream next. ^ 0 ].
10+
^ ('+-' includes: stream peek)
11+
ifTrue: [
12+
sign := stream next = $- ifTrue: [ -1 ] ifFalse: [ 1 ].
13+
hour := self readInteger: 2 from: stream.
14+
(self readOptionalSeparatorFrom: stream)
15+
ifNil: [ minute := 0 ]
16+
ifNotNil: [ minute := self readInteger: 2 from: stream ].
17+
sign * (hour * 3600) + (minute * 60) ]
18+
ifFalse: [ 0 ]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
system startup
2+
startup
3+
"Clear the cached clock timezone offset"
4+
5+
ClockTimezoneOffset := nil
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
accessing
2+
unixEpoch
3+
"Return the Unix or POSIX epoch as a ZTimestamp"
4+
5+
^ self year: 1970 month: 1 day: 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
accessing
2+
withJdn: jdn dayMonthYearDo: block
3+
"Return the value of executing block with the Gregorian Calender day, month and year as arguments,
4+
as computed from my Julian Day Number, jdn.
5+
See http://en.wikipedia.org/wiki/Julian_date#Gregorian_calendar_from_Julian_day_number"
6+
7+
| j g dg c dc b db a da y m d |
8+
j := jdn + 32044.
9+
g := j // 146097.
10+
dg := j \\ 146097.
11+
c := ((dg // 36524) + 1) * 3 // 4.
12+
dc := dg - (c * 36524).
13+
b := dc // 1461.
14+
db := dc \\ 1461.
15+
a := ((db // 365) + 1) * 3 // 4.
16+
da := db - (a * 365).
17+
y := (g * 400) + (c * 100) + (b * 4) + a.
18+
m := ((((da * 5) + 308)) // 153) - 2.
19+
d := da - ((m + 4) * 153 // 5) + 122.
20+
^ block
21+
value: d + 1
22+
value: ((m + 2) \\ 12) + 1
23+
value: (y - 4800 + ((m * 2) // 12))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
instance creation
2+
year: year month: month day: day
3+
"Create and return a new ZTimestamp for the Gregorian calendar date described by year, month and date"
4+
5+
^ self new
6+
jdn: (self jdnFromYear: year month: month day: day)
7+
secs: 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
instance creation
2+
year: year month: month day: day hour: hour minute: minute second: second
3+
"Create and return a new ZTimestamp for the Gregorian calendar date described by year, month and date
4+
and the Civial time of day on that date described by hour, minute and second"
5+
6+
^ self new
7+
jdn: (self jdnFromYear: year month: month day: day)
8+
secs: (hour * 3600) + (minute * 60) + second
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
accessing
2+
zero
3+
"Return my zero instance, the point in time from which we start counting, see #epoch"
4+
5+
^ self new
6+
jdn: 0 secs: 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
testing
2+
= aZTimestamp
3+
"Answer true when aZTimestamp equals the receiver, false otherwise.
4+
Part of the Magnitude protocol"
5+
6+
self == aZTimestamp ifTrue: [ ^ true ].
7+
(aZTimestamp isKindOf: self class) ifFalse: [ ^ false ].
8+
^ aZTimestamp julianDayNumber = jdn
9+
and: [ aZTimestamp secondsSinceMidnight = secs ]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
testing
2+
< aZTimestamp
3+
"Answer true when aZTimestamp is strictly later than me, false otherwise.
4+
Part of the Magnitude protocol"
5+
6+
^ aZTimestamp julianDayNumber = jdn
7+
ifTrue: [ secs < aZTimestamp secondsSinceMidnight ]
8+
ifFalse: [ jdn < aZTimestamp julianDayNumber ]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
arithmetic
2+
- operand
3+
"For ZTimestamp compatible operands, return a new Duration equal to the difference between the reveiver and the operand.
4+
Else return a new ZTimestamp that is the result of subtracting the operand considered as a Duration from the receiver"
5+
6+
^ (operand respondsTo: #asZTimestamp)
7+
ifTrue: [ | timestamp |
8+
timestamp := operand asZTimestamp.
9+
Duration seconds:
10+
(jdn - timestamp julianDayNumber) * 86400
11+
+ (secs - timestamp secondsSinceMidnight) ]
12+
ifFalse: [ self + (operand negated) ]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
arithmetic
2+
+ operand
3+
"Return a new ZTimestamp that is the result of adding
4+
the operand considered as a Duration to the receiver"
5+
6+
^ self class new
7+
jdn: jdn
8+
secs: secs + operand asDuration asSeconds
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
converting
2+
asDate
3+
"Convert the receiver to a Date"
4+
5+
^ self asDateAndTime asDate

0 commit comments

Comments
 (0)