X11 sample rewritten; now it works again
[k8lst.git] / modules / date.st
blobbcf230e9e152e81be202e41f086104dcbcda5100
2  coded by Ketmar // Vampire Avalon (psyc://ketmar.no-ip.org/~Ketmar)
3  Understanding is not required. Only obedience.
5  This program is free software. It comes without any warranty, to
6  the extent permitted by applicable law. You can redistribute it
7  and/or modify it under the terms of the Do What The Fuck You Want
8  To Public License, Version 2, as published by Sam Hocevar. See
9  http://sam.zoy.org/wtfpl/COPYING for more details.
11 Package [
12   DateTime
15 Object subclass: Date | secondsInDay monthNames firstDayOfMonth daysInMonth weekDayNames | [
16 | date time |
17 ^initialize [
18   "Initialize class variables with common date stuff"
19   daysInMonth := #(31 28 31 30 31 30 31 31 30 31 30 31).
20   firstDayOfMonth := #(1 32 60 91 121 152 182 213 244 274 305 335).
21   monthNames := #(January February March April May June July August September October November December).
22   secondsInDay := 24 * 60 * 60.
23   weekDayNames := #(Sunday Monday Tuesday Wednesday Thursday Friday Saturday)
26 ^daysInMonth [
27   ^daysInMonth
30 ^firstDayOfMonth [
31   ^firstDayOfMonth
34 ^monthNames [
35   ^monthNames
38 ^secondsInDay [
39   ^secondsInDay
42 ^weekDayNames [
43   ^weekDayNames
46 ^dayOfWeek: dayName [
47   | ds |
48   ds := dayName asSymbol.
49   (1 to: 7) do: [:x | ((weekDayNames at: x) == ds) ifTrue: [ ^x ]].
50   ^nil
53 ^isLeapYear: aYear [
54   "return true if aYear is a leap year"
55   (aYear / 4 = 0) ifTrue: [
56     (aYear / 100 ~= 0) ifTrue: [ ^true ].
57     (aYear / 400 = 0) ifTrue: [ ^true ]
58   ].
59   ^false
62 ^new [
63   | obj |
64   obj := super new.
65   obj unixTime: System unixTime.
66   ^obj
69 ^newWithUnixTime: ut [
70   | obj |
71   obj := super new.
72   obj unixTime: ut.
73   ^obj
76 ^newFrom: aDate [
77   | obj |
78   obj := super new.
79   obj unixTime: aDate unixTime.
80   ^obj
83 ^now [
84   "assign the current date and time"
85   ^self new
88 ^today [
89   ^self now
92 ^localTimeFor: aDateTime [
93   ^(System localTimeFor: aDateTime unixTime)
97 localTime [
98   ^(System localTimeFor: (self unixTime))
101 unixTime [
102   ^(date * 86400) + time
105 unixTime: ut [
106   date := ut / 86400.
107   time := ut % 86400.
110 = aDate [
111   (self class = aDate class) ifFalse: [ ^false ].
112   ^self unixTime = aDate unixTime
115 date [
116   ^date
119 date: aDate [
120   date := aDate
123 time [
124   ^time
127 time: aTime [
128   time := aTime
131 day [
132   ^(self localTime) at: 4
135 dayOfWeek [
136   ^(self localTime) at: 7
139 dayOfWeekName [
140   ^(Date weekDayNames) at: (self dayOfWeek + 1)
143 dayOfYear [
144   ^(self localTime) at: 8
147 hours [
148   ^(self localTime) at: 3
151 isLeapYear [
152   ^Date isLeapYear: (self year)
155 minutes [
156   ^(self localTime) at: 2
159 month [
160   ^(self localTime) at: 5
163 monthName [
164   ^(Date monthNames) at: (self month)
167 seconds [
168   ^(self localTime) at: 1
171 year [
172   ^(self localTime) at: 6
175 toNow [
176   "assign the current date and time to a Date instance"
177   self unixTime: System unixTime.
180 next: dayName [
181   "Return the next date whose weekday name is dayName"
182   | diff |
183   diff := (self class dayOfWeek: dayName) - self dayOfWeek.
184   diff = 0
185     ifTrue: [ diff := 7 ]
186     ifFalse: [ (diff < 0) ifTrue: [ diff := 7 + diff ]].
187   ^self addDays: diff
190 previous: dayName [
191   "Return the previous date whose weekday name is dayName"
192   ^self subtractDays:(self dayOfWeek - (self class dayOfWeek: dayName) - 1 / 7)
195 addDays: days [
196   | nDate |
197   nDate := self class newFrom: self.
198   ^nDate date: date + days.
201 subtractDays: days [
202   | nDate |
203   nDate := self class newFrom: self.
204   ^nDate date: date - days.
207 printString [
208   ^(self year asString) + '/' +
209   (self month printWidth: 2) + '/' +
210   (self day printWidth: 2) + ' ' +
211   (self hours printWidth: 2) + ':' +
212   (self minutes printWidth: 2) + ':' +
213   (self seconds printWidth: 2)
218 { Date initialize }