source: lu/LUDate.lua @ 1797

Revision 814, 9.1 KB checked in by rsz, 6 years ago (diff)

cleanup

Line 
1--------------------------------------------------------------------------------
2-- Title:               LUDate.lua
3-- Description:         Like a square peg in a round hole
4-- Author:              Raphaël Szwarc http://alt.textdrive.com/lua/
5-- Creation Date:       February 1, 2005
6-- Legal:               Copyright (C) 2005 Raphaël Szwarc
7--------------------------------------------------------------------------------
8
9-- import dependencies
10local LUObject = require( "LUObject" )
11local LUString = require( "LUString" )
12local math = require( "math" )
13local os = require( "os" )
14
15-- constant(s)
16local Second = 1
17local Minute = 60 * Second
18local Hour = 60 * Minute
19local Day = 24 * Hour
20local Week = 7 * Day
21local Month = 31 * Day
22local Year = 365 * Day
23local Durations = { Y = Year, M = Month, W = Week, D = Day, h = Hour, m = Minute, s = Second }
24
25-- define the class
26local super = LUObject
27local self = super()
28
29-- initialization method
30function self:init( aTime )
31        self = super.init( self )
32
33        self._time = aTime
34       
35        return self
36end
37
38-- method to access this date time
39function self:time()
40        if self._time == nil then
41                self._time = os.time()
42        end
43       
44        return self._time
45end
46
47-- method to access this date's date table
48function self:date()
49        if self._date == nil then
50                self._date = os.date( "*t", self:time() )
51        end
52
53        return self._date
54end
55
56-- method to compare the given object with this object
57function self:compare( anObject )
58        return self:time() < anObject:time()
59end
60
61 -- method for equality
62function self:equals( anObject )
63        if self:isKindOf( anObject ) == true then
64                if self:time() == anObject:time() then
65                        return true
66                end
67        end
68
69        return false
70end
71
72-- method for hashing
73function self:hashCode()
74        return self:time()
75end
76
77-- method to access this date year
78function self:year()
79        return self:date()[ "year" ]
80end
81
82-- method to access this date month
83function self:month()
84        return self:date()[ "month" ]
85end
86
87-- method to access this date week
88function self:week()
89        return tonumber( self:toString( "%U" ) )
90end
91
92-- method to access this month number of weeks
93function self:weeksInMonth()
94        return self:monthEndDate():week() - self:monthDate():week() + 1
95end
96
97-- method to access this date day
98function self:day()
99        return self:date()[ "day" ]
100end
101
102-- method to access this date day of year
103function self:dayOfYear()
104        return self:date()[ "yday" ]
105end
106
107-- method to access this date day of week
108function self:dayOfWeek()
109        return self:date()[ "wday" ]
110end
111
112-- method to access this date hour
113function self:hour()
114        return self:date()[ "hour" ]
115end
116
117-- method to access this date minute
118function self:minute()
119        return self:date()[ "min" ]
120end
121
122-- method to access this date second
123function self:second()
124        return self:date()[ "sec" ]
125end
126
127-- method to access this date day saving time
128function self:isDST()
129        return self:date()[ "isdst" ]
130end
131
132-- method to access this date UTC time
133function self:utc()
134        local anOffset = self:timeZoneOffset()
135       
136        if anOffset ~= nil then
137                return self:time() - anOffset
138        end
139       
140        return self:time()
141end
142
143-- method to access this date UTC date
144function self:utcDate()
145        return self:new( self:utc() )
146end
147
148-- method to access the day date
149function self:dayDate()
150        local someValues = { year = self:year(), month = self:month(), day = self:day(), hour = 0, min = 0, sec = 0 }
151       
152        return self:dateWithValues( someValues )
153end
154
155-- method to access the week date
156function self:weekDate()
157        local someValues = { year = self:year(), month = self:month(), day = self:day() - self:dayOfWeek() + 1, hour = 0, min = 0, sec = 0 }
158       
159        return self:dateWithValues( someValues )
160end
161
162-- method to access the month date
163function self:monthDate()
164        local someValues = { year = self:year(), month = self:month(), day = 1, hour = 0, min = 0, sec = 0 }
165       
166        return self:dateWithValues( someValues )
167end
168
169-- method to access the year date
170function self:yearDate()
171        local someValues = { year = self:year(), month = 1, day = 1, hour = 0, min = 0, sec = 0 }
172       
173        return self:dateWithValues( someValues )
174end
175
176-- method to access the end of the day date
177function self:dayEndDate()
178        local someValues = { year = self:year(), month = self:month(), day = self:day(), hour = 23, min = 59, sec = 59 }
179       
180        return self:dateWithValues( someValues )
181end
182
183-- method to access the end of the week date
184function self:weekEndDate()
185        local someValues = { year = self:year(), month = self:month(), day = self:day() + 7 - self:dayOfWeek(), hour = 23, min = 59, sec = 59 }
186       
187        return self:dateWithValues( someValues )
188end
189
190-- method to access the end of the month date
191function self:monthEndDate()
192        local someValues = { year = self:year(), month = self:month() + 1, day = 0, hour = 23, min = 59, sec = 59 }
193       
194        return self:dateWithValues( someValues )
195end
196
197-- method to access the end of the year date
198function self:yearEndDate()
199        local someValues = { year = self:year(), month = 12, day = 31, hour = 23, min = 59, sec = 59 }
200       
201        return self:dateWithValues( someValues )
202end
203
204-- method to add year, month, day, hour, minute and second to a date
205function self:add( aYear, aMonth, aDay, anHour, aMinute, aSecond )
206        local aYear = self:year() + ( aYear or 0 )
207        local aMonth = self:month() + ( aMonth or 0 )
208        local aDay = self:day() + ( aDay or 0 )
209        local anHour = self:hour() + ( anHour or 0 )
210        local aMinute = self:minute() + ( aMinute or 0 )
211        local aSecond = self:second() + ( aSecond or 0 )
212        local someValues = { year = aYear, month = aMonth, day = aDay, hour = anHour, min = aMinute, sec = aSecond }
213       
214        return self:dateWithValues( someValues )
215end
216
217-- method to compute an approximate interval between two dates
218function self:interval( aDate, aType )
219        local aDuration = Durations[ aType ] or Second
220        local anInterval = math.floor( math.abs( self:time() - aDate:time() ) / aDuration )
221       
222        if self > aDate then
223                anInterval = -anInterval
224        end
225       
226        return anInterval
227end
228
229-- as per "Frequently Asked Questions about Calendars Version 2.7"
230-- "2.16.1 Is there a formula for calculating the Julian day number?"
231-- http://www.tondering.dk/claus/cal/node3.html#SECTION003161000000000000000
232
233function self:julianDay()
234        local floor = math.floor
235        local a = floor( ( 14 - self:month() ) / 12 )
236        local y = self:year() + 4800 - a
237        local m = self:month() + ( 12 * a ) - 3
238        local aDay = self:day() + floor( ( ( 153 * m ) + 2 ) / 5 ) + ( 365 * y ) + floor( y / 4 ) - floor( y / 100 ) + floor( y / 400 ) - 32045
239       
240        return aDay
241end
242
243function self:dateWithJulianDay( aJulianDay )
244        local floor = math.floor
245        local a = aJulianDay + 32044
246        local b = floor( ( ( 4 * a ) + 3 ) / 146097 )
247        local c = a - floor( ( 146097 * b ) / 4 )
248        local d = floor( ( ( 4 * c ) + 3 ) / 1461 )
249        local e = c - floor( ( 1461 * d ) / 4 )
250        local m = floor( ( 5 * e + 2 ) / 153 )
251        local aDay = e - floor( ( 153 * m + 2 ) / 5 ) + 1
252        local aMonth = m + 3 - ( 12 * floor( m / 10 ) )
253        local aYear = ( 100 * b ) + d - 4800 + floor( m / 10 )
254        local someValues = { year = aYear, month = aMonth, day = aDay, hour = 0, min = 0, sec = 0 }
255
256        return self:dateWithValues( someValues )
257end
258
259-- method for string representation
260function self:toString( aFormat )
261        if aFormat == nil then
262                aFormat = "%Y-%m-%d %H:%M:%S"
263        end
264
265        return os.date( aFormat, self:time() )
266end
267
268-- method to create a date with the given values
269function self:dateWithValues( someValues )
270        return self:new( os.time( someValues ) )
271end
272
273-- method to access the local time zone
274function self:timeZone()
275        local aValue = os.date( "%Z", os.time() )
276       
277        if aValue == nil then
278                aValue = "UTC"
279        end
280       
281        return aValue
282end
283
284-- method to access the local time zone offset
285function self:timeZoneOffset()
286        local anOffset = os.date( "%z", os.time() )
287       
288        if anOffset ~= nil then
289                local anHour, aMinute = anOffset:match( "(%d%d)(%d%d)" )
290               
291                if anHour ~= nil and aMinute ~= nil then
292                        anHour = tonumber( anHour )
293                        aMinute = tonumber( aMinute )
294                       
295                        do
296                                local aValue = ( anHour * 60 * 60 ) + ( aMinute * 60 )
297                               
298                                if LUString:startsWith( anOffset, "-" ) == true then
299                                        aValue = 0 - aValue
300                                end
301
302                                return aValue
303                        end
304                end
305        end
306       
307        return 0
308end
309
310return self
Note: See TracBrowser for help on using the repository browser.