[275] | 1 | Namespace System
|
---|
[36] | 2 |
|
---|
[409] | 3 | /*!
|
---|
| 4 | @brief 時刻の種類
|
---|
| 5 | */
|
---|
| 6 | Enum DateTimeKind
|
---|
| 7 | Local
|
---|
| 8 | Unspecified
|
---|
| 9 | Utc
|
---|
| 10 | End Enum
|
---|
[275] | 11 |
|
---|
[409] | 12 | /*!
|
---|
| 13 | @brief 曜日
|
---|
| 14 | */
|
---|
| 15 | Enum DayOfWeek
|
---|
| 16 | Sunday = 0
|
---|
| 17 | Monday
|
---|
| 18 | Tuesday
|
---|
| 19 | Wednesday
|
---|
| 20 | Thursday
|
---|
| 21 | Friday
|
---|
| 22 | Saturday
|
---|
| 23 | End Enum
|
---|
| 24 |
|
---|
| 25 |
|
---|
| 26 | /*!
|
---|
| 27 | @brief 時刻を表すクラス
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[36] | 30 | Class DateTime
|
---|
| 31 | m_Date As Int64
|
---|
| 32 | Public
|
---|
[409] | 33 | Static Const MaxValue = 3162240000000000000 As Int64 'Const TicksPerDay*366*10000
|
---|
| 34 | Static Const MinValue = 316224000000000 As Int64 'Const TicksPerDay*366
|
---|
[36] | 35 |
|
---|
[409] | 36 | '----------------------------------------------------------------
|
---|
| 37 | ' パブリック コンストラクタ
|
---|
| 38 | '----------------------------------------------------------------
|
---|
| 39 |
|
---|
| 40 | /*!
|
---|
| 41 | @brief コンストラクタ
|
---|
| 42 | */
|
---|
[163] | 43 | Sub DateTime()
|
---|
[209] | 44 | initialize(MinValue, DateTimeKind.Unspecified)
|
---|
[163] | 45 | End Sub
|
---|
| 46 |
|
---|
[409] | 47 | /*!
|
---|
| 48 | @brief 時刻を指定して初期化する
|
---|
| 49 | @param 100ナノ秒単位で表した時刻
|
---|
| 50 | */
|
---|
[36] | 51 | Sub DateTime(ticks As Int64)
|
---|
[209] | 52 | initialize(ticks, DateTimeKind.Unspecified)
|
---|
[36] | 53 | End Sub
|
---|
| 54 |
|
---|
[409] | 55 | /*!
|
---|
| 56 | @brief 時刻を指定して初期化する
|
---|
| 57 | @param 100ナノ秒単位で表した時刻
|
---|
| 58 | @param 時刻の種類
|
---|
| 59 | */
|
---|
[36] | 60 | Sub DateTime(ticks As Int64, kind As DateTimeKind)
|
---|
[209] | 61 | initialize(ticks, kind)
|
---|
[36] | 62 | End Sub
|
---|
| 63 |
|
---|
[409] | 64 | /*!
|
---|
| 65 | @brief 時刻を指定して初期化する
|
---|
| 66 | @param 西暦
|
---|
| 67 | @param 月
|
---|
| 68 | @param 日
|
---|
| 69 | */
|
---|
[36] | 70 | Sub DateTime(year As Long, month As Long, day As Long)
|
---|
[209] | 71 | initialize(year, month, day, 0, 0, 0, 0, DateTimeKind.Unspecified)
|
---|
[36] | 72 | End Sub
|
---|
| 73 |
|
---|
[409] | 74 | /*!
|
---|
| 75 | @brief 時刻を指定して初期化する
|
---|
| 76 | @param 西暦
|
---|
| 77 | @param 月
|
---|
| 78 | @param 日
|
---|
| 79 | @param 時刻の種類
|
---|
| 80 | */
|
---|
[36] | 81 | Sub DateTime(year As Long, month As Long, day As Long, kind As DateTimeKind)
|
---|
[209] | 82 | initialize(year, month, day, 0, 0, 0, 0, kind)
|
---|
[36] | 83 | End Sub
|
---|
| 84 |
|
---|
[409] | 85 | /*!
|
---|
| 86 | @brief 時刻を指定して初期化する
|
---|
| 87 | @param 西暦
|
---|
| 88 | @param 月
|
---|
| 89 | @param 日
|
---|
| 90 | @param 時
|
---|
| 91 | @param 分
|
---|
| 92 | @param 秒
|
---|
| 93 | */
|
---|
[36] | 94 | Sub DateTime(year As Long, month As Long, day As Long, hour As Long, minute As Long, second As Long)
|
---|
[209] | 95 | initialize(year, month, day, hour, minute, second, 0, DateTimeKind.Unspecified)
|
---|
[36] | 96 | End Sub
|
---|
| 97 |
|
---|
[409] | 98 | /*!
|
---|
| 99 | @brief 時刻を指定して初期化する
|
---|
| 100 | @param 西暦
|
---|
| 101 | @param 月
|
---|
| 102 | @param 日
|
---|
| 103 | @param 時
|
---|
| 104 | @param 分
|
---|
| 105 | @param 秒
|
---|
| 106 | @param 時刻の種類
|
---|
| 107 | */
|
---|
[103] | 108 | Sub DateTime(year As Long, month As Long, day As Long, hour As Long, minute As Long, second As Long, kind As DateTimeKind)
|
---|
[209] | 109 | initialize(year, month, day, hour, minute, second, 0, kind)
|
---|
[103] | 110 | End Sub
|
---|
[36] | 111 |
|
---|
[409] | 112 | /*!
|
---|
| 113 | @brief 時刻を指定して初期化する
|
---|
| 114 | @param 西暦
|
---|
| 115 | @param 月
|
---|
| 116 | @param 日
|
---|
| 117 | @param 時
|
---|
| 118 | @param 分
|
---|
| 119 | @param 秒
|
---|
| 120 | @param ミリ秒
|
---|
| 121 | */
|
---|
[36] | 122 | Sub DateTime(year As Long, month As Long, day As Long, hour As Long, minute As Long, second As Long, millisecond As Long)
|
---|
[209] | 123 | initialize(year, month, day, hour, minute, second, millisecond, DateTimeKind.Unspecified)
|
---|
[36] | 124 | End Sub
|
---|
| 125 |
|
---|
[409] | 126 | /*!
|
---|
| 127 | @brief 時刻を指定して初期化する
|
---|
| 128 | @param 西暦
|
---|
| 129 | @param 月
|
---|
| 130 | @param 日
|
---|
| 131 | @param 時
|
---|
| 132 | @param 分
|
---|
| 133 | @param 秒
|
---|
| 134 | @param ミリ秒
|
---|
| 135 | @param 時刻の種類
|
---|
| 136 | */
|
---|
[36] | 137 | 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)
|
---|
[209] | 138 | initialize(year, month, day, hour, minute, second, millisecond, kind)
|
---|
[36] | 139 | End Sub
|
---|
| 140 |
|
---|
[409] | 141 | /*!
|
---|
| 142 | @brief 時刻を指定して初期化する
|
---|
| 143 | @param SYSTEMTIME構造体
|
---|
| 144 | */
|
---|
[36] | 145 | Sub DateTime(ByRef time As SYSTEMTIME)
|
---|
[209] | 146 | initialize(time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds, DateTimeKind.Unspecified)
|
---|
[36] | 147 | End Sub
|
---|
| 148 |
|
---|
[409] | 149 | /*!
|
---|
| 150 | @brief 時刻を指定して初期化する
|
---|
| 151 | @param SYSTEMTIME構造体
|
---|
| 152 | @param 時刻の種類
|
---|
| 153 | */
|
---|
[36] | 154 | Sub DateTime(ByRef time As SYSTEMTIME, kind As DateTimeKind)
|
---|
[209] | 155 | initialize(time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds, kind)
|
---|
[36] | 156 | End Sub
|
---|
| 157 |
|
---|
[409] | 158 | /*!
|
---|
| 159 | @brief コピーコンストラクタ
|
---|
| 160 | @param コピーするDateTime
|
---|
| 161 | */
|
---|
[268] | 162 | Sub DateTime(dateTime As DateTime)
|
---|
[209] | 163 | This.m_Date = dateTime.m_Date
|
---|
[40] | 164 | End Sub
|
---|
| 165 |
|
---|
[409] | 166 | '----------------------------------------------------------------
|
---|
| 167 | ' オペレータ
|
---|
| 168 | '----------------------------------------------------------------
|
---|
[36] | 169 |
|
---|
[268] | 170 | Function Operator + (value As TimeSpan) As DateTime
|
---|
[209] | 171 | Return New DateTime(Ticks + value.Ticks)
|
---|
[107] | 172 | End Function
|
---|
| 173 |
|
---|
[268] | 174 | Function Operator - (value As DateTime) As TimeSpan
|
---|
[107] | 175 | Return TimeSpan.FromTicks(Ticks - value.Ticks)
|
---|
| 176 | End Function
|
---|
| 177 |
|
---|
[268] | 178 | Function Operator - (value As TimeSpan) As DateTime
|
---|
[209] | 179 | Return New DateTime(Ticks - value.Ticks)
|
---|
[107] | 180 | End Function
|
---|
| 181 |
|
---|
[268] | 182 | Function Operator == (value As DateTime) As Boolean
|
---|
[36] | 183 | Return Equals(value)
|
---|
| 184 | End Function
|
---|
| 185 |
|
---|
[268] | 186 | Function Operator <> (value As DateTime) As Boolean
|
---|
[36] | 187 | Return Not Equals(value)
|
---|
| 188 | End Function
|
---|
| 189 |
|
---|
[268] | 190 | Function Operator > (value As DateTime) As Boolean
|
---|
[36] | 191 | If DateTime.Compare(This, value) > 0 Then
|
---|
[93] | 192 | Return True
|
---|
[36] | 193 | Else
|
---|
[93] | 194 | Return False
|
---|
[36] | 195 | End If
|
---|
| 196 | End Function
|
---|
| 197 |
|
---|
[268] | 198 | Function Operator < (value As DateTime) As Boolean
|
---|
[36] | 199 | If DateTime.Compare(This, value) < 0 Then
|
---|
[93] | 200 | Return True
|
---|
[36] | 201 | Else
|
---|
[93] | 202 | Return False
|
---|
[36] | 203 | End If
|
---|
| 204 | End Function
|
---|
| 205 |
|
---|
[268] | 206 | Function Operator >= (value As DateTime) As Boolean
|
---|
[36] | 207 | If DateTime.Compare(This, value) => 0 Then
|
---|
[93] | 208 | Return True
|
---|
[36] | 209 | Else
|
---|
[93] | 210 | Return False
|
---|
[36] | 211 | End If
|
---|
| 212 | End Function
|
---|
| 213 |
|
---|
[268] | 214 | Function Operator <= (value As DateTime) As Boolean
|
---|
[36] | 215 | If DateTime.Compare(This, value) <= 0 Then
|
---|
[93] | 216 | Return True
|
---|
[36] | 217 | Else
|
---|
[93] | 218 | Return False
|
---|
[36] | 219 | End If
|
---|
| 220 | End Function
|
---|
| 221 |
|
---|
[409] | 222 | '----------------------------------------------------------------
|
---|
| 223 | ' パブリック プロパティ
|
---|
| 224 | '----------------------------------------------------------------
|
---|
| 225 |
|
---|
| 226 | /*!
|
---|
| 227 | @brief 時刻を100ナノ秒単位で取得する
|
---|
| 228 | @return 時刻
|
---|
| 229 | */
|
---|
[36] | 230 | Function Ticks() As Int64
|
---|
| 231 | Return (m_Date And &H3FFFFFFFFFFFFFFF)
|
---|
| 232 | End Function
|
---|
| 233 |
|
---|
[409] | 234 | /*!
|
---|
| 235 | @brief 時刻のミリ秒単位部分を取得する
|
---|
| 236 | @return ミリ秒の部分
|
---|
| 237 | */
|
---|
[36] | 238 | Function Millisecond() As Long
|
---|
[107] | 239 | Return (Ticks \ TimeSpan.TicksPerMillisecond Mod 1000) As Long
|
---|
[36] | 240 | End Function
|
---|
| 241 |
|
---|
[409] | 242 | /*!
|
---|
| 243 | @brief 時刻の秒単位部分を取得する
|
---|
| 244 | @return 秒の部分
|
---|
| 245 | */
|
---|
[36] | 246 | Function Second() As Long
|
---|
[107] | 247 | Return (Ticks \ TimeSpan.TicksPerSecond Mod 60) As Long
|
---|
[36] | 248 | End Function
|
---|
| 249 |
|
---|
[409] | 250 | /*!
|
---|
| 251 | @brief 時刻の分単位部分を取得する
|
---|
| 252 | @return 分の部分
|
---|
| 253 | */
|
---|
[36] | 254 | Function Minute() As Long
|
---|
[107] | 255 | Return (Ticks \ TimeSpan.TicksPerMinute Mod 60) As Long
|
---|
[36] | 256 | End Function
|
---|
| 257 |
|
---|
[409] | 258 | /*!
|
---|
| 259 | @brief 時刻の時単位部分を取得する
|
---|
| 260 | @return 時の部分
|
---|
| 261 | */
|
---|
[36] | 262 | Function Hour() As Long
|
---|
[107] | 263 | Return (Ticks \ TimeSpan.TicksPerHour Mod 24) As Long
|
---|
[36] | 264 | End Function
|
---|
| 265 |
|
---|
[409] | 266 | /*!
|
---|
| 267 | @brief 時刻の日単位部分を取得する
|
---|
| 268 | @return 日の部分
|
---|
| 269 | */
|
---|
[36] | 270 | Function Day() As Long
|
---|
[263] | 271 | Return DayOfYear - totalDaysOfMonth(Year, Month - 1)
|
---|
[36] | 272 | End Function
|
---|
| 273 |
|
---|
[409] | 274 | /*!
|
---|
| 275 | @brief 時刻の月単位部分を取得する
|
---|
| 276 | @return 月の部分
|
---|
| 277 | */
|
---|
[36] | 278 | Function Month() As Long
|
---|
[209] | 279 | Dim year = Year As Long
|
---|
| 280 | Dim day = DayOfYear As Long
|
---|
| 281 | Dim i = 1 As Long
|
---|
[263] | 282 | While day > totalDaysOfMonth(year, i)
|
---|
[209] | 283 | i++
|
---|
| 284 | Wend
|
---|
| 285 | Return i
|
---|
[36] | 286 | End Function
|
---|
| 287 |
|
---|
[409] | 288 | /*!
|
---|
| 289 | @brief 時刻の年単位部分を取得する
|
---|
| 290 | @return 西暦
|
---|
| 291 | */
|
---|
[36] | 292 | Function Year() As Long
|
---|
[209] | 293 | Dim day = (Ticks \ TimeSpan.TicksPerDay) As Long
|
---|
[263] | 294 | Dim year = Int((day + day \ 36524 - day \ 146097) / 365.25) + 1 As Long
|
---|
| 295 | If day - yearToDay(year - 1) + 1 = 366 Then
|
---|
[209] | 296 | Return year + 1
|
---|
| 297 | Else
|
---|
| 298 | Return year
|
---|
| 299 | End If
|
---|
[36] | 300 | End Function
|
---|
| 301 |
|
---|
[409] | 302 | /*!
|
---|
| 303 | @brief 時刻の曜日部分を取得する
|
---|
| 304 | @return 曜日の部分
|
---|
| 305 | */
|
---|
| 306 | Function DayOfWeek() As DayOfWeek
|
---|
| 307 | Return New DayOfWeek( (((Ticks \ TimeSpan.TicksPerDay) Mod 7) + 1) As Long, "DayOfWeek")
|
---|
[36] | 308 | End Function
|
---|
| 309 |
|
---|
[409] | 310 | /*!
|
---|
| 311 | @brief 時刻の種類を取得する
|
---|
| 312 | @return 種類
|
---|
| 313 | */
|
---|
[81] | 314 | Function Kind() As DateTimeKind
|
---|
[36] | 315 | Return kindFromBinary(m_Date)
|
---|
| 316 | End Function
|
---|
| 317 |
|
---|
[409] | 318 | /*!
|
---|
| 319 | @brief その年の何日目かを取得する
|
---|
| 320 | @return 日数
|
---|
| 321 | */
|
---|
[36] | 322 | Function DayOfYear() As Long
|
---|
[263] | 323 | Return ((Ticks \ TimeSpan.TicksPerDay) - yearToDay(Year) + 1) As Long
|
---|
[36] | 324 | End Function
|
---|
| 325 |
|
---|
[409] | 326 | /*!
|
---|
| 327 | @brief 時刻の日付の部分を取得する
|
---|
| 328 | @return 日付
|
---|
| 329 | */
|
---|
[36] | 330 | Function Date() As DateTime
|
---|
[209] | 331 | Return New DateTime(Year, Month, Day, Kind)
|
---|
[36] | 332 | End Function
|
---|
| 333 |
|
---|
[409] | 334 | /*!
|
---|
| 335 | @brief 現在の時刻を取得する
|
---|
| 336 | @return 時刻
|
---|
| 337 | */
|
---|
[36] | 338 | Static Function Now() As DateTime
|
---|
| 339 | Dim time As SYSTEMTIME
|
---|
| 340 | GetLocalTime(time)
|
---|
[209] | 341 | Return New DateTime(time, DateTimeKind.Local)
|
---|
[36] | 342 | End Function
|
---|
| 343 |
|
---|
[409] | 344 | /*!
|
---|
| 345 | @brief 今日を表す時刻を取得する(時間は0時0分)
|
---|
| 346 | @return 時刻
|
---|
| 347 | */
|
---|
[272] | 348 | Static Function Today() As DateTime
|
---|
[36] | 349 | Dim time As SYSTEMTIME
|
---|
| 350 | GetLocalTime(time)
|
---|
[209] | 351 | Return New DateTime(time.wYear, time.wMonth, time.wDay, DateTimeKind.Local)
|
---|
[36] | 352 | End Function
|
---|
| 353 |
|
---|
[409] | 354 | /*!
|
---|
| 355 | @brief 現在の時刻をUTC時刻で取得する
|
---|
| 356 | @return 時刻
|
---|
| 357 | */
|
---|
[36] | 358 | Static Function UtcNow() As DateTime
|
---|
| 359 | Dim time As SYSTEMTIME
|
---|
| 360 | GetSystemTime(time)
|
---|
[209] | 361 | Return New DateTime(time, DateTimeKind.Utc)
|
---|
[36] | 362 | End Function
|
---|
| 363 |
|
---|
[409] | 364 | '----------------------------------------------------------------
|
---|
| 365 | ' パブリック メソッド
|
---|
| 366 | '----------------------------------------------------------------
|
---|
| 367 |
|
---|
| 368 | /*!
|
---|
| 369 | @brief 二つの時刻の差を求める
|
---|
| 370 | @return 差(単位は100ナノ秒)
|
---|
| 371 | */
|
---|
[268] | 372 | Static Function Compare(t1 As DateTime, t2 As DateTime) As Int64
|
---|
[36] | 373 | Return t1.Ticks - t2.Ticks
|
---|
| 374 | End Function
|
---|
| 375 |
|
---|
[409] | 376 | /*!
|
---|
| 377 | @brief 等しいどうかを取得する
|
---|
| 378 | @param 比較する値
|
---|
| 379 | @retval True 等しい
|
---|
| 380 | @retval False 等しくない
|
---|
| 381 | */
|
---|
[268] | 382 | Function Equals(value As DateTime) As Boolean
|
---|
[36] | 383 | If value.m_Date = m_Date Then
|
---|
[93] | 384 | Return True
|
---|
[36] | 385 | Else
|
---|
[93] | 386 | Return False
|
---|
[36] | 387 | End If
|
---|
| 388 | End Function
|
---|
| 389 |
|
---|
[409] | 390 | /*!
|
---|
| 391 | @brief 等しいどうかを取得する
|
---|
| 392 | @param 比較される値
|
---|
| 393 | @param 比較する値
|
---|
| 394 | @retval True 等しい
|
---|
| 395 | @retval False 等しくない
|
---|
| 396 | */
|
---|
[268] | 397 | Static Function Equals(t1 As DateTime, t2 As DateTime) As Boolean
|
---|
[36] | 398 | If t1.m_Date = t2.m_Date Then
|
---|
[93] | 399 | Return True
|
---|
[36] | 400 | Else
|
---|
[93] | 401 | Return False
|
---|
[36] | 402 | End If
|
---|
| 403 | End Function
|
---|
| 404 |
|
---|
[409] | 405 | /*!
|
---|
| 406 | @brief ハッシュコードを取得する
|
---|
| 407 | @return ハッシュコード
|
---|
| 408 | */
|
---|
[166] | 409 | Override Function GetHashCode() As Long
|
---|
| 410 | Return HIDWORD(m_Date) Xor LODWORD(m_Date)
|
---|
| 411 | End Function
|
---|
| 412 |
|
---|
[409] | 413 | /*!
|
---|
| 414 | @brief 時刻を進める
|
---|
| 415 | @param 進める時間
|
---|
| 416 | @return 進めた後の時刻
|
---|
| 417 | */
|
---|
[268] | 418 | Function Add(value As TimeSpan) As DateTime
|
---|
[107] | 419 | Return This + value
|
---|
| 420 | End Function
|
---|
| 421 |
|
---|
[409] | 422 | /*!
|
---|
| 423 | @brief 時刻を進める
|
---|
| 424 | @param 進める時間(100ナノ秒単位)
|
---|
| 425 | @return 進めた後の時刻
|
---|
| 426 | */
|
---|
[107] | 427 | Function AddTicks(value As Int64) As DateTime
|
---|
[209] | 428 | Return New DateTime(Ticks + value, Kind )
|
---|
[107] | 429 | End Function
|
---|
[36] | 430 |
|
---|
[409] | 431 | /*!
|
---|
| 432 | @brief 時刻を進める
|
---|
| 433 | @param 進める時間(ミリ秒単位)
|
---|
| 434 | @return 進めた後の時刻
|
---|
| 435 | */
|
---|
[107] | 436 | Function AddMilliseconds(value As Double) As DateTime
|
---|
| 437 | Return AddTicks((value * TimeSpan.TicksPerMillisecond) As Int64)
|
---|
| 438 | End Function
|
---|
[36] | 439 |
|
---|
[409] | 440 | /*!
|
---|
| 441 | @brief 時刻を進める
|
---|
| 442 | @param 進める時間(秒単位)
|
---|
| 443 | @return 進めた後の時刻
|
---|
| 444 | */
|
---|
[107] | 445 | Function AddSeconds(value As Double) As DateTime
|
---|
| 446 | Return AddTicks((value * TimeSpan.TicksPerSecond) As Int64)
|
---|
| 447 | End Function
|
---|
[36] | 448 |
|
---|
[409] | 449 | /*!
|
---|
| 450 | @brief 時刻を進める
|
---|
| 451 | @param 進める時間(分単位)
|
---|
| 452 | @return 進めた後の時刻
|
---|
| 453 | */
|
---|
[107] | 454 | Function AddMinutes(value As Double) As DateTime
|
---|
| 455 | Return AddTicks((value * TimeSpan.TicksPerMinute) As Int64)
|
---|
| 456 | End Function
|
---|
[36] | 457 |
|
---|
[409] | 458 | /*!
|
---|
| 459 | @brief 時刻を進める
|
---|
| 460 | @param 進める時間(時単位)
|
---|
| 461 | @return 進めた後の時刻
|
---|
| 462 | */
|
---|
[107] | 463 | Function AddHours(value As Double) As DateTime
|
---|
| 464 | Return AddTicks((value * TimeSpan.TicksPerHour) As Int64)
|
---|
| 465 | End Function
|
---|
[36] | 466 |
|
---|
[409] | 467 | /*!
|
---|
| 468 | @brief 時刻を進める
|
---|
| 469 | @param 進める時間(日単位)
|
---|
| 470 | @return 進めた後の時刻
|
---|
| 471 | */
|
---|
[107] | 472 | Function AddDays(value As Double) As DateTime
|
---|
| 473 | Return AddTicks((value * TimeSpan.TicksPerDay) As Int64)
|
---|
| 474 | End Function
|
---|
| 475 |
|
---|
[409] | 476 | /*!
|
---|
| 477 | @brief 時刻を進める
|
---|
| 478 | @param 進める時間(年単位)
|
---|
| 479 | @return 進めた後の時刻
|
---|
| 480 | */
|
---|
[107] | 481 | Function AddYears(value As Double) As DateTime
|
---|
[263] | 482 | Dim date = New DateTime(Year + Int(value), Month, Day, Hour, Minute, Second, Millisecond, Kind)
|
---|
| 483 | Dim ticks = Ticks _
|
---|
| 484 | - (yearToDay(Year) + totalDaysOfMonth(Year, Month - 1) + Day - 1) * TimeSpan.TicksPerDay _
|
---|
| 485 | - Hour * TimeSpan.TicksPerHour _
|
---|
| 486 | - Minute * TimeSpan.TicksPerMinute _
|
---|
| 487 | - Second * TimeSpan.TicksPerSecond _
|
---|
| 488 | - Millisecond * TimeSpan.TicksPerMillisecond As Int64
|
---|
| 489 | If IsLeapYear(Year + Int(value)) Then
|
---|
[370] | 490 | ticks += ( (value - Int(value)) * 366 * TimeSpan.TicksPerDay ) As Int64
|
---|
[36] | 491 | Else
|
---|
[370] | 492 | ticks += ( (value - Int(value)) * 365 * TimeSpan.TicksPerDay ) As Int64
|
---|
[36] | 493 | End If
|
---|
[263] | 494 | Return date.AddTicks(ticks)
|
---|
[107] | 495 | End Function
|
---|
[36] | 496 |
|
---|
[409] | 497 | /*!
|
---|
| 498 | @brief 時刻の差を取得する
|
---|
| 499 | @param 比較する値
|
---|
| 500 | @return 時刻の差
|
---|
| 501 | */
|
---|
[268] | 502 | Function Subtract(value As DateTime) As TimeSpan
|
---|
| 503 | Return This - value
|
---|
[107] | 504 | End Function
|
---|
| 505 |
|
---|
[409] | 506 | /*!
|
---|
| 507 | @brief 時刻を戻す
|
---|
| 508 | @param 戻す時間
|
---|
| 509 | @return 時刻
|
---|
| 510 | */
|
---|
[268] | 511 | Function Subtract(value As TimeSpan) As DateTime
|
---|
| 512 | Return This - value
|
---|
[107] | 513 | End Function
|
---|
| 514 |
|
---|
[409] | 515 | /*!
|
---|
| 516 | @brief 指定した年月が、その月の何日目かを取得する
|
---|
| 517 | @param 西暦
|
---|
| 518 | @param 月
|
---|
| 519 | @return 日数
|
---|
| 520 | */
|
---|
[93] | 521 | Static Function DaysInMonth(year As Long, month As Long) As Long
|
---|
[409] | 522 | If year < 1 Or year > 9999 Then
|
---|
| 523 | Throw New ArgumentOutOfRangeException("DateTime.DaysInMonth: One or more arguments are out of range value.", "year")
|
---|
[36] | 524 | End If
|
---|
[409] | 525 | If month < 1 Or month > 12 Then
|
---|
| 526 | Throw New ArgumentOutOfRangeException("DateTime.DaysInMonth: One or more arguments are out of range value.", "month")
|
---|
| 527 | End If
|
---|
[209] | 528 |
|
---|
[93] | 529 | If IsLeapYear(year) And month = 2 Then
|
---|
| 530 | Return 29
|
---|
| 531 | Else
|
---|
| 532 | Dim daysInMonth[11] = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] As Byte
|
---|
| 533 | Return daysInMonth[month - 1]
|
---|
| 534 | End If
|
---|
[36] | 535 | End Function
|
---|
| 536 |
|
---|
[409] | 537 | /*!
|
---|
| 538 | @brief 年が閏年かどうかを取得する
|
---|
| 539 | @param 西暦
|
---|
| 540 | @retval True 閏年
|
---|
| 541 | @retval False 閏年でない
|
---|
| 542 | */
|
---|
[93] | 543 | Static Function IsLeapYear(year As Long) As Boolean
|
---|
| 544 | If (year Mod 400) = 0 Then Return True
|
---|
| 545 | If (year Mod 100) = 0 Then Return False
|
---|
| 546 | If (year Mod 4) = 0 Then Return True
|
---|
[103] | 547 | Return False
|
---|
[36] | 548 | End Function
|
---|
| 549 |
|
---|
[409] | 550 | /*!
|
---|
| 551 | @brief 時刻を文字列に変換する
|
---|
| 552 | @return 時刻を表した文字列
|
---|
| 553 | */
|
---|
[36] | 554 | Function GetDateTimeFormats() As String
|
---|
[209] | 555 | Dim time = getSystemTime() 'As SYSTEMTIME を記述すると内部エラーが発生
|
---|
| 556 | Dim dateFormatSize = GetDateFormat(LOCALE_USER_DEFAULT, 0, time, NULL, NULL, 0)
|
---|
| 557 | Dim timeFormatSize = GetTimeFormat(LOCALE_USER_DEFAULT, 0, time, NULL, NULL, 0)
|
---|
[272] | 558 | Dim strLength = dateFormatSize + timeFormatSize
|
---|
| 559 | Dim dateTimeFormats = GC_malloc_atomic(SizeOf (TCHAR) * (strLength)) As PTSTR
|
---|
[209] | 560 | GetDateFormat(LOCALE_USER_DEFAULT, 0, time, NULL, dateTimeFormats, dateFormatSize)
|
---|
[400] | 561 | dateTimeFormats[dateFormatSize - 1] = &H20 As TCHAR 'Asc(" ") As TCHAR
|
---|
[209] | 562 | GetTimeFormat(LOCALE_USER_DEFAULT, 0, time, NULL, dateTimeFormats + dateFormatSize, timeFormatSize)
|
---|
[272] | 563 | Return New String(dateTimeFormats, strLength)
|
---|
[36] | 564 | End Function
|
---|
| 565 |
|
---|
[409] | 566 | /*!
|
---|
| 567 | @brief 時刻を指定した書式で文字列に変換する
|
---|
| 568 | @param 書式
|
---|
| 569 | @return 時刻を表した文字列
|
---|
| 570 | */
|
---|
[209] | 571 | Function GetDateTimeFormats(format As *TCHAR) As String
|
---|
| 572 | Dim time = getSystemTime() 'As SYSTEMTIME を記述すると内部エラーが発生
|
---|
| 573 | Dim dateFormatSize = GetDateFormat(LOCALE_USER_DEFAULT, 0, time, format, NULL, 0)
|
---|
| 574 | Dim dateFormats = malloc(dateFormatSize) As PTSTR
|
---|
| 575 | GetDateFormat(LOCALE_USER_DEFAULT, 0, time, format, dateFormats, dateFormatSize)
|
---|
[73] | 576 |
|
---|
[209] | 577 | Dim dateTimeFormatSize = GetTimeFormat(LOCALE_USER_DEFAULT, 0, time, dateFormats, NULL, 0)
|
---|
| 578 | Dim dateTimeFormats = malloc(dateTimeFormatSize) As PTSTR
|
---|
| 579 | GetTimeFormat(LOCALE_USER_DEFAULT, 0, time, dateFormats, dateTimeFormats, dateTimeFormatSize)
|
---|
[207] | 580 |
|
---|
[209] | 581 | Return New String(dateTimeFormats)
|
---|
[73] | 582 | End Function
|
---|
| 583 |
|
---|
[409] | 584 | /*!
|
---|
| 585 | @brief バイナリデータからDateTimeを作成する
|
---|
| 586 | @return DateTimeクラス
|
---|
| 587 | */
|
---|
[209] | 588 | Static Function FromBinary(date As Int64) As DateTime
|
---|
| 589 | Return New DateTime((date And &H3FFFFFFFFFFFFFFF), kindFromBinary(date))
|
---|
[36] | 590 | End Function
|
---|
| 591 |
|
---|
[409] | 592 | /*!
|
---|
| 593 | @brief バイナリデータに変換する
|
---|
| 594 | @return バイナリデータ
|
---|
| 595 | */
|
---|
[36] | 596 | Function ToBinary() As Int64
|
---|
| 597 | Return m_Date
|
---|
| 598 | End Function
|
---|
| 599 |
|
---|
[409] | 600 | /*!
|
---|
| 601 | @brief FILETIME構造体からDateTimeを作成する
|
---|
| 602 | @return DateTimeクラス
|
---|
| 603 | */
|
---|
[209] | 604 | Static Function FromFileTime(ByRef fileTime As FILETIME) As DateTime
|
---|
[36] | 605 | Dim localTime As FILETIME
|
---|
[82] | 606 | Dim time As SYSTEMTIME
|
---|
[36] | 607 | FileTimeToLocalFileTime(fileTime, localTime)
|
---|
| 608 | FileTimeToSystemTime(localTime, time)
|
---|
[209] | 609 | Return New DateTime(time, DateTimeKind.Local)
|
---|
[36] | 610 | End Function
|
---|
| 611 |
|
---|
[409] | 612 | /*!
|
---|
| 613 | @brief FILETIME構造体に変換する
|
---|
| 614 | @return FILETIME構造体
|
---|
| 615 | */
|
---|
[36] | 616 | Function ToFileTime() As FILETIME
|
---|
[209] | 617 | Dim time = getSystemTime() 'As SYSTEMTIME を記述すると内部エラーが発生 rev.207
|
---|
[36] | 618 | Dim fileTime As FILETIME
|
---|
| 619 | SystemTimeToFileTime(time, fileTime)
|
---|
| 620 | Return fileTime
|
---|
| 621 | End Function
|
---|
| 622 |
|
---|
[409] | 623 | /*!
|
---|
| 624 | @brief UTC時刻を表すFILETIME構造体からDateTimeを作成する
|
---|
| 625 | @return DateTimeクラス
|
---|
| 626 | */
|
---|
[209] | 627 | Static Function FromFileTimeUtc(ByRef fileTime As FILETIME) As DateTime
|
---|
[36] | 628 | Dim time As SYSTEMTIME
|
---|
| 629 | FileTimeToSystemTime(fileTime, time)
|
---|
[209] | 630 | Return New DateTime(time, DateTimeKind.Utc)
|
---|
[36] | 631 | End Function
|
---|
| 632 |
|
---|
[409] | 633 | /*!
|
---|
| 634 | @brief UTC時刻のFILETIME構造体に変換する
|
---|
| 635 | @return FILETIME構造体
|
---|
| 636 | */
|
---|
[36] | 637 | Function ToFileTimeUtc() As FILETIME
|
---|
[209] | 638 | Dim fileTime = ToFileTime() 'As FILETIME を記述すると内部エラー rev.207
|
---|
[103] | 639 | If Kind = DateTimeKind.Utc Then
|
---|
[209] | 640 | Return fileTime
|
---|
[36] | 641 | Else
|
---|
[209] | 642 | Dim utcTime As FILETIME
|
---|
| 643 | LocalFileTimeToFileTime(fileTime, utcTime)
|
---|
| 644 | Return utcTime
|
---|
[36] | 645 | End If
|
---|
| 646 | End Function
|
---|
| 647 |
|
---|
[409] | 648 | /*!
|
---|
| 649 | @brief 現地時刻に変換する
|
---|
| 650 | @return 現地時刻に変換したDateTime
|
---|
| 651 | */
|
---|
[36] | 652 | Function ToLocalTime() As DateTime
|
---|
[103] | 653 | If Kind = DateTimeKind.Local Then
|
---|
[209] | 654 | Return New DateTime(This)
|
---|
[82] | 655 | Else
|
---|
[209] | 656 | Dim fileTime = ToFileTime() '直接入れると計算できなくなります。 rev.207
|
---|
| 657 | Return DateTime.FromFileTime(fileTime)
|
---|
[81] | 658 | End If
|
---|
[36] | 659 | End Function
|
---|
[81] | 660 |
|
---|
[409] | 661 | /*!
|
---|
| 662 | @brief このインスタンスを文字列で取得する
|
---|
| 663 | @return 文字列
|
---|
| 664 | */
|
---|
[228] | 665 | Override Function ToString() As String
|
---|
| 666 | Return GetDateTimeFormats()
|
---|
| 667 | End Function
|
---|
| 668 |
|
---|
[409] | 669 | /*!
|
---|
| 670 | @brief 世界協定時刻(UTC)に変換する
|
---|
| 671 | @return 世界協定時刻(UTC)に変換したDateTime
|
---|
| 672 | */
|
---|
[81] | 673 | Function ToUniversalTime() As DateTime
|
---|
[103] | 674 | If Kind = DateTimeKind.Utc Then
|
---|
[209] | 675 | Return New DateTime(m_Date)
|
---|
[82] | 676 | Else
|
---|
[209] | 677 | Dim fileTime = ToFileTimeUtc() '直接入れると計算できなくなります。 rev.207
|
---|
| 678 | Return DateTime.FromFileTimeUtc(fileTime)
|
---|
[81] | 679 | End If
|
---|
| 680 | End Function
|
---|
[209] | 681 |
|
---|
[409] | 682 | '----------------------------------------------------------------
|
---|
| 683 | ' プライベート メソッド
|
---|
| 684 | '----------------------------------------------------------------
|
---|
[36] | 685 | Private
|
---|
[409] | 686 |
|
---|
| 687 | /*!
|
---|
| 688 | @brief インスタンスを初期化する
|
---|
| 689 | @param 時刻(100ナノ秒単位)
|
---|
| 690 | @param 時刻の種類
|
---|
| 691 | */
|
---|
[209] | 692 | Sub initialize(ticks As Int64, kind As DateTimeKind)
|
---|
| 693 | Kind = kind
|
---|
| 694 | Ticks = ticks
|
---|
| 695 | End Sub
|
---|
| 696 |
|
---|
[409] | 697 | /*!
|
---|
| 698 | @brief インスタンスを初期化する
|
---|
| 699 | @param 西暦
|
---|
| 700 | @param 月
|
---|
| 701 | @param 日
|
---|
| 702 | @param 時
|
---|
| 703 | @param 分
|
---|
| 704 | @param 秒
|
---|
| 705 | @param ミリ秒
|
---|
| 706 | @param 時刻の種類
|
---|
| 707 | */
|
---|
[209] | 708 | Sub initialize(year As Long, month As Long, day As Long, hour As Long, minute As Long, second As Long, millisecond As Long, kind As DateTimeKind)
|
---|
[409] | 709 | If month < 1 Or month > 12 Then
|
---|
| 710 | Throw New ArgumentOutOfRangeException("DateTime.initialize: One or more arguments are out of range value.", "month")
|
---|
[209] | 711 | End If
|
---|
[409] | 712 | If day < 1 Or day > DaysInMonth(year, month) Then
|
---|
| 713 | Throw New ArgumentOutOfRangeException("DateTime.initialize: One or more arguments are out of range value.", "day")
|
---|
| 714 | End If
|
---|
| 715 | If hour < 0 Or hour => 24 Then
|
---|
| 716 | Throw New ArgumentOutOfRangeException("DateTime.initialize: One or more arguments are out of range value.", "hour")
|
---|
| 717 | End If
|
---|
| 718 | If minute < 0 Or minute => 60 Then
|
---|
| 719 | Throw New ArgumentOutOfRangeException("DateTime.initialize: One or more arguments are out of range value.", "minute")
|
---|
| 720 | End If
|
---|
| 721 | If second < 0 Or second => 60 Then
|
---|
| 722 | Throw New ArgumentOutOfRangeException("DateTime.initialize: One or more arguments are out of range value.", "second")
|
---|
| 723 | End If
|
---|
| 724 | If millisecond < 0 Or millisecond => 1000 Then
|
---|
| 725 | Throw New ArgumentOutOfRangeException("DateTime.initialize: One or more arguments are out of range value.", "millisecond")
|
---|
| 726 | End If
|
---|
[209] | 727 |
|
---|
| 728 | initialize(
|
---|
[263] | 729 | yearToDay(year) * TimeSpan.TicksPerDay _
|
---|
| 730 | + totalDaysOfMonth(year, month - 1) * TimeSpan.TicksPerDay _
|
---|
[209] | 731 | + (day - 1) * TimeSpan.TicksPerDay _
|
---|
| 732 | + hour * TimeSpan.TicksPerHour _
|
---|
| 733 | + minute * TimeSpan.TicksPerMinute _
|
---|
| 734 | + second * TimeSpan.TicksPerSecond _
|
---|
| 735 | + millisecond * TimeSpan.TicksPerMillisecond,
|
---|
| 736 | kind
|
---|
| 737 | )
|
---|
| 738 | End Sub
|
---|
| 739 |
|
---|
[409] | 740 | /*!
|
---|
| 741 | @brief 時刻を設定する
|
---|
| 742 | @param 時刻(100ナノ秒単位)
|
---|
| 743 | */
|
---|
[36] | 744 | Sub Ticks(value As Int64)
|
---|
[209] | 745 | If value < MinValue Or value > MaxValue Then
|
---|
[409] | 746 | Throw New ArgumentOutOfRangeException("DateTime.value: One or more arguments are out of range value.", "value")
|
---|
[209] | 747 | End If
|
---|
| 748 |
|
---|
| 749 | Dim temp = Kind As DateTimeKind
|
---|
[36] | 750 | m_Date = value
|
---|
[209] | 751 | Kind = temp
|
---|
[36] | 752 | End Sub
|
---|
| 753 |
|
---|
[409] | 754 | /*!
|
---|
| 755 | @brief 時刻の種類を設定する
|
---|
| 756 | @param 時刻の種類
|
---|
| 757 | */
|
---|
[36] | 758 | Sub Kind(kind As DateTimeKind)
|
---|
| 759 | Dim temp As Int64
|
---|
| 760 | temp = kind
|
---|
| 761 | temp = (temp << 62) And &HC000000000000000
|
---|
| 762 | m_Date = (m_Date And &H3FFFFFFFFFFFFFFF) Or temp
|
---|
| 763 | End Sub
|
---|
| 764 |
|
---|
[409] | 765 | /*!
|
---|
| 766 | @brief バイナリデータから時刻の種類を取得する
|
---|
| 767 | @return 時刻の種類
|
---|
| 768 | */
|
---|
[209] | 769 | Static Function kindFromBinary(date As Int64) As DateTimeKind
|
---|
| 770 | date = (date >> 62) And &H03
|
---|
| 771 | If date = &H01 Then
|
---|
[103] | 772 | Return DateTimeKind.Local
|
---|
[209] | 773 | ElseIf date = &H02 Then
|
---|
[103] | 774 | Return DateTimeKind.Unspecified
|
---|
[209] | 775 | ElseIf date = &H03 Then
|
---|
[103] | 776 | Return DateTimeKind.Utc
|
---|
[36] | 777 | End If
|
---|
| 778 | End Function
|
---|
[209] | 779 |
|
---|
[409] | 780 | /*!
|
---|
| 781 | @brief インスタンスをSYSTEMTIME構造体に変換する
|
---|
| 782 | @return SYSTEMTIME構造体
|
---|
| 783 | */
|
---|
[209] | 784 | Function getSystemTime() As SYSTEMTIME
|
---|
[409] | 785 | Dim dayOfWeek = DayOfWeek As Long
|
---|
[209] | 786 | Dim time As SYSTEMTIME
|
---|
| 787 | With time
|
---|
| 788 | .wYear = Year As Word
|
---|
| 789 | .wMonth = Month As Word
|
---|
[409] | 790 | .wDayOfWeek = dayOfWeek As Word
|
---|
[209] | 791 | .wDay = Day As Word
|
---|
| 792 | .wHour = Hour As Word
|
---|
| 793 | .wMinute = Minute As Word
|
---|
| 794 | .wSecond = Second As Word
|
---|
| 795 | .wMilliseconds = Millisecond As Word
|
---|
| 796 | End With
|
---|
| 797 | Return time
|
---|
| 798 | End Function
|
---|
| 799 |
|
---|
[409] | 800 | /*!
|
---|
| 801 | @brief 西暦から日数に変換する
|
---|
| 802 | @return 日数
|
---|
| 803 | */
|
---|
[263] | 804 | Static Function yearToDay(year As Long) As Long
|
---|
| 805 | year--
|
---|
[209] | 806 | Return (Int(year * 365.25) - Int(year * 0.01) + Int(year * 0.0025))
|
---|
| 807 | End Function
|
---|
| 808 |
|
---|
[409] | 809 | /*!
|
---|
| 810 | @brief その月までその年の元旦から何日経っているかを取得する
|
---|
| 811 | @return 日数
|
---|
| 812 | */
|
---|
[263] | 813 | Static Function totalDaysOfMonth(year As Long, month As Long) As Long
|
---|
[209] | 814 | Dim days As Long
|
---|
| 815 | Dim i As Long
|
---|
| 816 | For i = 1 To month
|
---|
| 817 | days += DaysInMonth(year, i)
|
---|
| 818 | Next
|
---|
| 819 | Return days
|
---|
| 820 | End Function
|
---|
[36] | 821 | End Class
|
---|
| 822 |
|
---|
[275] | 823 | End Namespace
|
---|