Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TimeSpan.cs « System « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e9976a3173da7bcf9c51e436aa5642ab0eb8f66e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
//
// System.TimeSpan.cs
//
// author:
//   Duco Fijma (duco@lorentz.xs4all.nl)
//
//   (C) 2001 Duco Fijma
//

using System.Globalization;

namespace System {

[Serializable]
public struct TimeSpan :  IComparable  {

	private long _ticks;

	public TimeSpan (long value) { _ticks = value; }
	public TimeSpan (int hours, int minutes, int seconds) 
		: this(false, 0, hours, minutes, seconds, 0, 0) {}
	public TimeSpan (int days, int hours, int minutes, int seconds) 
		: this(false, days, hours, minutes, seconds, 0, 0) {}
	public TimeSpan (int days, int hours, int minutes, int seconds, int milliseconds)
		: this(false, days, hours, minutes, seconds, milliseconds, 0) {}

	internal TimeSpan (bool sign, int days, int hours, int minutes, int seconds, int milliseconds, long ticks)
	{
		checked {
			_ticks = TicksPerDay * days + 
				TicksPerHour * hours +
				TicksPerMinute * minutes +
				TicksPerSecond * seconds +
				TicksPerMillisecond * milliseconds +
				ticks;
			if ( sign ) {
				_ticks = -_ticks;
			}
		}
	}
	
	public static readonly TimeSpan MaxValue = new TimeSpan (long.MaxValue);
	public static readonly TimeSpan MinValue = new TimeSpan (long.MinValue);
	public const long TicksPerDay = 864000000000L;
	public const long TicksPerHour = 36000000000L;
	public const long TicksPerMillisecond = 10000L;
	public const long TicksPerMinute = 600000000L;
	public const long TicksPerSecond = 10000000L;
	public static readonly TimeSpan Zero = new TimeSpan (0L);

	public int Days
	{
		get {
			return (int) TotalDays;
		}
	}

	public int Hours
	{
		get {
			return (int) (_ticks % TicksPerDay / TicksPerHour);
		}
	}

	public int Milliseconds
	{
		get
		{
			return (int) (_ticks % TicksPerSecond / TicksPerMillisecond);
		}
	}

	public int Minutes
	{
		get
		{
			return (int) (_ticks % TicksPerHour / TicksPerMinute);
		}
	}

	public int Seconds
	{
		get
		{
			return (int) (_ticks % TicksPerMinute / TicksPerSecond);
		}
	}

	public long Ticks
	{ 
		get
		{
			return _ticks;
		}
	}

	public double TotalDays
	{
		get
		{
			return (double) _ticks / TicksPerDay;
		}
	}

	public double TotalHours
	{
		get
		{
			return (double) _ticks / TicksPerHour;
		}
	}

	public double TotalMilliseconds
	{
		get
		{
			return (double) _ticks  / TicksPerMillisecond;
		}
	}

	public double TotalMinutes
	{
		get {
			return (double) _ticks / TicksPerMinute;
		}
	}

	public double TotalSeconds
	{
		get {
			return (double) _ticks / TicksPerSecond;
		}
	}

	public TimeSpan Add (TimeSpan ts)
	{
		checked {
			return new TimeSpan (_ticks + ts.Ticks);
		}
	}

	public static int Compare (TimeSpan t1, TimeSpan t2)
	{
		if (t1._ticks < t2._ticks) {
			return -1;
		}
		else if (t1._ticks > t2._ticks) {
			return 1;
		}
		else {
			return 0;
		}
	}

	public int CompareTo (object value)
	{
		if (value == null )
			return 1;

		if (!(value is TimeSpan)) {
			throw new ArgumentException (Locale.GetText (
				"Argument of System.TimeSpan.CompareTo should be a TimeSpan"));
		}
	
		return Compare(this, (TimeSpan) value);
	}

	public TimeSpan Duration ()
	{
		checked {
			return new TimeSpan (Math.Abs (_ticks));
		}
	}

	public override bool Equals (object value)
	{
		if (!(value is TimeSpan)) {
			return false;
		}
		return Equals (this, (TimeSpan) value);
	}

	public static bool Equals (TimeSpan t1, TimeSpan t2)
	{
		return t1._ticks == t2._ticks;
	}

