source: Include/Classes/System/DateTime.ab@ 93

Last change on this file since 93 was 93, checked in by OverTaker, 17 years ago

いくつかのメソッドを静的メンバに修正。その他、細かい修正。

File size: 11.3 KB
Line 
1' Classes/System/DateTime.ab
2
3#ifndef __SYSTEM_DATETIME_AB__
4#define __SYSTEM_DATETIME_AB__
5
6
7Class DateTime
8 m_Date As Int64
9Public
10 Static MaxValue = 3162240000000000000 As Int64 'Const
11 Static MinValue = 316224000000000 As Int64 'Const
12
13 Sub DateTime(ticks As Int64)
14 Ticks = ticks
15 Kind = Unspecified
16 End Sub
17
18 Sub DateTime(ticks As Int64, kind As DateTimeKind)
19 DateTime(ticks)
20 Kind = kind
21 End Sub
22
23 Sub DateTime(year As Long, month As Long, day As Long)
24 If year < 1 Or year > 9999 Or month < 1 Or month > 12 Or day < 1 Or day > DaysInMonth(year, month) Then
25 'ArgumentOutOfRangeException
26 debug
27 End If
28 DateTime(316224000000000)
29 AddYears(year - 1)
30
31 Dim days As Long
32 Dim i As Long
33 For i = 1 To month - 1
34 days += DaysInMonth(Year, i)
35 Next
36 days += day
37 AddDays(days - 1)
38 End Sub
39
40 Sub DateTime(year As Long, month As Long, day As Long, kind As DateTimeKind)
41 DateTime(year, month, day)
42 Kind = kind
43 End Sub
44
45 Sub DateTime(year As Long, month As Long, day As Long, hour As Long, minute As Long, second As Long)
46 If hour < 0 Or hour > 23 Or minute < 0 Or minute > 59 Or second < 0 Or second > 59 Then
47 'ArgumentOutOfRangeException
48 debug
49 End If
50 DateTime(year, month, day)
51 AddHours(hour)
52 AddMinutes(minute)
53 AddSeconds(second)
54 End Sub
55
56/* Sub DateTime(year As Long, month As Long, day As Long, hour As Long, minute As Long, second As Long, kind As DateTimeKind)
57 DateTime(year, month, day, hour, minute, second)
58 Kind = kind
59 End Sub*/
60
61 Sub DateTime(year As Long, month As Long, day As Long, hour As Long, minute As Long, second As Long, millisecond As Long)
62 DateTime(year, month, day, hour, minute, second)
63 AddMilliseconds(millisecond)
64 End Sub
65
66 Sub DateTime(year As Long, month As Long, day As Long, hour As Long, minute As Long, second As Long, millisecond As Long, kind As DateTimeKind)
67 DateTime(year, month, day, hour, minute, second, millisecond)
68 Kind = kind
69 End Sub
70
71 Sub DateTime(ByRef time As SYSTEMTIME)
72 DateTime(time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds)
73 End Sub
74
75 Sub DateTime(ByRef time As SYSTEMTIME, kind As DateTimeKind)
76 DateTime(time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds, kind)
77 End Sub
78
79 'Copy Constructor
80 Sub DateTime( ByRef datetime As DateTime )
81 This.m_Date = datetime.m_Date
82 End Sub
83
84 Sub ~DateTime()
85 End Sub
86
87 Function Operator == (ByRef value As DateTime) As Boolean
88 Return Equals(value)
89 End Function
90
91 Function Operator <> (ByRef value As DateTime) As Boolean
92 Return Not Equals(value)
93 End Function
94
95 Function Operator > (ByRef value As DateTime) As Boolean
96 If DateTime.Compare(This, value) > 0 Then
97 Return True
98 Else
99 Return False
100 End If
101 End Function
102
103 Function Operator < (ByRef value As DateTime) As Boolean
104 If DateTime.Compare(This, value) < 0 Then
105 Return True
106 Else
107 Return False
108 End If
109 End Function
110
111 Function Operator >= (ByRef value As DateTime) As Boolean
112 If DateTime.Compare(This, value) => 0 Then
113 Return True
114 Else
115 Return False
116 End If
117 End Function
118
119 Function Operator <= (ByRef value As DateTime) As Boolean
120 If DateTime.Compare(This, value) <= 0 Then
121 Return True
122 Else
123 Return False
124 End If
125 End Function
126
127 'Property
128 Function Ticks() As Int64
129 Return (m_Date And &H3FFFFFFFFFFFFFFF)
130 End Function
131
132 Function Millisecond() As Long
133 Return (Ticks \ 10000 Mod 1000) As Long
134 End Function
135
136 Function Second() As Long
137 Return (Ticks \ 10000000 Mod 60) As Long
138 End Function
139
140 Function Minute() As Long
141 Return (Ticks \ 600000000 Mod 60) As Long
142 End Function
143
144 Function Hour() As Long
145 Return (Ticks \ 36000000000 Mod 24) As Long
146 End Function
147
148 Function Day() As Long
149 Dim day As Long
150 day = DayOfYear
151
152 Dim i As Long
153 For i = 1 To Month - 1
154 day -= DaysInMonth(Year, i)
155 Next
156 Return day
157 End Function
158
159 Function Month() As Long
160 Dim year As Long
161 Dim day As Long
162 year = Year
163 day = DayOfYear
164
165 Dim i As Long
166 For i = 1 To 12
167 day -= DaysInMonth(year, i)
168 If day <= 0 Then Return i
169 Next
170 Return 12
171 End Function
172
173 Function Year() As Long
174 Dim day As Long
175 day = totalDays()
176 Return Int((day + day \ 36523 - day \ 146097) / 365.25)
177 End Function
178
179 Function DayOfWeek() As DayOfWeek
180 Return totalDays() Mod 7 - 1
181 End Function
182
183 Function Kind() As DateTimeKind
184 Return kindFromBinary(m_Date)
185 End Function
186
187 Function DayOfYear() As Long
188 Dim day As Long
189 day = totalDays()
190 Return day - Int(Year * 365.25 - day \ 36523 + day \ 146097)
191 End Function
192
193 Function Date() As DateTime
194 Dim date As DateTime(Year, Month, Day, Kind)
195 Return date
196 End Function
197
198 Static Function Now() As DateTime
199 Dim time As SYSTEMTIME
200 GetLocalTime(time)
201 Dim date As DateTime(time, Local)
202 Return date
203 End Function
204
205 Static Function ToDay() As DateTime
206 Dim time As SYSTEMTIME
207 GetLocalTime(time)
208 Dim date As DateTime(time.wYear, time.wMonth, time.wDay, Local)
209 Return date
210 End Function
211
212 Static Function UtcNow() As DateTime
213 Dim time As SYSTEMTIME
214 GetSystemTime(time)
215 Dim date As DateTime(time, Utc)
216 Return date
217 End Function
218
219 'method
220 Static Function Compare(ByRef t1 As DateTime, ByRef t2 As DateTime) As Int64
221 Return t1.Ticks - t2.Ticks
222 End Function
223
224 Function Equals(ByRef value As DateTime) As Boolean
225 If value.m_Date = m_Date Then
226 Return True
227 Else
228 Return False
229 End If
230 End Function
231
232 Static Function Equals(ByRef t1 As DateTime, ByRef t2 As DateTime) As Boolean
233 If t1.m_Date = t2.m_Date Then
234 Return True
235 Else
236 Return False
237 End If
238 End Function
239
240 Sub AddTicks(value As Int64)
241 Dim ticks As Int64
242 ticks = Ticks
243 If (ticks > DateTime.MaxValue - value) Or (ticks < DateTime.MinValue - value) Then
244 'ArgumentOutOfRangeException
245 debug
246 End If
247 Ticks = ticks + value
248 End Sub
249
250 Sub AddMilliseconds(value As Double)
251 AddTicks((value * 10000) As Int64)
252 End Sub
253
254 Sub AddSeconds(value As Double)
255 AddTicks((value * 10000000) As Int64)
256 End Sub
257
258 Sub AddMinutes(value As Double)
259 AddTicks((value * 600000000) As Int64)
260 End Sub
261
262 Sub AddHours(value As Double)
263 AddTicks((value * 36000000000) As Int64)
264 End Sub
265
266 Sub AddDays(value As Double)
267 AddTicks((value * 864000000000) As Int64)
268 End Sub
269
270 Sub AddYears(value As Double)
271 Dim year As Long
272 Dim intValue As Long
273 year = Year
274 intValue = Int(value)
275 AddTicks(intValue * 315360000000000 + 864000000000 * ((year Mod 4 + intValue) \ 4 - (year Mod 100 + intValue) \ 100 + (year Mod 400 + intValue) \ 400))
276 If value < 0 Then
277 If (year Mod 4 + intValue <= 0 And year Mod 100 > 4) Or (year Mod 400 <= 4) Then
278 AddTicks(-864000000000)
279 End If
280 End If
281
282 If IsLeapYear(year) = TRUE Then
283 AddTicks(((value - intValue) * 316224000000000) As Int64)
284 Else
285 AddTicks(((value - intValue) * 315360000000000) As Int64)
286 End If
287 End Sub
288
289 Static Function DaysInMonth(year As Long, month As Long) As Long
290 If year < 1 Or year > 9999 Or month < 1 Or month > 12 Then
291 'ArgumentOutOfRangeException
292 debug
293 End If
294 If IsLeapYear(year) And month = 2 Then
295 Return 29
296 Else
297 Dim daysInMonth[11] = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] As Byte
298 Return daysInMonth[month - 1]
299 End If
300 End Function
301
302 Static Function IsLeapYear(year As Long) As Boolean
303 If (year Mod 400) = 0 Then Return True
304 If (year Mod 100) = 0 Then Return False
305 If (year Mod 4) = 0 Then Return True
306 End Function
307
308 Function GetDateTimeFormats() As String
309 Return GetDateTimeFormats(NULL)
310 End Function
311
312 Function GetDateTimeFormats(format As *Byte) As String
313 Dim time As SYSTEMTIME
314 With time
315 .wYear = Year As Word
316 .wMonth = Month As Word
317 .wDay = Day As Word
318 .wHour = Hour As Word
319 .wMinute = Minute As Word
320 .wSecond = Second As Word
321 .wMilliseconds = Millisecond As Word
322 .wDayOfWeek = DayOfWeek() As Word
323 End With
324
325 Dim size As Long
326 size = GetDateFormat(LOCALE_USER_DEFAULT, 0, time, format, NULL, 0)
327 GetDateTimeFormats.ReSize(size - 1)
328 GetDateFormat(LOCALE_USER_DEFAULT, 0, time, format, GetDateTimeFormats, size)
329
330 Dim temp As String
331 If format = NULL Then
332 size = GetTimeFormat(LOCALE_USER_DEFAULT, 0, time, format, NULL, 0)
333 temp.ReSize(size - 1)
334 GetTimeFormat(LOCALE_USER_DEFAULT, 0, time, format, temp, size)
335 GetDateTimeFormats = GetDateTimeFormats + " " + temp
336 Else
337 size = GetTimeFormat(LOCALE_USER_DEFAULT, 0, time, GetDateTimeFormats, NULL, 0)
338 temp.ReSize(size - 1)
339 GetTimeFormat(LOCALE_USER_DEFAULT, 0, time, GetDateTimeFormats, temp, size)
340 GetDateTimeFormats = temp
341 End If
342 End Function
343
344 Static Function FromBinary(dateData As Int64) As DateTime
345 Dim date As DateTime((dateData And &H3FFFFFFFFFFFFFFF), kindFromBinary(dateData))
346 Return date
347 End Function
348
349 Function ToBinary() As Int64
350 Return m_Date
351 End Function
352
353 Static Function FromFileTime(fileTime As FILETIME) As DateTime
354 Dim localTime As FILETIME
355 Dim time As SYSTEMTIME
356 FileTimeToLocalFileTime(fileTime, localTime)
357 FileTimeToSystemTime(localTime, time)
358
359 Dim date As DateTime(time, Local)
360 Return date
361 End Function
362
363 Function ToFileTime() As FILETIME
364 Dim time As SYSTEMTIME
365 With time
366 .wYear = Year As Word
367 .wMonth = Month As Word
368 .wDayOfWeek = DayOfWeek As Word
369 .wDay = Day As Word
370 .wHour = Hour As Word
371 .wMinute = Minute As Word
372 .wSecond = Second As Word
373 .wMilliseconds = Millisecond As Word
374 End With
375 Dim fileTime As FILETIME
376 SystemTimeToFileTime(time, fileTime)
377 Return fileTime
378 End Function
379
380 Static Function FromFileTimeUtc(fileTime As FILETIME) As DateTime
381 Dim time As SYSTEMTIME
382 FileTimeToSystemTime(fileTime, time)
383
384 Dim date As DateTime(time, Utc)
385 Return date
386 End Function
387
388 Function ToFileTimeUtc() As FILETIME
389 Dim fileTime As FILETIME
390 fileTime = ToFileTime()
391 If Kind = Utc Then
392 ToFileTimeUtc = fileTime
393 Else
394 LocalFileTimeToFileTime(fileTime, ToFileTimeUtc) 'Return
395 End If
396 End Function
397
398 Function ToLocalTime() As DateTime
399 If Kind = Local Then
400 ToLocalTime = This
401 Else
402 ToLocalTime = DateTime.FromFileTime(ToFileTime())
403 ToLocalTime.Kind = Local
404 End If
405 End Function
406
407 Function ToUniversalTime() As DateTime
408 If Kind = Utc Then
409 ToUniversalTime = This
410 Else
411 ToUniversalTime = DateTime.FromFileTimeUtc(ToFileTimeUtc())
412 ToUniversalTime.Kind = Utc
413 End If
414 End Function
415Private
416 Sub Ticks(value As Int64)
417 Dim kind As DateTimeKind
418 kind = Kind
419 m_Date = value
420 Kind = kind
421 End Sub
422
423 Sub Kind(kind As DateTimeKind)
424 Dim temp As Int64
425 temp = kind
426 temp = (temp << 62) And &HC000000000000000
427 m_Date = (m_Date And &H3FFFFFFFFFFFFFFF) Or temp
428 End Sub
429
430 Function totalDays() As Long
431 Return (Ticks \ 864000000000) As Long
432 End Function
433
434 Function kindFromBinary(dateData As Int64) As Long
435 dateData = (dateData >> 62) And &H03
436 If dateData = &H01 Then
437 Return Local
438 ElseIf dateData = &H02 Then
439 Return Unspecified
440 ElseIf dateData = &H03 Then
441 Return Utc
442 End If
443 End Function
444End Class
445
446Const Enum DateTimeKind
447 Local
448 Unspecified
449 Utc
450End Enum
451
452Const Enum DayOfWeek
453 Sunday = 0
454 Monday
455 Tuesday
456 Wednesday
457 Thursday
458 Friday
459 Saturday
460End Enum
461
462#endif '__SYSTEM_DATETIME_AB__
Note: See TracBrowser for help on using the repository browser.