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

Last change on this file since 207 was 207, checked in by dai, 17 years ago

動的型情報(Object.GetType)に対応。
戻り値やクラスメンバがオブジェクトだったとき、その初期値をNothingにした(※戻り値として関数名を使っている部分、要注意!!)。

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