Ignore:
Timestamp:
Feb 23, 2008, 11:52:55 AM (16 years ago)
Author:
OverTaker
Message:

DriveInfoのコンストラクタの間違い修整。他、コメント、例外追加。

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Include/Classes/System/DateTime.ab

    r400 r409  
    11Namespace System
    22
     3/*!
     4    @brief  時刻の種類
     5*/
     6Enum DateTimeKind
     7    Local
     8    Unspecified
     9    Utc
     10End Enum
     11
     12/*!
     13    @brief  曜日
     14*/
     15Enum DayOfWeek
     16    Sunday = 0
     17    Monday
     18    Tuesday
     19    Wednesday
     20    Thursday
     21    Friday
     22    Saturday
     23End Enum
     24
     25
     26/*!
     27    @brief  時刻を表すクラス
     28*/
    329
    430Class DateTime
    531    m_Date As Int64
    632Public
    7     Static MaxValue = 3162240000000000000 As Int64 'Const TicksPerDay*366*10000
    8     Static MinValue = 316224000000000 As Int64     'Const TicksPerDay*366
    9 
     33    Static Const MaxValue = 3162240000000000000 As Int64 'Const TicksPerDay*366*10000
     34    Static Const MinValue = 316224000000000 As Int64     'Const TicksPerDay*366
     35
     36    '----------------------------------------------------------------
     37    ' パブリック コンストラクタ
     38    '----------------------------------------------------------------
     39
     40    /*!
     41    @brief  コンストラクタ
     42    */
    1043    Sub DateTime()
    1144        initialize(MinValue, DateTimeKind.Unspecified)
    1245    End Sub
    1346
     47    /*!
     48    @brief  時刻を指定して初期化する
     49    @param  100ナノ秒単位で表した時刻
     50    */
    1451    Sub DateTime(ticks As Int64)
    1552        initialize(ticks, DateTimeKind.Unspecified)
    1653    End Sub
    1754
     55    /*!
     56    @brief  時刻を指定して初期化する
     57    @param  100ナノ秒単位で表した時刻
     58    @param  時刻の種類
     59    */
    1860    Sub DateTime(ticks As Int64, kind As DateTimeKind)
    1961        initialize(ticks, kind)
    2062    End Sub
    2163
     64    /*!
     65    @brief  時刻を指定して初期化する
     66    @param  西暦
     67    @param  月
     68    @param  日
     69    */
    2270    Sub DateTime(year As Long, month As Long, day As Long)
    2371        initialize(year, month, day, 0, 0, 0, 0, DateTimeKind.Unspecified)
    2472    End Sub
    2573
     74    /*!
     75    @brief  時刻を指定して初期化する
     76    @param  西暦
     77    @param  月
     78    @param  日
     79    @param  時刻の種類
     80    */
    2681    Sub DateTime(year As Long, month As Long, day As Long, kind As DateTimeKind)
    2782        initialize(year, month, day, 0, 0, 0, 0, kind)
    2883    End Sub
    2984
     85    /*!
     86    @brief  時刻を指定して初期化する
     87    @param  西暦
     88    @param  月
     89    @param  日
     90    @param  時
     91    @param  分
     92    @param  秒
     93    */
    3094    Sub DateTime(year As Long, month As Long, day As Long, hour As Long, minute As Long, second As Long)
    3195        initialize(year, month, day, hour, minute, second, 0, DateTimeKind.Unspecified)
    3296    End Sub
    3397
     98    /*!
     99    @brief  時刻を指定して初期化する
     100    @param  西暦
     101    @param  月
     102    @param  日
     103    @param  時
     104    @param  分
     105    @param  秒
     106    @param  時刻の種類
     107    */
    34108    Sub DateTime(year As Long, month As Long, day As Long, hour As Long, minute As Long, second As Long, kind As DateTimeKind)
    35109        initialize(year, month, day, hour, minute, second, 0, kind)
    36110    End Sub
    37111
     112    /*!
     113    @brief  時刻を指定して初期化する
     114    @param  西暦
     115    @param  月
     116    @param  日
     117    @param  時
     118    @param  分
     119    @param  秒
     120    @param  ミリ秒
     121    */
    38122    Sub DateTime(year As Long, month As Long, day As Long, hour As Long, minute As Long, second As Long, millisecond As Long)
    39123        initialize(year, month, day, hour, minute, second, millisecond, DateTimeKind.Unspecified)
    40124    End Sub
    41125
     126    /*!
     127    @brief  時刻を指定して初期化する
     128    @param  西暦
     129    @param  月
     130    @param  日
     131    @param  時
     132    @param  分
     133    @param  秒
     134    @param  ミリ秒
     135    @param  時刻の種類
     136    */
    42137    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)
    43138        initialize(year, month, day, hour, minute, second, millisecond, kind)
    44139    End Sub
    45140
     141    /*!
     142    @brief  時刻を指定して初期化する
     143    @param  SYSTEMTIME構造体
     144    */
    46145    Sub DateTime(ByRef time As SYSTEMTIME)
    47146        initialize(time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds, DateTimeKind.Unspecified)
    48147    End Sub
    49148
     149    /*!
     150    @brief  時刻を指定して初期化する
     151    @param  SYSTEMTIME構造体
     152    @param  時刻の種類
     153    */
    50154    Sub DateTime(ByRef time As SYSTEMTIME, kind As DateTimeKind)
    51155        initialize(time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds, kind)
    52156    End Sub
    53157
    54     'Copy Constructor
     158    /*!
     159    @brief  コピーコンストラクタ
     160    @param  コピーするDateTime
     161    */
    55162    Sub DateTime(dateTime As DateTime)
    56163        This.m_Date = dateTime.m_Date
    57164    End Sub
    58165
    59     Sub ~DateTime()
    60     End Sub
     166    '----------------------------------------------------------------
     167    ' オペレータ
     168    '----------------------------------------------------------------
    61169
    62170    Function Operator + (value As TimeSpan) As DateTime
     
    112220    End Function
    113221   
    114     'Public Properties
     222    '----------------------------------------------------------------
     223    ' パブリック プロパティ
     224    '----------------------------------------------------------------
     225
     226    /*!
     227    @brief  時刻を100ナノ秒単位で取得する
     228    @return 時刻
     229    */
    115230    Function Ticks() As Int64
    116231        Return (m_Date And &H3FFFFFFFFFFFFFFF)
    117232    End Function
    118233
     234    /*!
     235    @brief  時刻のミリ秒単位部分を取得する
     236    @return ミリ秒の部分
     237    */
    119238    Function Millisecond() As Long
    120239        Return (Ticks \ TimeSpan.TicksPerMillisecond Mod 1000) As Long
    121240    End Function
    122241
     242    /*!
     243    @brief  時刻の秒単位部分を取得する
     244    @return 秒の部分
     245    */
    123246    Function Second() As Long
    124247        Return (Ticks \ TimeSpan.TicksPerSecond Mod 60) As Long
    125248    End Function
    126249
     250    /*!
     251    @brief  時刻の分単位部分を取得する
     252    @return 分の部分
     253    */
    127254    Function Minute() As Long
    128255        Return (Ticks \ TimeSpan.TicksPerMinute Mod 60) As Long
    129256    End Function
    130257
     258    /*!
     259    @brief  時刻の時単位部分を取得する
     260    @return 時の部分
     261    */
    131262    Function Hour() As Long
    132263        Return (Ticks \ TimeSpan.TicksPerHour Mod 24) As Long
    133264    End Function
    134265
     266    /*!
     267    @brief  時刻の日単位部分を取得する
     268    @return 日の部分
     269    */
    135270    Function Day() As Long
    136271        Return DayOfYear - totalDaysOfMonth(Year, Month - 1)
    137272    End Function
    138273
     274    /*!
     275    @brief  時刻の月単位部分を取得する
     276    @return 月の部分
     277    */
    139278    Function Month() As Long
    140279        Dim year = Year As Long
     
    147286    End Function
    148287
     288    /*!
     289    @brief  時刻の年単位部分を取得する
     290    @return 西暦
     291    */
    149292    Function Year() As Long
    150293        Dim day = (Ticks \ TimeSpan.TicksPerDay) As Long
     
    157300    End Function
    158301
    159     Function DayOfWeek() As Long 'As DayOfWeek
    160         Return ((Ticks \ TimeSpan.TicksPerDay) Mod 7) As Long + 1
    161     End Function
    162 
     302    /*!
     303    @brief  時刻の曜日部分を取得する
     304    @return 曜日の部分
     305    */
     306    Function DayOfWeek() As DayOfWeek
     307        Return New DayOfWeek( (((Ticks \ TimeSpan.TicksPerDay) Mod 7) + 1) As Long, "DayOfWeek")
     308    End Function
     309
     310    /*!
     311    @brief  時刻の種類を取得する
     312    @return 種類
     313    */
    163314    Function Kind() As DateTimeKind
    164315        Return kindFromBinary(m_Date)
    165316    End Function
    166317
     318    /*!
     319    @brief  その年の何日目かを取得する
     320    @return 日数
     321    */
    167322    Function DayOfYear() As Long
    168323        Return ((Ticks \ TimeSpan.TicksPerDay) - yearToDay(Year) + 1) As Long
    169324    End Function
    170325
     326    /*!
     327    @brief  時刻の日付の部分を取得する
     328    @return 日付
     329    */
    171330    Function Date() As DateTime
    172331        Return New DateTime(Year, Month, Day, Kind)
    173332    End Function
    174333
     334    /*!
     335    @brief  現在の時刻を取得する
     336    @return 時刻
     337    */
    175338    Static Function Now() As DateTime
    176339        Dim time As SYSTEMTIME
     
    179342    End Function
    180343
     344    /*!
     345    @brief  今日を表す時刻を取得する(時間は0時0分)
     346    @return 時刻
     347    */
    181348    Static Function Today() As DateTime
    182349        Dim time As SYSTEMTIME
     
    185352    End Function
    186353
     354    /*!
     355    @brief  現在の時刻をUTC時刻で取得する
     356    @return 時刻
     357    */
    187358    Static Function UtcNow() As DateTime
    188359        Dim time As SYSTEMTIME
     
    191362    End Function
    192363       
    193     'Public Methods
     364    '----------------------------------------------------------------
     365    ' パブリック メソッド
     366    '----------------------------------------------------------------
     367
     368    /*!
     369    @brief  二つの時刻の差を求める
     370    @return 差(単位は100ナノ秒)
     371    */
    194372    Static Function Compare(t1 As DateTime, t2 As DateTime) As Int64
    195373        Return t1.Ticks - t2.Ticks
    196374    End Function
    197375
     376    /*!
     377    @brief  等しいどうかを取得する
     378    @param  比較する値
     379    @retval True  等しい
     380    @retval False 等しくない
     381    */
    198382    Function Equals(value As DateTime) As Boolean
    199383        If value.m_Date = m_Date Then
     
    204388    End Function
    205389
     390    /*!
     391    @brief  等しいどうかを取得する
     392    @param  比較される値
     393    @param  比較する値
     394    @retval True  等しい
     395    @retval False 等しくない
     396    */
    206397    Static Function Equals(t1 As DateTime, t2 As DateTime) As Boolean
    207398        If t1.m_Date = t2.m_Date Then
     
    212403    End Function
    213404
     405    /*!
     406    @brief  ハッシュコードを取得する
     407    @return ハッシュコード
     408    */
    214409    Override Function GetHashCode() As Long
    215410        Return HIDWORD(m_Date) Xor LODWORD(m_Date)
    216411    End Function
    217412
     413    /*!
     414    @brief  時刻を進める
     415    @param  進める時間
     416    @return 進めた後の時刻
     417    */
    218418    Function Add(value As TimeSpan) As DateTime
    219419        Return This + value
    220420    End Function
    221421
     422    /*!
     423    @brief  時刻を進める
     424    @param  進める時間(100ナノ秒単位)
     425    @return 進めた後の時刻
     426    */
    222427    Function AddTicks(value As Int64) As DateTime
    223428        Return New DateTime(Ticks + value, Kind )
    224429    End Function
    225430
     431    /*!
     432    @brief  時刻を進める
     433    @param  進める時間(ミリ秒単位)
     434    @return 進めた後の時刻
     435    */
    226436    Function AddMilliseconds(value As Double) As DateTime
    227437        Return AddTicks((value * TimeSpan.TicksPerMillisecond) As Int64)
    228438    End Function
    229439
     440    /*!
     441    @brief  時刻を進める
     442    @param  進める時間(秒単位)
     443    @return 進めた後の時刻
     444    */
    230445    Function AddSeconds(value As Double) As DateTime
    231446        Return AddTicks((value * TimeSpan.TicksPerSecond) As Int64)
    232447    End Function
    233448
     449    /*!
     450    @brief  時刻を進める
     451    @param  進める時間(分単位)
     452    @return 進めた後の時刻
     453    */
    234454    Function AddMinutes(value As Double) As DateTime
    235455        Return AddTicks((value * TimeSpan.TicksPerMinute) As Int64)
    236456    End Function
    237457
     458    /*!
     459    @brief  時刻を進める
     460    @param  進める時間(時単位)
     461    @return 進めた後の時刻
     462    */
    238463    Function AddHours(value As Double) As DateTime
    239464        Return AddTicks((value * TimeSpan.TicksPerHour) As Int64)
    240465    End Function
    241466
     467    /*!
     468    @brief  時刻を進める
     469    @param  進める時間(日単位)
     470    @return 進めた後の時刻
     471    */
    242472    Function AddDays(value As Double) As DateTime
    243473        Return AddTicks((value * TimeSpan.TicksPerDay) As Int64)
    244474    End Function
    245475
     476    /*!
     477    @brief  時刻を進める
     478    @param  進める時間(年単位)
     479    @return 進めた後の時刻
     480    */
    246481    Function AddYears(value As Double) As DateTime
    247482        Dim date = New DateTime(Year + Int(value), Month, Day, Hour, Minute, Second, Millisecond, Kind)
     
    260495    End Function
    261496
     497    /*!
     498    @brief  時刻の差を取得する
     499    @param  比較する値
     500    @return 時刻の差
     501    */
    262502    Function Subtract(value As DateTime) As TimeSpan
    263503        Return This - value
    264504    End Function
    265505
     506    /*!
     507    @brief  時刻を戻す
     508    @param  戻す時間
     509    @return 時刻
     510    */
    266511    Function Subtract(value As TimeSpan) As DateTime
    267512        Return This - value
    268513    End Function
    269514
     515    /*!
     516    @brief  指定した年月が、その月の何日目かを取得する
     517    @param  西暦
     518    @param  月
     519    @return 日数
     520    */
    270521    Static Function DaysInMonth(year As Long, month As Long) As Long
    271         If year < 1 Or year > 9999 Or month < 1 Or month > 12 Then
    272             'ArgumentOutOfRangeException
    273             debug
     522        If year < 1 Or year > 9999 Then
     523            Throw New ArgumentOutOfRangeException("DateTime.DaysInMonth: One or more arguments are out of range value.", "year")
     524        End If
     525        If month < 1 Or month > 12 Then
     526            Throw New ArgumentOutOfRangeException("DateTime.DaysInMonth: One or more arguments are out of range value.", "month")
    274527        End If
    275528
     
    282535    End Function
    283536
     537    /*!
     538    @brief  年が閏年かどうかを取得する
     539    @param  西暦
     540    @retval True  閏年
     541    @retval False 閏年でない
     542    */
    284543    Static Function IsLeapYear(year As Long) As Boolean
    285544        If (year Mod 400) = 0 Then Return True
     
    289548    End Function
    290549
     550    /*!
     551    @brief  時刻を文字列に変換する
     552    @return 時刻を表した文字列
     553    */
    291554    Function GetDateTimeFormats() As String
    292555        Dim time = getSystemTime() 'As SYSTEMTIME を記述すると内部エラーが発生
     
    298561        dateTimeFormats[dateFormatSize - 1] = &H20 As TCHAR 'Asc(" ") As TCHAR
    299562        GetTimeFormat(LOCALE_USER_DEFAULT, 0, time, NULL, dateTimeFormats + dateFormatSize, timeFormatSize)
    300 'Debug
    301563        Return New String(dateTimeFormats, strLength)
    302564    End Function
    303565
     566    /*!
     567    @brief  時刻を指定した書式で文字列に変換する
     568    @param  書式
     569    @return 時刻を表した文字列
     570    */
    304571    Function GetDateTimeFormats(format As *TCHAR) As String
    305572        Dim time = getSystemTime() 'As SYSTEMTIME を記述すると内部エラーが発生
     
    315582    End Function
    316583
     584    /*!
     585    @brief  バイナリデータからDateTimeを作成する
     586    @return DateTimeクラス
     587    */
    317588    Static Function FromBinary(date As Int64) As DateTime
    318589        Return New DateTime((date And &H3FFFFFFFFFFFFFFF), kindFromBinary(date))
    319590    End Function
    320591
     592    /*!
     593    @brief  バイナリデータに変換する
     594    @return バイナリデータ
     595    */
    321596    Function ToBinary() As Int64
    322597        Return m_Date
    323598    End Function
    324599
     600    /*!
     601    @brief  FILETIME構造体からDateTimeを作成する
     602    @return DateTimeクラス
     603    */
    325604    Static Function FromFileTime(ByRef fileTime As FILETIME) As DateTime
    326605        Dim localTime As FILETIME
     
    331610    End Function
    332611
     612    /*!
     613    @brief  FILETIME構造体に変換する
     614    @return FILETIME構造体
     615    */
    333616    Function ToFileTime() As FILETIME
    334617        Dim time = getSystemTime() 'As SYSTEMTIME を記述すると内部エラーが発生 rev.207
     
    338621    End Function
    339622
     623    /*!
     624    @brief  UTC時刻を表すFILETIME構造体からDateTimeを作成する
     625    @return DateTimeクラス
     626    */
    340627    Static Function FromFileTimeUtc(ByRef fileTime As FILETIME) As DateTime
    341628        Dim time As SYSTEMTIME
     
    344631    End Function
    345632
     633    /*!
     634    @brief  UTC時刻のFILETIME構造体に変換する
     635    @return FILETIME構造体
     636    */
    346637    Function ToFileTimeUtc() As FILETIME
    347638        Dim fileTime = ToFileTime() 'As FILETIME を記述すると内部エラー rev.207
     
    355646    End Function
    356647
     648    /*!
     649    @brief  現地時刻に変換する
     650    @return 現地時刻に変換したDateTime
     651    */
    357652    Function ToLocalTime() As DateTime
    358653        If Kind = DateTimeKind.Local Then
     
    364659    End Function
    365660
     661    /*!
     662    @brief  このインスタンスを文字列で取得する
     663    @return 文字列
     664    */
    366665    Override Function ToString() As String
    367666        Return GetDateTimeFormats()
    368667    End Function
    369668
     669    /*!
     670    @brief  世界協定時刻(UTC)に変換する
     671    @return 世界協定時刻(UTC)に変換したDateTime
     672    */
    370673    Function ToUniversalTime() As DateTime
    371674        If Kind = DateTimeKind.Utc Then
     
    377680    End Function
    378681
     682    '----------------------------------------------------------------
     683    ' プライベート メソッド
     684    '----------------------------------------------------------------
    379685Private
     686
     687    /*!
     688    @brief  インスタンスを初期化する
     689    @param  時刻(100ナノ秒単位)
     690    @param  時刻の種類
     691    */
    380692    Sub initialize(ticks As Int64, kind As DateTimeKind)
    381693        Kind = kind
     
    383695    End Sub
    384696
     697    /*!
     698    @brief  インスタンスを初期化する
     699    @param  西暦
     700    @param  月
     701    @param  日
     702    @param  時
     703    @param  分
     704    @param  秒
     705    @param  ミリ秒
     706    @param  時刻の種類
     707    */
    385708    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)
    386         If month < 1 Or month > 12 _
    387             Or day < 1 Or day > DaysInMonth(year, month) _
    388             Or hour < 0 Or hour => 24 _
    389             Or minute < 0 Or minute => 60 _
    390             Or second < 0 Or second => 60 _
    391             Or millisecond < 0 Or millisecond  => 1000 Then
    392             debug 'ArgumentOutOfRangeException
     709        If month < 1 Or month > 12 Then
     710            Throw New ArgumentOutOfRangeException("DateTime.initialize: One or more arguments are out of range value.", "month")
     711        End If
     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")
    393726        End If
    394727
     
    405738    End Sub
    406739
     740    /*!
     741    @brief  時刻を設定する
     742    @param  時刻(100ナノ秒単位)
     743    */
    407744    Sub Ticks(value As Int64)
    408745        If value < MinValue Or value > MaxValue Then
    409             debug 'ArgumentOutOfRangeException
     746            Throw New ArgumentOutOfRangeException("DateTime.value: One or more arguments are out of range value.", "value")
    410747        End If
    411748
     
    415752    End Sub
    416753
     754    /*!
     755    @brief  時刻の種類を設定する
     756    @param  時刻の種類
     757    */
    417758    Sub Kind(kind As DateTimeKind)
    418759        Dim temp As Int64
     
    422763    End Sub
    423764
     765    /*!
     766    @brief  バイナリデータから時刻の種類を取得する
     767    @return 時刻の種類
     768    */
    424769    Static Function kindFromBinary(date As Int64) As DateTimeKind
    425770        date = (date >> 62) And &H03
     
    431776            Return DateTimeKind.Utc
    432777        End If
    433 
    434         ' ここにはこないはず
    435         debug
    436     End Function
    437 
     778    End Function
     779
     780    /*!
     781    @brief  インスタンスをSYSTEMTIME構造体に変換する
     782    @return SYSTEMTIME構造体
     783    */
    438784    Function getSystemTime() As SYSTEMTIME
     785        Dim dayOfWeek = DayOfWeek As Long
    439786        Dim time As SYSTEMTIME
    440787        With time
    441788            .wYear = Year As Word
    442789            .wMonth = Month As Word
    443             .wDayOfWeek = DayOfWeek As Word
     790            .wDayOfWeek = dayOfWeek As Word
    444791            .wDay = Day As Word
    445792            .wHour = Hour As Word
     
    451798    End Function
    452799
     800    /*!
     801    @brief  西暦から日数に変換する
     802    @return 日数
     803    */
    453804    Static Function yearToDay(year As Long) As Long
    454805        year--
     
    456807    End Function
    457808
     809    /*!
     810    @brief  その月までその年の元旦から何日経っているかを取得する
     811    @return 日数
     812    */
    458813    Static Function totalDaysOfMonth(year As Long, month As Long) As Long
    459814        Dim days As Long
     
    466821End Class
    467822
    468 Enum DateTimeKind
    469     Local
    470     Unspecified
    471     Utc
    472 End Enum
    473 
    474 Enum DayOfWeek
    475     Sunday = 0
    476     Monday
    477     Tuesday
    478     Wednesday
    479     Thursday
    480     Friday
    481     Saturday
    482 End Enum
    483 
    484 
    485823End Namespace
Note: See TracChangeset for help on using the changeset viewer.