source: trunk/ab5.0/ablib/src/Classes/System/TimeSpan.ab@ 531

Last change on this file since 531 was 526, checked in by OverTaker, 16 years ago

List,TimeSpan.ToString()実装。
Console.WriteのObject型のオーバーロードを追加。

File size: 5.2 KB
Line 
1Namespace System
2
3
4Class TimeSpan
5 m_Time As Int64
6Public
7 Static MaxValue = 3162240000000000000 As Int64
8 Static MinValue = -3162240000000000000 As Int64
9 Static TicksPerDay = 864000000000 As Int64
10 Static TicksPerHour = 36000000000 As Int64
11 Static TicksPerMinute = 600000000 As Int64
12 Static TicksPerSecond = 10000000 As Int64
13 Static TicksPerMillisecond = 10000 As Int64
14 Static Zero = 0 As Int64
15
16 Sub TimeSpan(ticks As Int64)
17 If (ticks > MaxValue) Or (ticks < MinValue) Then
18 'ArgumentOutOfRangeException
19 debug
20 Exit Sub
21 End If
22 m_Time = ticks
23 End Sub
24
25 Sub TimeSpan(ts As TimeSpan)
26 m_Time = ts.m_Time
27 End Sub
28
29 Sub ~TimeSpan()
30 End Sub
31
32 Function Operator+ (ts As TimeSpan) As TimeSpan
33 Return FromTicks(m_Time + ts.m_Time)
34 End Function
35
36 Function Operator- (ts As TimeSpan) As TimeSpan
37 Return FromTicks(m_Time - ts.m_Time)
38 End Function
39
40 Function Operator* (value As Long) As TimeSpan
41 Return FromTicks(m_Time * value)
42 End Function
43
44 Function Operator/ (value As Long) As TimeSpan
45 Return FromTicks(m_Time \ value)
46 End Function
47
48 Function Operator== (ts As TimeSpan) As Boolean
49 Return Equals(ts)
50 End Function
51
52 Function Operator<> (ts As TimeSpan) As Boolean
53 Return Not Equals(ts)
54 End Function
55
56 Function Operator> (ts As TimeSpan) As Boolean
57 If CompareTo(ts) > 0 Then
58 Return True
59 Else
60 Return False
61 End If
62 End Function
63
64 Function Operator< (ts As TimeSpan) As Boolean
65 If CompareTo(ts) < 0 Then
66 Return True
67 Else
68 Return False
69 End If
70 End Function
71
72 Function Operator>= (ts As TimeSpan) As Boolean
73 If CompareTo(ts) => 0 Then
74 Return True
75 Else
76 Return False
77 End If
78 End Function
79
80 Function Operator<= (ts As TimeSpan) As Boolean
81 If CompareTo(ts) <= 0 Then
82 Return True
83 Else
84 Return False
85 End If
86 End Function
87
88 'Public Properties
89 Function Ticks() As Int64
90 Return m_Time
91 End Function
92
93 Function Milliseconds() As Long
94 Return (m_Time \ TicksPerMillisecond Mod 1000) As Long
95 End Function
96
97 Function Seconds() As Long
98 Return (m_Time \ TicksPerSecond Mod 60) As Long
99 End Function
100
101 Function Minutes() As Long
102 Return (m_Time \ TicksPerMinute Mod 60) As Long
103 End Function
104
105 Function Hours() As Long
106 Return (m_Time \ TicksPerHour Mod 24) As Long
107 End Function
108
109 Function Days() As Long
110 Return (m_Time \ TicksPerDay) As Long
111 End Function
112
113 Function TotalMilliseconds() As Double
114 Return m_Time / TicksPerMillisecond
115 End Function
116
117 Function TotalSeconds() As Double
118 Return m_Time / TicksPerSecond
119 End Function
120
121 Function TotalMinute() As Double
122 Return m_Time / TicksPerMinute
123 End Function
124
125 Function TotalHours() As Double
126 Return m_Time / TicksPerHour
127 End Function
128
129 Function TotalDays() As Double
130 Return m_Time / TicksPerDay
131 End Function
132
133 Function Add(ts As TimeSpan) As TimeSpan
134 Return FromTicks(m_Time + ts.m_Time)
135 End Function
136
137 Static Function Compare(ts1 As TimeSpan, ts2 As TimeSpan) As Long
138 If ts1.m_Time < ts2.m_Time Then
139 Return -1
140 ElseIf ts1.m_Time = ts2.m_Time Then
141 Return 0
142 ElseIf ts1.m_Time > ts2.m_Time Then
143 Return 1
144 End If
145 End Function
146
147 Function CompareTo(ts As TimeSpan) As Long
148 Return (m_Time - ts.m_Time) As Long
149 End Function
150
151 Function Duration() As TimeSpan
152 Return FromTicks(System.Math.Abs(m_Time))
153 End Function
154
155 Function Equals(ts As TimeSpan) As Boolean
156 Return Equals(This, ts)
157 End Function
158
159 Static Function Equals(ts1 As TimeSpan, ts2 As TimeSpan) As Boolean
160 If ts1.m_Time = ts2.m_Time Then
161 Return True
162 Else
163 Return False
164 End If
165 End Function
166
167 Override Function ToString() As String
168 If Ticks < 0 Then
169 Dim span = Negate() As TimeSpan
170 Return ActiveBasic.Strings.SPrintf("-%d:%.2d:%.2d:%.2d.%.4d",
171 New Int32(span.Days) ,New Int32(span.Hours), New Int32(span.Minutes), New Int32(span.Seconds), New Int32(span.Milliseconds))
172 Else
173 Return ActiveBasic.Strings.SPrintf("%d:%.2d:%.2d:%.2d.%.4d",
174 New Int32(Days) ,New Int32(Hours), New Int32(Minutes), New Int32(Seconds), New Int32(Milliseconds))
175 End If
176 End Function
177
178 Static Function FromTicks(ticks As Int64) As TimeSpan
179 If (ticks > MaxValue) Or (ticks < MinValue) Then
180 'ArgumentOutOfRangeException
181 debug
182 Exit Function
183 End If
184 Return New TimeSpan( ticks )
185 End Function
186
187 Static Function FromMilliseconds(value As Double) As TimeSpan
188 Return FromTicks((value * TicksPerMillisecond) As Int64)
189 End Function
190
191 Static Function FromSeconds(value As Double) As TimeSpan
192 Return FromTicks((value * TicksPerSecond)As Int64)
193 End Function
194
195 Static Function FromMinutes(value As Double) As TimeSpan
196 Return FromTicks((value * TicksPerMinute) As Int64)
197 End Function
198
199 Static Function FromHours(value As Double) As TimeSpan
200 Return FromTicks((value * TicksPerHour) As Int64)
201 End Function
202
203 Static Function FromDays(value As Double) As TimeSpan
204 Return FromTicks((value * TicksPerDay) As Int64)
205 End Function
206
207 Function Negate() As TimeSpan
208 Return FromTicks(m_Time * (-1))
209 End Function
210
211 Function Subtract(ts As TimeSpan) As TimeSpan
212 Return FromTicks(m_Time - ts.m_Time)
213 End Function
214End Class
215
216
217End Namespace
Note: See TracBrowser for help on using the repository browser.