	// Implementing FromDays -> FromHours -> FromMinutes -> FromSeconds ->
	// FromMilliseconds as done here is probably not the most efficient
	// way. 
	public static TimeSpan FromDays (double value)
	{
		if (Double.IsNaN (value) || Double.IsNegativeInfinity (value)) {
			return MinValue;
		}

		if (Double.IsPositiveInfinity (value)) {
			return MaxValue;
		}

		return new TimeSpan ((int) value,0,0,0,0) + FromHours ((value - ((int) value)) * 24);
	}

	public static TimeSpan FromHours (double value)
	{
		if (Double.IsNaN (value) || Double.IsNegativeInfinity (value)) {
			return MinValue;
		}

		if (Double.IsPositiveInfinity (value)) {
			return MaxValue;
		}

		return new TimeSpan ((int) value,0,0) + FromMinutes ((value - ((int) value)) * 60);
	}

	public static TimeSpan FromMinutes (double value)
	{
		if (Double.IsNaN (value) || Double.IsNegativeInfinity (value)) {
			return MinValue;
		}

		if (Double.IsPositiveInfinity (value)) {
			return MaxValue;
		}

		return new TimeSpan (0, (int) value, 0) + FromSeconds((value - ((int) value)) * 60);
	}

	public static TimeSpan FromSeconds (double value)
	{
		if (Double.IsNaN (value) || Double.IsNegativeInfinity (value)) {
			return MinValue;
		}

		if (Double.IsPositiveInfinity (value)) {
			return MaxValue;
		}

		return new TimeSpan (0, 0, 0, (int) value) + FromMilliseconds((value - ((int) value)) * 1000);

	}

	public static TimeSpan FromMilliseconds (double value)
	{
		if (Double.IsNaN (value) || Double.IsNegativeInfinity (value)) {
			return MinValue;
		}

		if (Double.IsPositiveInfinity (value)) {
			return MaxValue;
		}

		return new TimeSpan (0, 0, 0, 0, (int) value);
	}

	public static TimeSpan FromTicks (long value)
	{
		return new TimeSpan (value);
	}

	public override int GetHashCode ()
	{
		return _ticks.GetHashCode ();
	}

	public TimeSpan Negate ()
	{
		checked {
			return new TimeSpan (-_ticks);
		}
	}

	public static TimeSpan Parse (string s)
	{
		if (s == null) {
			throw new ArgumentNullException (
				Locale.GetText ("null reference passed to TimeSpan.Parse"));
		}

		Parser p = new Parser (s); 
		return p.Execute ();
	}

	public TimeSpan Subtract (TimeSpan ts)
	{
		checked {
			return new TimeSpan (_ticks - ts.Ticks);
		}
	}

	public override string ToString ()
	{
		string res = "";	

		if (_ticks < 0) {
			res += "-";
		}

		// We need to take absolute values of all components.
		// Can't handle negative timespans by negating the TimeSpan
		// as a whole. This would lead to an overflow for the 
		// degenerate case "TimeSpan.MinValue.ToString()".
		if (Days != 0) {
			res += Math.Abs (Days) + "." ;
		}

		res += string.Format ("{0:D2}:{1:D2}:{2:D2}", Math.Abs(Hours), Math.Abs(Minutes), Math.Abs(Seconds));

		int fractional = (int) Math.Abs (_ticks % TicksPerSecond);
		if (fractional != 0) {
			res += string.Format (".{0:D7}", fractional);
		}
 
		return res;
	}

	public static TimeSpan operator + (TimeSpan t1, TimeSpan t2)
	{
		return t1.Add (t2);
	}

	public static bool operator == (TimeSpan t1, TimeSpan t2)
	{
		return Compare (t1, t2) == 0;
	}

	public static bool operator > (TimeSpan t1, TimeSpan t2)
	{
		return Compare (t1, t2) == 1;
	}

	public static bool operator >= (TimeSpan t1, TimeSpan t2)
	{
		return Compare (t1, t2) != -1;
	}

	public static bool operator != (TimeSpan t1, TimeSpan t2)
	{
		return Compare (t1, t2) != 0;
	}

	public static bool operator < (TimeSpan t1, TimeSpan t2)
	{
		return Compare (t1, t2) == -1;
	}

	public static bool operator <= (TimeSpan t1, TimeSpan t2)
	{
		return Compare (t1, t2) != 1;
	}

