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.
15 Object subclass: Date | secondsInDay monthNames firstDayOfMonth daysInMonth weekDayNames | [
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)
48 ds := dayName asSymbol.
49 (1 to: 7) do: [:x | ((weekDayNames at: x) == ds) ifTrue: [ ^x ]].
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 ]
65 obj unixTime: System unixTime.
69 ^newWithUnixTime: ut [
79 obj unixTime: aDate unixTime.
84 "assign the current date and time"
92 ^localTimeFor: aDateTime [
93 ^(System localTimeFor: aDateTime unixTime)
98 ^(System localTimeFor: (self unixTime))
102 ^(date * 86400) + time
111 (self class = aDate class) ifFalse: [ ^false ].
112 ^self unixTime = aDate unixTime
132 ^(self localTime) at: 4
136 ^(self localTime) at: 7
140 ^(Date weekDayNames) at: (self dayOfWeek + 1)
144 ^(self localTime) at: 8
148 ^(self localTime) at: 3
152 ^Date isLeapYear: (self year)
156 ^(self localTime) at: 2
160 ^(self localTime) at: 5
164 ^(Date monthNames) at: (self month)
168 ^(self localTime) at: 1
172 ^(self localTime) at: 6
176 "assign the current date and time to a Date instance"
177 self unixTime: System unixTime.
181 "Return the next date whose weekday name is dayName"
183 diff := (self class dayOfWeek: dayName) - self dayOfWeek.
185 ifTrue: [ diff := 7 ]
186 ifFalse: [ (diff < 0) ifTrue: [ diff := 7 + diff ]].
191 "Return the previous date whose weekday name is dayName"
192 ^self subtractDays:(self dayOfWeek - (self class dayOfWeek: dayName) - 1 / 7)
197 nDate := self class newFrom: self.
198 ^nDate date: date + days.
203 nDate := self class newFrom: self.
204 ^nDate date: date - days.
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)