-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTimeOfDay.cs
More file actions
1024 lines (906 loc) · 38.8 KB
/
TimeOfDay.cs
File metadata and controls
1024 lines (906 loc) · 38.8 KB
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
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================
using RapidField.SolidInstruments.Core.ArgumentValidation;
using RapidField.SolidInstruments.Core.Extensions;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Serialization;
namespace RapidField.SolidInstruments.Core
{
/// <summary>
/// Represents a specific time of day in a specific time zone.
/// </summary>
[DataContract]
public sealed class TimeOfDay : ICloneable, IComparable, IComparable<TimeOfDay>, IEquatable<TimeOfDay>
{
/// <summary>
/// Initializes a new instance of the <see cref="TimeOfDay" /> class.
/// </summary>
/// <param name="zone">
/// The time zone for this instance.
/// </param>
/// <param name="hour">
/// The hour component (0 through 23) of the time represented by this instance.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="zone" /> is <see langword="null" />.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// A specified numeric argument is less than zero or greater than the logical maximum (eg. hour 25).
/// </exception>
public TimeOfDay(TimeZoneInfo zone, Int32 hour)
: this(zone, hour, 0, 0, 0)
{
return;
}
/// <summary>
/// Initializes a new instance of the <see cref="TimeOfDay" /> class.
/// </summary>
/// <param name="zone">
/// The time zone for this instance.
/// </param>
/// <param name="hour">
/// The hour component (0 through 23) of the time represented by this instance.
/// </param>
/// <param name="minute">
/// The minute component (0 through 59) of the time represented by this instance. The default value is zero.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="zone" /> is <see langword="null" />.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// A specified numeric argument is less than zero or greater than the logical maximum (eg. hour 25).
/// </exception>
public TimeOfDay(TimeZoneInfo zone, Int32 hour, Int32 minute)
: this(zone, hour, minute, 0, 0)
{
return;
}
/// <summary>
/// Initializes a new instance of the <see cref="TimeOfDay" /> class.
/// </summary>
/// <param name="zone">
/// The time zone for this instance.
/// </param>
/// <param name="hour">
/// The hour component (0 through 23) of the time represented by this instance.
/// </param>
/// <param name="minute">
/// The minute component (0 through 59) of the time represented by this instance. The default value is zero.
/// </param>
/// <param name="second">
/// The second component (0 through 59) of the time represented by this instance. The default value is zero.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="zone" /> is <see langword="null" />.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// A specified numeric argument is less than zero or greater than the logical maximum (eg. hour 25).
/// </exception>
public TimeOfDay(TimeZoneInfo zone, Int32 hour, Int32 minute, Int32 second)
: this(zone, hour, minute, second, 0)
{
return;
}
/// <summary>
/// Initializes a new instance of the <see cref="TimeOfDay" /> class.
/// </summary>
/// <param name="zone">
/// The time zone for this instance.
/// </param>
/// <param name="hour">
/// The hour component (0 through 23) of the time represented by this instance.
/// </param>
/// <param name="minute">
/// The minute component (0 through 59) of the time represented by this instance. The default value is zero.
/// </param>
/// <param name="second">
/// The second component (0 through 59) of the time represented by this instance. The default value is zero.
/// </param>
/// <param name="millisecond">
/// The millisecond component (0 through 999) of the time represented by this instance. The default value is zero.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="zone" /> is <see langword="null" />.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// A specified numeric argument is less than zero or greater than the logical maximum (eg. hour 25).
/// </exception>
public TimeOfDay(TimeZoneInfo zone, Int32 hour, Int32 minute, Int32 second, Int32 millisecond)
{
Hour = hour.RejectIf().IsLessThan(0, nameof(hour)).OrIf().IsGreaterThan(23, nameof(hour));
Millisecond = millisecond.RejectIf().IsLessThan(0, nameof(millisecond)).OrIf().IsGreaterThan(999, nameof(millisecond));
Minute = minute.RejectIf().IsLessThan(0, nameof(minute)).OrIf().IsGreaterThan(59, nameof(minute));
Second = second.RejectIf().IsLessThan(0, nameof(second)).OrIf().IsGreaterThan(59, nameof(second));
Zone = zone.RejectIf().IsNull(nameof(zone));
}
/// <summary>
/// Determines whether or not two specified <see cref="TimeOfDay" /> instances are not equal.
/// </summary>
/// <param name="a">
/// The first <see cref="TimeOfDay" /> instance to compare.
/// </param>
/// <param name="b">
/// The second <see cref="TimeOfDay" /> instance to compare.
/// </param>
/// <returns>
/// A value indicating whether or not the specified instances are not equal.
/// </returns>
public static Boolean operator !=(TimeOfDay a, TimeOfDay b) => (a == b) == false;
/// <summary>
/// Determines whether or not a specified <see cref="TimeOfDay" /> instance is less than another specified instance.
/// </summary>
/// <param name="a">
/// The first <see cref="TimeOfDay" /> instance to compare.
/// </param>
/// <param name="b">
/// The second <see cref="TimeOfDay" /> instance to compare.
/// </param>
/// <returns>
/// <see langword="true" /> if the second object is earlier than the first object, otherwise <see langword="false" />.
/// </returns>
public static Boolean operator <(TimeOfDay a, TimeOfDay b) => a.CompareTo(b) == -1;
/// <summary>
/// Determines whether or not a specified <see cref="TimeOfDay" /> instance is less than or equal to another supplied
/// instance.
/// </summary>
/// <param name="a">
/// The first <see cref="TimeOfDay" /> instance to compare.
/// </param>
/// <param name="b">
/// The second <see cref="TimeOfDay" /> instance to compare.
/// </param>
/// <returns>
/// <see langword="true" /> if the second object is earlier than or equal to the first object, otherwise
/// <see langword="false" />.
/// </returns>
public static Boolean operator <=(TimeOfDay a, TimeOfDay b) => a.CompareTo(b) < 1;
/// <summary>
/// Determines whether or not two specified <see cref="TimeOfDay" /> instances are equal.
/// </summary>
/// <param name="a">
/// The first <see cref="TimeOfDay" /> instance to compare.
/// </param>
/// <param name="b">
/// The second <see cref="TimeOfDay" /> instance to compare.
/// </param>
/// <returns>
/// A value indicating whether or not the specified instances are equal.
/// </returns>
public static Boolean operator ==(TimeOfDay a, TimeOfDay b)
{
if ((Object)a is null && (Object)b is null)
{
return true;
}
else if ((Object)a is null || (Object)b is null)
{
return false;
}
return a.Equals(b);
}
/// <summary>
/// Determines whether or not a specified <see cref="TimeOfDay" /> instance is greater than another specified instance.
/// </summary>
/// <param name="a">
/// The first <see cref="TimeOfDay" /> instance to compare.
/// </param>
/// <param name="b">
/// The second <see cref="TimeOfDay" /> instance to compare.
/// </param>
/// <returns>
/// <see langword="true" /> if the second object is later than the first object, otherwise <see langword="false" />.
/// </returns>
public static Boolean operator >(TimeOfDay a, TimeOfDay b) => a.CompareTo(b) == 1;
/// <summary>
/// Determines whether or not a specified <see cref="TimeOfDay" /> instance is greater than or equal to another supplied
/// instance.
/// </summary>
/// <param name="a">
/// The first <see cref="TimeOfDay" /> instance to compare.
/// </param>
/// <param name="b">
/// The second <see cref="TimeOfDay" /> instance to compare.
/// </param>
/// <returns>
/// <see langword="true" /> if the second object is later than or equal to the first object, otherwise
/// <see langword="false" />.
/// </returns>
public static Boolean operator >=(TimeOfDay a, TimeOfDay b) => a.CompareTo(b) > -1;
/// <summary>
/// Converts the specified <see cref="String" /> representation of a time of day value to its <see cref="TimeOfDay" />
/// equivalent.
/// </summary>
/// <param name="input">
/// A <see cref="String" /> containing a time of day value to convert.
/// </param>
/// <returns>
/// A <see cref="TimeOfDay" /> that is equivalent to <paramref name="input" />.
/// </returns>
/// <exception cref="ArgumentEmptyException">
/// <paramref name="input" /> is empty.
/// </exception>
/// <exception cref="ArgumentNullException">
/// <paramref name="input" /> is <see langword="null" />.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// A specified numeric component is less than zero or greater than the logical maximum (eg. hour 25).
/// </exception>
/// <exception cref="FormatException">
/// <paramref name="input" /> does not contain a valid representation of a time of day value.
/// </exception>
public static TimeOfDay Parse(String input)
{
if (Parse(input, out var zone, out var hour, out var minute, out var second, out var millisecond, true))
{
return new TimeOfDay(zone, hour, minute, second, millisecond);
}
return null;
}
/// <summary>
/// Converts the specified <see cref="String" /> representation of a time of day value to its <see cref="TimeOfDay" />
/// equivalent. The method returns a value that indicates whether the conversion succeeded.
/// </summary>
/// <param name="input">
/// A <see cref="String" /> containing a time of day value to convert.
/// </param>
/// <param name="result">
/// The parsed result if the operation is successful, otherwise the default instance.
/// </param>
/// <returns>
/// <see langword="true" /> if the parse operation is successful, otherwise <see langword="false" />.
/// </returns>
public static Boolean TryParse(String input, out TimeOfDay result)
{
if (Parse(input, out var zone, out var hour, out var minute, out var second, out var millisecond, false) == false)
{
result = null;
return false;
}
else if (hour < 0 || hour > 23)
{
result = null;
return false;
}
else if (minute < 0 || minute > 59)
{
result = null;
return false;
}
else if (second < 0 || second > 59)
{
result = null;
return false;
}
else if (millisecond < 0 || millisecond > 999)
{
result = null;
return false;
}
result = new TimeOfDay(zone, hour, minute, second, millisecond);
return true;
}
/// <summary>
/// Defines a <see cref="TimeOfDay" /> representing the start of the hour of the current <see cref="TimeOfDay" />.
/// </summary>
/// <returns>
/// A <see cref="TimeOfDay" /> representing the start of the hour of the current <see cref="TimeOfDay" />.
/// </returns>
public TimeOfDay BeginningOfThisHour() => new TimeOfDay(Zone, Hour, 0, 0, 0);
/// <summary>
/// Defines a <see cref="TimeOfDay" /> representing the start of the minute of the current <see cref="TimeOfDay" />.
/// </summary>
/// <returns>
/// A <see cref="TimeOfDay" /> representing the start of the minute of the current <see cref="TimeOfDay" />.
/// </returns>
public TimeOfDay BeginningOfThisMinute() => new TimeOfDay(Zone, Hour, Minute, 0, 0);
/// <summary>
/// Defines a <see cref="TimeOfDay" /> representing the start of the second of the current <see cref="TimeOfDay" />.
/// </summary>
/// <returns>
/// A <see cref="TimeOfDay" /> representing the start of the second of the current <see cref="TimeOfDay" />.
/// </returns>
public TimeOfDay BeginningOfThisSecond() => new TimeOfDay(Zone, Hour, Minute, Second, 0);
/// <summary>
/// Creates a new <see cref="TimeOfDay" /> that is an identical copy of the current <see cref="TimeOfDay" />.
/// </summary>
/// <returns>
/// A new <see cref="TimeOfDay" /> that is an identical copy of the current <see cref="TimeOfDay" />.
/// </returns>
public Object Clone() => new TimeOfDay(Zone, Hour, Minute, Second, Millisecond);
/// <summary>
/// Compares the current <see cref="TimeOfDay" /> to the specified object and returns an indication of their relative
/// values.
/// </summary>
/// <param name="other">
/// The <see cref="TimeOfDay" /> to compare to this instance.
/// </param>
/// <returns>
/// Negative one if this instance is earlier than the specified instance; one if this instance is later than the supplied
/// instance; zero if they are equal.
/// </returns>
public Int32 CompareTo(TimeOfDay other)
{
var thisInstanceCountOfMillisecondsPastMidnightUtc = CountOfMillisecondsPastMidnightUtc();
var otherInstanceCountOfMillisecondsPastMidnightUtc = other.CountOfMillisecondsPastMidnightUtc();
if (thisInstanceCountOfMillisecondsPastMidnightUtc < otherInstanceCountOfMillisecondsPastMidnightUtc)
{
return -1;
}
else if (thisInstanceCountOfMillisecondsPastMidnightUtc > otherInstanceCountOfMillisecondsPastMidnightUtc)
{
return 1;
}
else
{
return 0;
}
}
/// <summary>
/// Compares the current <see cref="TimeOfDay" /> to the specified object and returns an indication of their relative
/// values.
/// </summary>
/// <param name="obj">
/// The object to compare to this instance.
/// </param>
/// <returns>
/// Negative one if this instance is earlier than the specified instance; one if this instance is later than the supplied
/// instance; zero if they are equal.
/// </returns>
public Int32 CompareTo(Object obj) => obj is TimeOfDay timeOfDay ? CompareTo(timeOfDay) : GetType().FullName.CompareTo(obj.GetType().FullName);
/// <summary>
/// Determines whether or not the current <see cref="TimeOfDay" /> is equal to the specified <see cref="Object" />.
/// </summary>
/// <param name="obj">
/// The <see cref="Object" /> to compare to this instance.
/// </param>
/// <returns>
/// A value indicating whether or not the specified instances are equal.
/// </returns>
public override Boolean Equals(Object obj)
{
if (obj is null)
{
return false;
}
else if (obj is TimeOfDay timeOfDay)
{
return Equals(timeOfDay);
}
return false;
}
/// <summary>
/// Determines whether or not two specified <see cref="TimeOfDay" /> instances are equal.
/// </summary>
/// <param name="other">
/// The <see cref="TimeOfDay" /> to compare to this instance.
/// </param>
/// <returns>
/// A value indicating whether or not the specified instances are equal.
/// </returns>
public Boolean Equals(TimeOfDay other)
{
if ((Object)other is null)
{
return false;
}
else if (Millisecond != other.Millisecond)
{
return false;
}
else if (Second != other.Second)
{
return false;
}
else if (Minute != other.Minute)
{
return false;
}
else if (Hour != other.Hour)
{
return false;
}
else if (Zone.Id != other.Zone.Id)
{
return false;
}
return true;
}
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <returns>
/// A 32-bit signed integer hash code.
/// </returns>
public override Int32 GetHashCode() => ToByteArray().ComputeThirtyTwoBitHash();
/// <summary>
/// Converts the current <see cref="TimeOfDay" /> to an array of bytes.
/// </summary>
/// <returns>
/// An array of bytes representing the current <see cref="TimeOfDay" />.
/// </returns>
public Byte[] ToByteArray()
{
var bytes = new List<Byte>();
bytes.AddRange(Hour.ToByteArray());
bytes.AddRange(Minute.ToByteArray());
bytes.AddRange(Second.ToByteArray());
bytes.AddRange(Millisecond.ToByteArray());
bytes.AddRange(Zone.BaseUtcOffset.ToByteArray());
return bytes.ToArray();
}
/// <summary>
/// Converts the value of the current <see cref="TimeOfDay" /> to its equivalent string representation.
/// </summary>
/// <returns>
/// A string representation of the current <see cref="TimeOfDay" />.
/// </returns>
public override String ToString()
{
Int32? twelveHourClockFormatHourValue;
String meridiemStringFragment;
switch (Hour)
{
case 0:
twelveHourClockFormatHourValue = 12;
meridiemStringFragment = AnteMeridiemStringFragment;
break;
case 1:
twelveHourClockFormatHourValue = 1;
meridiemStringFragment = AnteMeridiemStringFragment;
break;
case 2:
twelveHourClockFormatHourValue = 2;
meridiemStringFragment = AnteMeridiemStringFragment;
break;
case 3:
twelveHourClockFormatHourValue = 3;
meridiemStringFragment = AnteMeridiemStringFragment;
break;
case 4:
twelveHourClockFormatHourValue = 4;
meridiemStringFragment = AnteMeridiemStringFragment;
break;
case 5:
twelveHourClockFormatHourValue = 5;
meridiemStringFragment = AnteMeridiemStringFragment;
break;
case 6:
twelveHourClockFormatHourValue = 6;
meridiemStringFragment = AnteMeridiemStringFragment;
break;
case 7:
twelveHourClockFormatHourValue = 7;
meridiemStringFragment = AnteMeridiemStringFragment;
break;
case 8:
twelveHourClockFormatHourValue = 8;
meridiemStringFragment = AnteMeridiemStringFragment;
break;
case 9:
twelveHourClockFormatHourValue = 9;
meridiemStringFragment = AnteMeridiemStringFragment;
break;
case 10:
twelveHourClockFormatHourValue = 10;
meridiemStringFragment = AnteMeridiemStringFragment;
break;
case 11:
twelveHourClockFormatHourValue = 11;
meridiemStringFragment = AnteMeridiemStringFragment;
break;
case 12:
twelveHourClockFormatHourValue = 12;
meridiemStringFragment = PostMeridiemStringFragment;
break;
case 13:
twelveHourClockFormatHourValue = 1;
meridiemStringFragment = PostMeridiemStringFragment;
break;
case 14:
twelveHourClockFormatHourValue = 2;
meridiemStringFragment = PostMeridiemStringFragment;
break;
case 15:
twelveHourClockFormatHourValue = 3;
meridiemStringFragment = PostMeridiemStringFragment;
break;
case 16:
twelveHourClockFormatHourValue = 4;
meridiemStringFragment = PostMeridiemStringFragment;
break;
case 17:
twelveHourClockFormatHourValue = 5;
meridiemStringFragment = PostMeridiemStringFragment;
break;
case 18:
twelveHourClockFormatHourValue = 6;
meridiemStringFragment = PostMeridiemStringFragment;
break;
case 19:
twelveHourClockFormatHourValue = 7;
meridiemStringFragment = PostMeridiemStringFragment;
break;
case 20:
twelveHourClockFormatHourValue = 8;
meridiemStringFragment = PostMeridiemStringFragment;
break;
case 21:
twelveHourClockFormatHourValue = 9;
meridiemStringFragment = PostMeridiemStringFragment;
break;
case 22:
twelveHourClockFormatHourValue = 10;
meridiemStringFragment = PostMeridiemStringFragment;
break;
case 23:
twelveHourClockFormatHourValue = 11;
meridiemStringFragment = PostMeridiemStringFragment;
break;
default:
twelveHourClockFormatHourValue = default(Int32);
meridiemStringFragment = null;
break;
}
return $"{twelveHourClockFormatHourValue.Value}:{Minute:00}:{Second:00}.{Millisecond:000} {meridiemStringFragment} {Zone.Id}";
}
/// <summary>
/// Converts the current <see cref="TimeOfDay" /> to Coordinated Universal Time (UTC) using the base UTC offset.
/// </summary>
/// <returns>
/// The current <see cref="TimeOfDay" /> instance converted to Coordinated Universal Time (UTC) using the base UTC offset.
/// </returns>
public TimeOfDay ToUniversalTime()
{
if (Zone == TimeZoneInfo.Utc)
{
return this;
}
var utcHour = Hour + Zone.BaseUtcOffset.Hours;
var utcMinute = Minute + Zone.BaseUtcOffset.Minutes;
if (utcMinute < 0)
{
// Adjust one hour backward.
utcHour -= 1;
utcMinute += 60;
}
else if (utcMinute > 59)
{
// Adjust one hour forward.
utcHour += 1;
utcMinute -= 60;
}
if (utcHour < 0)
{
// Adjust one day backward.
utcHour += 24;
}
else if (utcHour > 23)
{
// Adjust one day forward.
utcHour -= 24;
}
return new TimeOfDay(TimeZoneInfo.Utc, utcHour, utcMinute, Second, Millisecond);
}
/// <summary>
/// Converts the specified <see cref="String" /> representation of a time of day value to its <see cref="TimeOfDay" />
/// equivalent.
/// </summary>
/// <param name="input">
/// A <see cref="String" /> containing a time of day value to convert.
/// </param>
/// <param name="zone">
/// A reference to the <see cref="TimeZoneInfo" /> component of the resulting <see cref="TimeOfDay" />.
/// </param>
/// <param name="hour">
/// A reference to the hour component of the resulting <see cref="TimeOfDay" />.
/// </param>
/// <param name="minute">
/// A reference to the minute component of the resulting <see cref="TimeOfDay" />.
/// </param>
/// <param name="second">
/// A reference to the second component of the resulting <see cref="TimeOfDay" />.
/// </param>
/// <param name="millisecond">
/// A reference to the millisecond component of the resulting <see cref="TimeOfDay" />.
/// </param>
/// <param name="raiseExceptionOnFail">
/// A value indicating whether or not an exception should be raised if the parse operation fails.
/// </param>
/// <returns>
/// <see langword="true" /> if the parse operation is successful, otherwise <see langword="false" />.
/// </returns>
/// <exception cref="ArgumentEmptyException">
/// <paramref name="input" /> is empty.
/// </exception>
/// <exception cref="ArgumentNullException">
/// <paramref name="input" /> is <see langword="null" />.
/// </exception>
/// <exception cref="FormatException">
/// <paramref name="input" /> does not contain a valid representation of a time of day value.
/// </exception>
[DebuggerHidden]
private static Boolean Parse(String input, out TimeZoneInfo zone, out Int32 hour, out Int32 minute, out Int32 second, out Int32 millisecond, Boolean raiseExceptionOnFail)
{
if (raiseExceptionOnFail)
{
if (input is null)
{
throw new ArgumentNullException(nameof(input));
}
else if (input.Length == 0)
{
throw new ArgumentEmptyException(nameof(input));
}
}
else if (input.IsNullOrEmpty())
{
zone = null;
hour = default;
minute = default;
second = default;
millisecond = default;
return false;
}
var rawValueSubstrings = input.Compress().Split(' ');
if (rawValueSubstrings.Length < 3)
{
if (raiseExceptionOnFail)
{
throw new FormatException(ParseFormatExceptionMessageTemplate.ApplyFormat(input, nameof(TimeOfDay)));
}
zone = null;
hour = default;
minute = default;
second = default;
millisecond = default;
return false;
}
var timeStringFragment = rawValueSubstrings[0];
var timeValueSubstrings = timeStringFragment.Split(':');
String hourString;
String minuteString;
String secondString;
String millisecondString;
if (timeValueSubstrings.Length != 3)
{
if (raiseExceptionOnFail)
{
throw new FormatException(ParseFormatExceptionMessageTemplate.ApplyFormat(input, nameof(TimeOfDay)));
}
zone = null;
hour = default;
minute = default;
second = default;
millisecond = default;
return false;
}
hourString = timeValueSubstrings[0];
minuteString = timeValueSubstrings[1];
var secondAndMillisecondSubstrings = timeValueSubstrings[2].Split('.');
if (secondAndMillisecondSubstrings.Length != 2)
{
if (raiseExceptionOnFail)
{
throw new FormatException(ParseFormatExceptionMessageTemplate.ApplyFormat(input, nameof(TimeOfDay)));
}
zone = null;
hour = default;
minute = default;
second = default;
millisecond = default;
return false;
}
secondString = secondAndMillisecondSubstrings[0];
millisecondString = secondAndMillisecondSubstrings[1];
try
{
if (raiseExceptionOnFail)
{
hour = Int32.Parse(hourString);
minute = Int32.Parse(minuteString);
second = Int32.Parse(secondString);
millisecond = Int32.Parse(millisecondString);
}
else if (Int32.TryParse(hourString, out hour) == false)
{
zone = null;
hour = default;
minute = default;
second = default;
millisecond = default;
return false;
}
else if (Int32.TryParse(minuteString, out minute) == false)
{
zone = null;
hour = default;
minute = default;
second = default;
millisecond = default;
return false;
}
else if (Int32.TryParse(secondString, out second) == false)
{
zone = null;
hour = default;
minute = default;
second = default;
millisecond = default;
return false;
}
else if (Int32.TryParse(millisecondString, out millisecond) == false)
{
zone = null;
hour = default;
minute = default;
second = default;
millisecond = default;
return false;
}
if (hour == 12)
{
hour = 0;
}
if (rawValueSubstrings[1].ToUpper() == PostMeridiemStringFragment)
{
// Adjust for the twelve hour format.
hour += 12;
}
var zoneStringFragmentCount = (rawValueSubstrings.Length - 2);
var zoneStringFragment = zoneStringFragmentCount == 1 ? rawValueSubstrings[2] : String.Join(" ", rawValueSubstrings.Skip(2).Take(zoneStringFragmentCount).ToArray(), 0, zoneStringFragmentCount);
if (raiseExceptionOnFail)
{
// Attempt to match the specified time zone against the list of system time zones. Hard fault upon failure.
zone = TimeZoneInfo.FindSystemTimeZoneById(zoneStringFragment);
}
else
{
// Attempt to match the specified time zone against the list of system time zones.
zone = TimeZoneInfo.GetSystemTimeZones().Where(systemTimeZone => systemTimeZone.Id.ToLower() == zoneStringFragment.ToLower()).FirstOrDefault();
if (zone is null)
{
return false;
}
}
}
catch (FormatException exception)
{
throw new FormatException(ParseFormatExceptionMessageTemplate.ApplyFormat(input, nameof(TimeOfDay)), exception);
}
catch (IndexOutOfRangeException exception)
{
throw new FormatException(ParseFormatExceptionMessageTemplate.ApplyFormat(input, nameof(TimeOfDay)), exception);
}
catch (InvalidTimeZoneException exception)
{
throw new FormatException(ParseFormatExceptionMessageTemplate.ApplyFormat(input, nameof(TimeOfDay)), exception);
}
return true;
}
/// <summary>
/// Counts how many milliseconds past midnight in the local timezone the current <see cref="TimeOfDay" /> represents.
/// </summary>
/// <returns>
/// The number of milliseconds past midnight in the local timezone that the current instance represents.
/// </returns>
[DebuggerHidden]
private Int32 CountOfMillisecondsPastMidnightLocal() => ((Convert.ToInt32(Hour) * 3600000) + (Convert.ToInt32(Minute) * 60000) + (Convert.ToInt32(Second) * 1000) + Convert.ToInt32(Millisecond));
/// <summary>
/// Counts how many milliseconds past midnight in Coordinated Universal Time (UTC) the current <see cref="TimeOfDay" />
/// represents.
/// </summary>
/// <returns>
/// The number of milliseconds past midnight UTC that the current instance represents.
/// </returns>
[DebuggerHidden]
private Int32 CountOfMillisecondsPastMidnightUtc() => (CountOfMillisecondsPastMidnightLocal() + (Zone.BaseUtcOffset.Hours * 3600000));
/// <summary>
/// Initializes or reinitializes the instance using the specified serialized representation.
/// </summary>
/// <param name="input">
/// A string representation of a <see cref="TimeOfDay" />.
/// </param>
/// <exception cref="ArgumentEmptyException">
/// <paramref name="input" /> is empty.
/// </exception>
/// <exception cref="ArgumentNullException">
/// <paramref name="input" /> is <see langword="null" />.
/// </exception>
/// <exception cref="FormatException">
/// <paramref name="input" /> does not contain a valid representation of a time of day value.
/// </exception>
[DebuggerHidden]
private void Initialize(String input)
{
Parse(input, out var zone, out var hour, out var minute, out var second, out var millisecond, true);
Zone = zone;
Hour = hour;
Minute = minute;
Second = second;
Millisecond = millisecond;
}
/// <summary>
/// Gets a <see cref="TimeOfDay" /> representing the current time of day in the local time zone for this machine.
/// </summary>
public static TimeOfDay NowLocal => DateTime.Now.ToTimeOfDay();
/// <summary>
/// Gets a <see cref="TimeOfDay" /> representing the current time of day in the Coordinated Universal Time (UTC) Zone.
/// </summary>
public static TimeOfDay NowUtc => DateTime.UtcNow.ToTimeOfDay();
/// <summary>
/// Gets the hour component (0 through 23) of the time represented by this instance.
/// </summary>
[IgnoreDataMember]
public Int32 Hour
{
get;
private set;
}
/// <summary>
/// Gets the millisecond component (0 through 999) of the time represented by this instance.
/// </summary>
[IgnoreDataMember]
public Int32 Millisecond
{
get;
private set;
}
/// <summary>
/// Gets the minute component (0 through 59) of the time represented by this instance.
/// </summary>
[IgnoreDataMember]
public Int32 Minute
{
get;
private set;
}
/// <summary>
/// Gets the second component (0 through 59) of the time represented by this instance.
/// </summary>
[IgnoreDataMember]
public Int32 Second
{
get;
private set;
}
/// <summary>
/// Gets the time zone for the current <see cref="TimeOfDay" />.
/// </summary>
[IgnoreDataMember]
public TimeZoneInfo Zone
{
get;
private set;
}
/// <summary>
/// Gets or sets a serialized representation of the current <see cref="TimeOfDay" />.
/// </summary>
[DataMember(EmitDefaultValue = true, IsRequired = true, Name = "Value")]
private String SerializationValue
{
[DebuggerHidden]