	public static TimeSpan operator - (TimeSpan t1, TimeSpan t2)
	{
		return t1.Subtract (t2);
	}

	public static TimeSpan operator - (TimeSpan t)
	{
		return t.Negate ();
	}

	public static TimeSpan operator + (TimeSpan t)
	{
		return t;
	}

// Class Parser implements simple parser for TimeSpan::Parse
internal class Parser {

	private string _src;
	private int _cur;
	private int _length;

	public Parser (string src)
	{
		_src = src;
		Reset ();
	}

	public void Reset ()
	{
		_cur = 0;
		_length = _src.Length;
	}

	public bool AtEnd
	{
		get {
			return _cur >= _length;
		}
	}

	private void ThrowFormatException() 
	{
		throw new FormatException (Locale.GetText ("Invalid format for TimeSpan.Parse"));
	}

	// All "Parse" functions throw a FormatException on syntax error.
	// Their return value is semantic value of the item parsed.

	// Range checking is spread over three different places:
	// 1) When parsing "int" values, an exception is thrown immediately
	//    when the value parsed exceeds the maximum value for an int.
	// 2) An explicit check is built in that checks for hours > 23 and
	//    for minutes and seconds > 59.
	// 3) Throwing an exceptions for a final TimeSpan value > MaxValue
	//    or < MinValue is left to the TimeSpan constructor called.

	// Parse zero or more whitespace chars.
	private void ParseWhiteSpace ()
	{
		while (!AtEnd && Char.IsWhiteSpace (_src, _cur)) {
			_cur++; 
		}
	}

	// Parse optional sign character.
	private bool ParseSign () 
	{
		bool res = false;

		if (!AtEnd && _src[_cur] == '-') { 
			res = true;
			_cur++;
		}

		return res;
	}

	// Parse simple int value
	private int ParseInt ()
	{
		int res = 0;
		int count = 0;

		while (!AtEnd && Char.IsDigit (_src, _cur)) {
			checked {
				res = res*10 + _src[_cur] - '0';
			}
			_cur++;
			count++;
		}
		
		if (count == 0) {
			ThrowFormatException ();
		}

		return res;
	}

	// Parse optional dot
	private bool ParseOptDot ()
	{
		if (AtEnd) {
			return false;
		}
		
		if (_src[_cur] == '.') {
			_cur++;
			return true;
		}
		else {
			return false;
		}
	}	
	
	// Parse NON-optional colon 
	private void ParseColon ()
	{
		if (!AtEnd && _src[_cur] == ':') {
			_cur++;
		}
		else {
			ThrowFormatException ();
		}
	}

	// Parse [1..7] digits, representing fractional seconds (ticks)
	private long ParseTicks ()
	{
		long mag = 1000000;
		long res = 0;
		bool digitseen = false;
		
		while ( mag > 0 && !AtEnd && Char.IsDigit (_src, _cur) ) {
			res = res + (_src[_cur] - '0') * mag;
			_cur++;
			mag = mag / 10;
			digitseen = true;
		}

		if (!digitseen) {
			ThrowFormatException ();
		}

		return res;
	}

	public TimeSpan Execute ()
	{
		bool sign;
		int days;
		int hours;
		int minutes;
		int seconds;
		long ticks;

		// Parse [ws][dd.]hh:mm:ss[.ff][ws]
		ParseWhiteSpace ();
		sign = ParseSign ();
		days = ParseInt ();
		if (ParseOptDot ()) {
			hours = ParseInt ();
		}
		else {
			hours = days;
			days = 0;
		}
		ParseColon();
		minutes = ParseInt ();
		ParseColon();
		seconds = ParseInt ();
		if ( ParseOptDot () ) {
			ticks = ParseTicks ();
		}	
		else {
			ticks = 0;
		}
		ParseWhiteSpace ();

		if ( !AtEnd ) {
			ThrowFormatException ();
		}

		if ( hours > 23 || minutes > 59 || seconds > 59 ) {
			throw new OverflowException (Locale.GetText (
				"Value outside range in TimeSpan.Parse" ));
		}

		TimeSpan ts = new TimeSpan (sign, days, hours, minutes, seconds, 0, ticks);

		return ts;
	}	

}

} /* TimeSpan */
}