This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
Time.cs
1761 lines (1609 loc) · 82.9 KB
/
Time.cs
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
namespace System
{
/// <summary>
/// Represents a time of day, as would be read from a clock, within the range 00:00:00 to 23:59:59.9999999
/// Has properties for working with both 12-hour and 24-hour time values.
/// </summary>
[Serializable]
[XmlSchemaProvider("GetSchema")]
[StructLayout(LayoutKind.Auto)]
public struct Time : IEquatable<Time>, IComparable<Time>, IComparable, IFormattable, IXmlSerializable
{
private const long TicksPerMillisecond = 10000;
private const long TicksPerSecond = TicksPerMillisecond * 1000; // 10,000,000
private const long TicksPerMinute = TicksPerSecond * 60; // 600,000,000
private const long TicksPerHour = TicksPerMinute * 60; // 36,000,000,000
private const long TicksPerDay = TicksPerHour * 24; // 864,000,000,000
private const long MinTicks = 0L;
private const long MaxTicks = 863999999999L;
private static readonly Regex EscapeCharRegex = new Regex(@"\\.|"".*?""|'.*?'", RegexOptions.Compiled);
private static readonly Regex InvalidFormatsRegex = new Regex(@"[dKMyz\/]+|%[dDfFgGmMrRuUyY]", RegexOptions.Compiled);
private static readonly Regex ISOFormatRegex = new Regex(@"%[Oos]", RegexOptions.Compiled);
/// <summary>
/// Represents the smallest possible value of <see cref="Time"/>. This field is read-only.
/// </summary>
public static readonly Time MinValue = new Time(MinTicks);
/// <summary>
/// Represents the largest possible value of <see cref="Time"/>. This field is read-only.
/// </summary>
public static readonly Time MaxValue = new Time(MaxTicks);
// Number of ticks (100ns units) since midnight at the beginning of a standard 24-hour day.
// NOTE: This is the only field in this structure.
private readonly long _ticks;
/// <summary>
/// Initializes a new instance of a <see cref="Time"/> structure to a specified number of ticks.
/// </summary>
/// <param name="ticks">
/// A time expressed in the number of 100-nanosecond intervals that have elapsed since midnight (00:00:00),
/// without regard to daylight saving time transitions.
/// </param>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="ticks"/> is out of the range supported by the <see cref="Time"/> object.
/// </exception>
public Time(long ticks)
{
if (ticks < MinTicks || ticks > MaxTicks)
{
throw new ArgumentOutOfRangeException(nameof(ticks), ticks, Strings.ArgumentOutOfRange_TimeBadTicks);
}
Contract.EndContractBlock();
_ticks = ticks;
}
/// <summary>
/// Initializes a new instance of a <see cref="Time"/> structure to the specified
/// hour and minute.
/// </summary>
/// <param name="hour">The hours (0 through 23).</param>
/// <param name="minute">The minutes (0 through 59).</param>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="hour"/> is less than 0 or greater than 23.
/// <para>-or-</para>
/// <paramref name="minute"/> is less than 0 or greater than 59.
/// </exception>
public Time(int hour, int minute)
{
if (hour < 0 || hour > 23)
{
throw new ArgumentOutOfRangeException(nameof(hour), hour, Strings.ArgumentOutOfRange_Hour);
}
if (minute < 0 || minute > 59)
{
throw new ArgumentOutOfRangeException(nameof(minute), minute, Strings.ArgumentOutOfRange_Minute);
}
Contract.EndContractBlock();
_ticks = hour * TicksPerHour +
minute * TicksPerMinute;
}
/// <summary>
/// Initializes a new instance of a <see cref="Time"/> structure to the specified
/// hour, minute, and meridiem, using the hours of a 12-hour clock.
/// </summary>
/// <param name="hour">The hours (1 through 12).</param>
/// <param name="minute">The minutes (0 through 59).</param>
/// <param name="meridiem">The meridiem, either <see cref="System.Meridiem.AM"/>,
/// or <see cref="System.Meridiem.PM"/>.</param>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="hour"/> is less than 1 or greater than 12.
/// <para>-or-</para>
/// <paramref name="minute"/> is less than 0 or greater than 59.
/// </exception>
public Time(int hour, int minute, Meridiem meridiem)
{
if (hour < 1 || hour > 12)
{
throw new ArgumentOutOfRangeException(nameof(hour), hour, Strings.ArgumentOutOfRange_Hour12HF);
}
if (minute < 0 || minute > 59)
{
throw new ArgumentOutOfRangeException(nameof(minute), minute, Strings.ArgumentOutOfRange_Minute);
}
if (meridiem < Meridiem.AM || meridiem > Meridiem.PM)
{
throw new ArgumentOutOfRangeException(nameof(meridiem), meridiem, Strings.ArgumentOutOfRange_Meridiem);
}
Contract.EndContractBlock();
int hours24 = Hours12To24(hour, meridiem);
_ticks = hours24 * TicksPerHour +
minute * TicksPerMinute;
}
/// <summary>
/// Initializes a new instance of a <see cref="Time"/> structure to the specified
/// hour, minute, and second, using the hours of a 24-hour clock.
/// </summary>
/// <param name="hour">The hours (0 through 23).</param>
/// <param name="minute">The minutes (0 through 59).</param>
/// <param name="second">The seconds (0 through 59).</param>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="hour"/> is less than 0 or greater than 23.
/// <para>-or-</para>
/// <paramref name="minute"/> is less than 0 or greater than 59.
/// <para>-or-</para>
/// <paramref name="second"/> is less than 0 or greater than 59.
/// </exception>
public Time(int hour, int minute, int second)
{
if (hour < 0 || hour > 23)
{
throw new ArgumentOutOfRangeException(nameof(hour), hour, Strings.ArgumentOutOfRange_Hour);
}
if (minute < 0 || minute > 59)
{
throw new ArgumentOutOfRangeException(nameof(minute), minute, Strings.ArgumentOutOfRange_Minute);
}
if (second < 0 || second > 59)
{
throw new ArgumentOutOfRangeException(nameof(second), second, Strings.ArgumentOutOfRange_Second);
}
Contract.EndContractBlock();
_ticks = hour * TicksPerHour +
minute * TicksPerMinute +
second * TicksPerSecond;
}
/// <summary>
/// Initializes a new instance of a <see cref="Time"/> structure to the specified
/// hour, minute, second, and meridiem, using the hours of a 12-hour clock.
/// </summary>
/// <param name="hour">The hours (1 through 12).</param>
/// <param name="minute">The minutes (0 through 59).</param>
/// <param name="second">The seconds (0 through 59).</param>
/// <param name="meridiem">The meridiem, either <see cref="System.Meridiem.AM"/>,
/// or <see cref="System.Meridiem.PM"/>.</param>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="hour"/> is less than 1 or greater than 12.
/// <para>-or-</para>
/// <paramref name="minute"/> is less than 0 or greater than 59.
/// <para>-or-</para>
/// <paramref name="second"/> is less than 0 or greater than 59.
/// </exception>
public Time(int hour, int minute, int second, Meridiem meridiem)
{
if (hour < 1 || hour > 12)
{
throw new ArgumentOutOfRangeException(nameof(hour), hour, Strings.ArgumentOutOfRange_Hour12HF);
}
if (minute < 0 || minute > 59)
{
throw new ArgumentOutOfRangeException(nameof(minute), minute, Strings.ArgumentOutOfRange_Minute);
}
if (second < 0 || second > 59)
{
throw new ArgumentOutOfRangeException(nameof(second), second, Strings.ArgumentOutOfRange_Second);
}
if (meridiem < Meridiem.AM || meridiem > Meridiem.PM)
{
throw new ArgumentOutOfRangeException(nameof(meridiem), meridiem, Strings.ArgumentOutOfRange_Meridiem);
}
Contract.EndContractBlock();
int hours24 = Hours12To24(hour, meridiem);
_ticks = hours24 * TicksPerHour +
minute * TicksPerMinute +
second * TicksPerSecond;
}
/// <summary>
/// Initializes a new instance of a <see cref="Time"/> structure to the specified
/// hour, minute, second, and millisecond, using the hours of a 24-hour clock.
/// </summary>
/// <param name="hour">The hours (0 through 23).</param>
/// <param name="minute">The minutes (0 through 59).</param>
/// <param name="second">The seconds (0 through 59).</param>
/// <param name="millisecond">The milliseconds (0 through 999).</param>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="hour"/> is less than 0 or greater than 23.
/// <para>-or-</para>
/// <paramref name="minute"/> is less than 0 or greater than 59.
/// <para>-or-</para>
/// <paramref name="second"/> is less than 0 or greater than 59.
/// <para>-or-</para>
/// <paramref name="millisecond"/> is less than 0 or greater than 999.
/// </exception>
public Time(int hour, int minute, int second, int millisecond)
{
if (hour < 0 || hour > 23)
{
throw new ArgumentOutOfRangeException(nameof(hour), hour, Strings.ArgumentOutOfRange_Hour);
}
if (minute < 0 || minute > 59)
{
throw new ArgumentOutOfRangeException(nameof(minute), minute, Strings.ArgumentOutOfRange_Minute);
}
if (second < 0 || second > 59)
{
throw new ArgumentOutOfRangeException(nameof(second), second, Strings.ArgumentOutOfRange_Second);
}
if (millisecond < 0 || millisecond > 999)
{
throw new ArgumentOutOfRangeException(nameof(millisecond), millisecond, Strings.ArgumentOutOfRange_Millisecond);
}
Contract.EndContractBlock();
_ticks = hour * TicksPerHour +
minute * TicksPerMinute +
second * TicksPerSecond +
millisecond * TicksPerMillisecond;
}
/// <summary>
/// Initializes a new instance of a <see cref="Time"/> structure to the specified
/// hour, minute, second, millisecond, and meridiem, using the hours of a 12-hour clock.
/// </summary>
/// <param name="hour">The hours (1 through 12).</param>
/// <param name="minute">The minutes (0 through 59).</param>
/// <param name="second">The seconds (0 through 59).</param>
/// <param name="millisecond">The milliseconds (0 through 999).</param>
/// <param name="meridiem">The meridiem, either <see cref="System.Meridiem.AM"/>,
/// or <see cref="System.Meridiem.PM"/>.</param>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="hour"/> is less than 1 or greater than 12.
/// <para>-or-</para>
/// <paramref name="minute"/> is less than 0 or greater than 59.
/// <para>-or-</para>
/// <paramref name="second"/> is less than 0 or greater than 59.
/// <para>-or-</para>
/// <paramref name="millisecond"/> is less than 0 or greater than 999.
/// </exception>
public Time(int hour, int minute, int second, int millisecond, Meridiem meridiem)
{
if (hour < 1 || hour > 12)
{
throw new ArgumentOutOfRangeException(nameof(hour), hour, Strings.ArgumentOutOfRange_Hour12HF);
}
if (minute < 0 || minute > 59)
{
throw new ArgumentOutOfRangeException(nameof(minute), minute, Strings.ArgumentOutOfRange_Minute);
}
if (second < 0 || second > 59)
{
throw new ArgumentOutOfRangeException(nameof(second), second, Strings.ArgumentOutOfRange_Second);
}
if (millisecond < 0 || millisecond > 999)
{
throw new ArgumentOutOfRangeException(nameof(millisecond), millisecond, Strings.ArgumentOutOfRange_Millisecond);
}
if (meridiem < Meridiem.AM || meridiem > Meridiem.PM)
{
throw new ArgumentOutOfRangeException(nameof(meridiem), meridiem, Strings.ArgumentOutOfRange_Meridiem);
}
Contract.EndContractBlock();
int hours24 = Hours12To24(hour, meridiem);
_ticks = hours24 * TicksPerHour +
minute * TicksPerMinute +
second * TicksPerSecond +
millisecond * TicksPerMillisecond;
}
/// <summary>
/// Gets the hour component of the time represented by this instance, using the hours of a 24-hour clock.
/// </summary>
/// <value>The hour component, expressed as a value between 0 and 23.</value>
public int Hour
{
get
{
Contract.Ensures(Contract.Result<int>() >= 0);
Contract.Ensures(Contract.Result<int>() <= 23);
return (int)((_ticks / TicksPerHour) % 24);
}
}
/// <summary>
/// Gets the hour component of the time represented by this instance, using the hours of a 12-hour clock.
/// </summary>
/// <value>The hour component, expressed as a value between 1 and 12.</value>
public int HourOf12HourClock
{
get
{
Contract.Ensures(Contract.Result<int>() >= 1);
Contract.Ensures(Contract.Result<int>() <= 12);
return (Hour + 11) % 12 + 1;
}
}
/// <summary>
/// Gets the meridiem (AM or PM) of the time represented by this instance.
/// The meridiem can be used inconjunction with the <see cref="HourOf12HourClock"/> property
/// to represent this instance's time on a 12-hour clock.
/// </summary>
/// <value>An enumerated constant that indicates the meridiem of this <see cref="Time"/> value.</value>
/// <remarks>
/// Though commonly used in English, these abbreviations derive from Latin.
/// AM is an abbreviation for "Ante Meridiem", meaning "before mid-day".
/// PM is an abbreviation for "Post Meridiem", meaning "after mid-day".
/// </remarks>
public Meridiem Meridiem => Hour < 12 ? Meridiem.AM : Meridiem.PM;
/// <summary>
/// Gets the minute component of the time represented by this instance.
/// </summary>
/// <value>The minute component, expressed as a value between 0 and 59.</value>
public int Minute
{
get
{
Contract.Ensures(Contract.Result<int>() >= 0);
Contract.Ensures(Contract.Result<int>() <= 59);
return (int)((_ticks / TicksPerMinute) % 60);
}
}
/// <summary>
/// Gets the second component of the time represented by this instance.
/// </summary>
/// <value>The second component, expressed as a value between 0 and 59.</value>
public int Second
{
get
{
Contract.Ensures(Contract.Result<int>() >= 0);
Contract.Ensures(Contract.Result<int>() <= 59);
return (int)((_ticks / TicksPerSecond) % 60);
}
}
/// <summary>
/// Gets the millisecond component of the time represented by this instance.
/// </summary>
/// <value>The millisecond component, expressed as a value between 0 and 999.</value>
public int Millisecond
{
get
{
Contract.Ensures(Contract.Result<int>() >= 0);
Contract.Ensures(Contract.Result<int>() <= 999);
return (int)((_ticks / TicksPerMillisecond) % 1000);
}
}
/// <summary>
/// Gets the number of ticks that represent the time of this instance.
/// </summary>
/// <value>
/// The number of ticks that represent the time of this instance.
/// The value is between <c>Time.MinValue.Ticks</c> and <c>Time.MaxValue.Ticks</c>.
/// </value>
/// <remarks>
/// Each tick is a 100-nanosecond interval. Collectively, they represent the time that has
/// elapsed since midnight (00:00:00), without regard to daylight saving time transitions.
/// </remarks>
public long Ticks
{
get
{
Contract.Ensures(Contract.Result<long>() >= MinTicks);
Contract.Ensures(Contract.Result<long>() <= MaxTicks);
return _ticks;
}
}
/// <summary>
/// Creates a <see cref="DateTime"/> object from the current <see cref="Time"/> and the specified <see cref="Date"/>.
/// The resulting value has a <see cref="DateTime.Kind"/> of <see cref="DateTimeKind.Unspecified"/>.
/// </summary>
/// <remarks>
/// Since neither <see cref="Date"/> or <see cref="Time"/> keep track of <see cref="DateTimeKind"/>,
/// recognize that the <see cref="DateTime"/> produced by <c>Time.Now.On(Date.Today)</c> will have
/// <see cref="DateTimeKind.Unspecified"/>, rather than then <see cref="DateTimeKind.Local"/> that would be
/// given by <c>DateTime.Now</c>.
/// <para>The same applies for <see cref="DateTimeKind.Utc"/>.</para>
/// </remarks>
public DateTime On(Date date)
{
long ticks = date.DayNumber * TicksPerDay + _ticks;
return new DateTime(ticks);
}
/// <summary>
/// Gets a <see cref="Time"/> object that is set to the current time in the specified time zone.
/// </summary>
/// <param name="timeZoneInfo">The <see cref="TimeZoneInfo"/> instance.</param>
/// <returns>The current <see cref="Time"/> for the specified time zone.</returns>
public static Time NowInTimeZone(TimeZoneInfo timeZoneInfo)
{
DateTimeOffset utcNow = DateTimeOffset.UtcNow;
DateTimeOffset localNow = TimeZoneInfo.ConvertTime(utcNow, timeZoneInfo);
return TimeFromTimeSpan(localNow.TimeOfDay);
}
/// <summary>
/// Gets a <see cref="Time"/> object that is set to the current time,
/// expressed in this computer's local time zone.
/// </summary>
/// <value>An object whose value is the current local time.</value>
public static Time Now
{
get
{
DateTime localNow = DateTime.Now;
return TimeFromTimeSpan(localNow.TimeOfDay);
}
}
/// <summary>
/// Gets a <see cref="Time"/> object that is set to the current time,
/// expressed as Coordinated Universal Time (UTC).
/// </summary>
/// <value>An object whose value is the current UTC time.</value>
public static Time UtcNow
{
get
{
DateTime utcNow = DateTime.UtcNow;
return TimeFromTimeSpan(utcNow.TimeOfDay);
}
}
/// <summary>
/// Determines if a time falls within the range provided.
/// Supports both "normal" ranges such as 10:00-12:00, and ranges that span midnight such as 23:00-01:00.
/// </summary>
/// <param name="startTime">The starting time of day, inclusive.</param>
/// <param name="endTime">The ending time of day, exclusive.</param>
/// <returns>True, if the time falls within the range, false otherwise.</returns>
public bool IsBetween(Time startTime, Time endTime)
{
long startTicks = startTime._ticks;
long endTicks = endTime._ticks;
return startTicks <= endTicks
? (startTicks <= _ticks && endTicks > _ticks)
: (startTicks <= _ticks || endTicks > _ticks);
}
/// <summary>
/// Subtracts another <see cref="Time"/> value from this instance, returning a <see cref="TimeSpan"/>.
/// Assumes a standard day, with no invalid or ambiguous times due to Daylight Saving Time.
/// Supports both "normal" ranges such as 10:00-12:00, and ranges that span midnight such as 23:00-01:00.
/// </summary>
/// <param name="startTime">The starting time of day, inclusive.</param>
/// <returns>
/// A <see cref="TimeSpan"/> representing the duration between the <paramref name="startTime"/>
/// (inclusive), and this instance (exclusive).
/// </returns>
public TimeSpan Subtract(Time startTime)
{
return TimeSpan.FromTicks((_ticks - startTime._ticks + TicksPerDay) % TicksPerDay);
}
/// <summary>
/// Gets a <see cref="Time"/> object whose value is ahead or behind the value of this instance by the
/// specified amount of time. Positive values will move the time forward; negative values will move the
/// time backwards.
/// </summary>
/// <param name="timeSpan">The amount of time to adjust by. The value can be negative or positive.</param>
/// <returns>
/// A new <see cref="Time"/> object which is the result of adjusting this instance by the
/// <paramref name="timeSpan"/> specified.
/// </returns>
/// <remarks>
/// The time is modeled on a circular 24-hour clock. When a value crosses midnight, it carries forward
/// into the next day. For example, 23:00 plus two hours is 01:00.
/// </remarks>
public Time Add(TimeSpan timeSpan)
{
return AddTicks(timeSpan.Ticks);
}
/// <summary>
/// Gets a <see cref="Time"/> object whose value is ahead or behind the value of this instance by the
/// specified number of hours. Positive values will move the time forward; negative values will move the
/// time backwards.
/// </summary>
/// <param name="hours">The number of hours to adjust by. The value can be negative or positive.</param>
/// <returns>
/// A new <see cref="Time"/> object which is the result of adjusting this instance by the
/// <paramref name="hours"/> specified.
/// </returns>
/// <remarks>
/// The time is modeled on a circular 24-hour clock. When a value crosses midnight, it carries forward
/// into the next day. For example, 23:00 plus one hour is 00:00.
/// </remarks>
public Time AddHours(double hours)
{
return AddTicks((long)(hours * TicksPerHour));
}
/// <summary>
/// Gets a <see cref="Time"/> object whose value is ahead or behind the value of this instance by the
/// specified number of minutes. Positive values will move the time forward; negative values will move the
/// time backwards.
/// </summary>
/// <param name="minutes">The number of minutes to adjust by. The value can be negative or positive.</param>
/// <returns>
/// A new <see cref="Time"/> object which is the result of adjusting this instance by the
/// <paramref name="minutes"/> specified.
/// </returns>
/// <remarks>
/// The time is modeled on a circular 24-hour clock. When a value crosses midnight, it carries forward
/// into the next day. For example, 23:59 plus one minute is 00:00.
/// </remarks>
public Time AddMinutes(double minutes)
{
return AddTicks((long)(minutes * TicksPerMinute));
}
/// <summary>
/// Gets a <see cref="Time"/> object whose value is ahead or behind the value of this instance by the
/// specified number of seconds. Positive values will move the time forward; negative values will move the
/// time backwards.
/// </summary>
/// <param name="seconds">The number of seconds to adjust by. The value can be negative or positive.</param>
/// <returns>
/// A new <see cref="Time"/> object which is the result of adjusting this instance by the
/// <paramref name="seconds"/> specified.
/// </returns>
/// <remarks>
/// The time is modeled on a circular 24-hour clock. When a value crosses midnight, it carries forward
/// into the next day. For example, 23:59:59 plus one second is 00:00:00.
/// </remarks>
public Time AddSeconds(double seconds)
{
return AddTicks((long)(seconds * TicksPerSecond));
}
/// <summary>
/// Gets a <see cref="Time"/> object whose value is ahead or behind the value of this instance by the
/// specified number of milliseconds. Positive values will move the time forward; negative values will move the
/// time backwards.
/// </summary>
/// <param name="milliseconds">
/// The number of milliseconds to adjust by. The value can be negative or positive.
/// </param>
/// <returns>
/// A new <see cref="Time"/> object which is the result of adjusting this instance by the
/// <paramref name="milliseconds"/> specified.
/// </returns>
/// <remarks>
/// The time is modeled on a circular 24-hour clock. When a value crosses midnight, it carries forward
/// into the next day. For example, 23:59:59.9990000 plus one millisecond is 00:00:00.0000000.
/// </remarks>
public Time AddMilliseconds(double milliseconds)
{
return AddTicks((long)(milliseconds * TicksPerMillisecond));
}
/// <summary>
/// Gets a <see cref="Time"/> object whose value is ahead or behind the value of this instance by the
/// specified number of ticks. Positive values will move the time forward; negative values will move the
/// time backwards.
/// </summary>
/// <param name="ticks">
/// The number of ticks to adjust by. The value can be negative or positive.
/// A tick is a unit of time equal to 100 nanoseconds.
/// </param>
/// <returns>
/// A new <see cref="Time"/> object which is the result of adjusting this instance by the
/// <paramref name="ticks"/> specified.
/// </returns>
/// <remarks>
/// The time is modeled on a circular 24-hour clock. When a value crosses midnight, it carries forward
/// into the next day. For example, 23:59:59.9999999 plus one tick is 00:00:00.0000000.
/// </remarks>
public Time AddTicks(long ticks)
{
long t = (_ticks + TicksPerDay + (ticks % TicksPerDay)) % TicksPerDay;
return new Time(t);
}
/// <summary>
/// Gets a <see cref="Time"/> object whose value is ahead or behind the value of this instance by the
/// specified amount of time. Positive values will move the time backwards; negative values will move the
/// time forward. This is equivalent to calling <c>Add(timeSpan.Negate())</c>.
/// </summary>
/// <param name="timeSpan">The amount of time to adjust by. The value can be negative or positive.</param>
/// <returns>
/// A new <see cref="Time"/> object which is the result of adjusting this instance by the
/// <paramref name="timeSpan"/> specified.
/// </returns>
/// <remarks>
/// The time is modeled on a circular 24-hour clock. When a value crosses midnight, it carries backwards
/// into the previous day. For example, 01:00 minus two hours is 23:00.
/// </remarks>
public Time Subtract(TimeSpan timeSpan)
{
return AddTicks(-timeSpan.Ticks);
}
/// <summary>
/// Adds a specified time interval to a specified time, yielding a new time.
/// </summary>
/// <param name="time">The time of day value to add to.</param>
/// <param name="timeSpan">The time interval to add.</param>
/// <returns>
/// A <see cref="Time"/> object which is the result of adding the <paramref name="timeSpan"/>
/// specified to the <paramref name="time"/> provided.
/// </returns>
/// <remarks>
/// The time is modeled on a circular 24-hour clock. When a value crosses midnight, it carries forward
/// into the next day. For example, 23:00 plus two hours is 01:00.
/// </remarks>
public static Time operator +(Time time, TimeSpan timeSpan)
{
return time.Add(timeSpan);
}
/// <summary>
/// Subtracts a specified time interval to a specified time, yielding a new time.
/// </summary>
/// <param name="time">The time of day value to subtract from.</param>
/// <param name="timeSpan">The time interval to subtract.</param>
/// <returns>
/// A <see cref="Time"/> object which is the result of subtracting the <paramref name="timeSpan"/>
/// specified from the <paramref name="time"/> provided.
/// </returns>
/// <remarks>
/// The time is modeled on a circular 24-hour clock. When a value crosses midnight, it carries backwards
/// into the previous day. For example, 01:00 minus two hours is 23:00.
/// </remarks>
public static Time operator -(Time time, TimeSpan timeSpan)
{
return time.Subtract(timeSpan);
}
/// <summary>
/// Calculates the duration between the <paramref name="startTime"/> and <see cref="endTime"/>.
/// Assumes a standard day, with no invalid or ambiguous times due to Daylight Saving Time.
/// Supports both "normal" ranges such as 10:00-12:00, and ranges that span midnight such as 23:00-01:00.
/// </summary>
/// <param name="startTime">The starting time of day, inclusive.</param>
/// <param name="endTime">The ending time of day, exclusive.</param>
/// <returns>
/// A <see cref="TimeSpan"/> representing the duration between the two time of day values.
/// </returns>
public static TimeSpan operator -(Time endTime, Time startTime)
{
return endTime.Subtract(startTime);
}
/// <summary>
/// Compares two instances of <see cref="Time"/> and returns an integer that indicates whether the first
/// instance is earlier than, the same as, or later than the second instance, within the same day.
/// </summary>
/// <param name="left">The first object to compare.</param>
/// <param name="right">The second object to compare.</param>
/// <returns>
/// A signed number indicating the relative values of <paramref name="left"/> and <paramref name="right"/>.
/// <list type="table">
/// <listheader><term>Value</term><term>Description</term></listheader>
/// <item>
/// <term>Less than zero</term>
/// <term><paramref name="left"/> is earlier than <paramref name="right"/>.</term>
/// </item>
/// <item>
/// <term>Zero</term>
/// <term><paramref name="left"/> is the same as <paramref name="right"/>.</term>
/// </item>
/// <item>
/// <term>Greater than zero</term>
/// <term><paramref name="left"/> is later than <paramref name="right"/>.</term>
/// </item>
/// </list>
/// </returns>
/// <remarks>
/// This operation considers all time values to be contained within the same day. If you need to compare
/// time values that cross midnight into a different day, use the <see cref="IsBetween"/> method instead.
/// </remarks>
public static int Compare(Time left, Time right)
{
if (left._ticks > right._ticks)
{
return 1;
}
if (left._ticks < right._ticks)
{
return -1;
}
return 0;
}
/// <summary>
/// Compares the value of this instance to a specified <see cref="Time"/> value and returns an integer
/// that indicates whether this instance is earlier than, the same as, or later than the specified
/// <see cref="Time"/> value, within the same day.
/// </summary>
/// <param name="value">The object to compare to the current instance.</param>
/// <returns>
/// A signed number indicating the relative values of this instance and the <paramref name="value"/> parameter.
/// <list type="table">
/// <listheader><term>Value</term><term>Description</term></listheader>
/// <item>
/// <term>Less than zero</term>
/// <term>This instance is earlier than <paramref name="value"/>.</term>
/// </item>
/// <item>
/// <term>Zero</term>
/// <term>This instance is the same as <paramref name="value"/>.</term>
/// </item>
/// <item>
/// <term>Greater than zero</term>
/// <term>This instance is later than <paramref name="value"/>.</term>
/// </item>
/// </list>
/// </returns>
/// <remarks>
/// This operation considers all time values to be contained within the same day. If you need to compare
/// time values that cross midnight into a different day, use the <see cref="IsBetween"/> method instead.
/// </remarks>
public int CompareTo(Time value)
{
return Compare(this, value);
}
/// <summary>
/// Compares the value of this instance to a specified object that contains a <see cref="Time"/> value and
/// returns an integer that indicates whether this instance is earlier than, the same as, or later than the
/// specified <see cref="Time"/> value, within the same day.
/// </summary>
/// <param name="value">The object to compare to the current instance.</param>
/// <returns>
/// A signed number indicating the relative values of this instance and the <paramref name="value"/> parameter.
/// <list type="table">
/// <listheader><term>Value</term><term>Description</term></listheader>
/// <item>
/// <term>Less than zero</term>
/// <term>This instance is earlier than <paramref name="value"/>.</term>
/// </item>
/// <item>
/// <term>Zero</term>
/// <term>This instance is earlier than <paramref name="value"/>.</term>
/// </item>
/// <item>
/// <term>Greater than zero</term>
/// <term>
/// This instance is later than <paramref name="value"/>,
/// or <paramref name="value"/> is <c>null</c>.
/// </term>
/// </item>
/// </list>
/// </returns>
/// <remarks>
/// This operation considers all time values to be contained within the same day. If you need to compare
/// time values that cross midnight into a different day, use the <see cref="IsBetween"/> method instead.
/// </remarks>
/// <exception cref="ArgumentException">
/// <paramref name="value"/> is not a <see cref="Time"/>.
/// </exception>
public int CompareTo(object value)
{
if (value == null)
{
return 1;
}
if (!(value is Time))
{
throw new ArgumentException(Strings.Argument_MustBeTime);
}
return Compare(this, (Time)value);
}
/// <summary>
/// Returns a value indicating whether two <see cref="Time"/> instances have the same time value.
/// </summary>
/// <param name="left">The first object to compare.</param>
/// <param name="right">The second object to compare.</param>
/// <returns><c>true</c> if the two values are equal; otherwise, <c>false</c>.</returns>
public static bool Equals(Time left, Time right)
{
return left.Equals(right);
}
/// <summary>
/// Returns a value indicating whether the value of this instance is equal to the value of the specified
/// <see cref="Time"/> instance.
/// </summary>
/// <param name="value">The other <see cref="Time"/> object to compare against this instance.</param>
/// <returns>
/// <c>true</c> if the <paramref name="value"/> parameter equals the value of this instance;
/// otherwise, <c>false</c>.
/// </returns>
public bool Equals(Time value)
{
return _ticks == value._ticks;
}
/// <summary>
/// Returns a value indicating whether this instance is equal to the specified object.
/// </summary>
/// <param name="value">The object to compare to this instance.</param>
/// <returns>
/// <c>true</c> if <paramref name="value"/> is an instance of <see cref="Time"/>
/// and equals the value of this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object value)
{
if (ReferenceEquals(null, value))
{
return false;
}
return value is Time time && Equals(time);
}
/// <summary>
/// Returns the hash code of this instance.
/// </summary>
/// <returns>A 32-bit signed integer hash code.</returns>
/// <remarks>
/// The hash code of a <see cref="Time"/> object is the same as the hash code of
/// its <see cref="Ticks"/> value.
/// </remarks>
public override int GetHashCode()
{
return _ticks.GetHashCode();
}
/// <summary>
/// Converts the value of the current <see cref="Time"/> object to its equivalent string representation.
/// </summary>
/// <returns>A string representation of value of the current <see cref="Time"/> object.</returns>
public override string ToString()
{
Contract.Ensures(Contract.Result<string>() != null);
return DateTime.MinValue.AddTicks(_ticks).ToString("T");
}
/// <summary>
/// Converts the value of the current <see cref="Time"/> object to its equivalent string representation
/// using the specified culture-specific format information.
/// </summary>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <returns>
/// A string representation of value of the current <see cref="Time"/> object as specified by
/// <paramref name="provider"/>.
/// </returns>
public string ToString(IFormatProvider provider)
{
Contract.Ensures(Contract.Result<string>() != null);
return DateTime.MinValue.AddTicks(_ticks).ToString("T", provider);
}
/// <summary>
/// Converts the value of the current <see cref="Time"/> object to its equivalent string representation
/// using the specified format.
/// </summary>
/// <param name="format">A standard or custom time-of-day format string.</param>
/// <returns>
/// A string representation of value of the current <see cref="Time"/> object as specified by
/// <paramref name="format"/>.
/// </returns>
/// <exception cref="FormatException">
/// The length of <paramref name="format"/> is 1, and it is not one of the format specifier characters defined
/// for <see cref="DateTimeFormatInfo"/>.
/// <para>-or-</para>
/// <paramref name="format"/> does not contain a valid custom format pattern.
/// <para>-or-</para>
/// The standard or custom format specified is not valid for a <see cref="Time"/> object, because it
/// contains a date component.
/// </exception>
public string ToString(string format)
{
Contract.Ensures(Contract.Result<string>() != null);
format = NormalizeTimeFormat(format);
return DateTime.MinValue.AddTicks(_ticks).ToString(format);
}
/// <summary>
/// Converts the value of the current <see cref="Time"/> object to its equivalent string representation
/// using the specified format and culture-specific format information.
/// </summary>
/// <param name="format">A standard or custom time-of-day format string.</param>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <returns>
/// A string representation of value of the current <see cref="Time"/> object as specified by
/// <paramref name="format"/> and <paramref name="provider"/>.
/// </returns>
/// <exception cref="FormatException">
/// The length of <paramref name="format"/> is 1, and it is not one of the format specifier characters defined
/// for <see cref="DateTimeFormatInfo"/>.
/// <para>-or-</para>
/// <paramref name="format"/> does not contain a valid custom format pattern.
/// <para>-or-</para>
/// The standard or custom format specified is not valid for a <see cref="Time"/> object, because it
/// contains a date component.
/// </exception>
public string ToString(string format, IFormatProvider provider)
{
Contract.Ensures(Contract.Result<string>() != null);
format = NormalizeTimeFormat(format);
return DateTime.MinValue.AddTicks(_ticks).ToString(format, provider);
}
/// <summary>
/// Converts the value of the current <see cref="Time"/> object to its equivalent
/// long time string representation.
/// </summary>
/// <returns>A string that contains the long time string representation of the
/// current <see cref="Time"/> object.</returns>
/// <remarks>The value of the current <see cref="Time"/> object is formatted
/// using the pattern defined by the <see cref="DateTimeFormatInfo.LongTimePattern" />
/// property associated with the current thread culture.</remarks>
public string ToLongTimeString()
{
return ToString(CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern);
}
/// <summary>
/// Converts the value of the current <see cref="Time"/> object to its equivalent
/// long time string representation.
/// </summary>
/// <returns>A string that contains the long time string representation of the
/// current <see cref="Time"/> object.</returns>
/// <remarks>The value of the current <see cref="Time"/> object is formatted
/// using the pattern defined by the <see cref="DateTimeFormatInfo.LongTimePattern" />
/// property associated with the invariant culture.</remarks>
public string ToLongTimeStringInvariant()
{
return ToString(CultureInfo.InvariantCulture.DateTimeFormat.LongTimePattern, CultureInfo.InvariantCulture);
}
/// <summary>