-
-
Notifications
You must be signed in to change notification settings - Fork 408
/
Copy pathSmartDate.cs
1364 lines (1223 loc) · 40.4 KB
/
SmartDate.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
//-----------------------------------------------------------------------
// <copyright file="SmartDate.cs" company="Marimer LLC">
// Copyright (c) Marimer LLC. All rights reserved.
// Website: https://cslanet.com
// </copyright>
// <summary>Provides a date data type that understands the concept</summary>
//-----------------------------------------------------------------------
using Csla.Properties;
using Csla.Serialization.Mobile;
namespace Csla
{
/// <summary>
/// Provides a date data type that understands the concept
/// of an empty date value.
/// </summary>
/// <remarks>
/// See Chapter 5 for a full discussion of the need for this
/// data type and the design choices behind it.
/// </remarks>
[Serializable]
[System.ComponentModel.TypeConverter(typeof(Core.TypeConverters.SmartDateConverter))]
public struct SmartDate : Core.ISmartField,
IConvertible,
IComparable, IFormattable, IMobileObject
{
private DateTime _date;
private bool _initialized;
private EmptyValue _emptyValue;
private string _format;
private static string _defaultFormat;
[NonSerialized]
[NotUndoable]
private static Func<string, DateTime?> _customParser;
#region EmptyValue enum
/// <summary>
/// Indicates the empty value of a
/// SmartDate.
/// </summary>
public enum EmptyValue
{
/// <summary>
/// Indicates that an empty SmartDate
/// is the smallest date.
/// </summary>
MinDate,
/// <summary>
/// Indicates that an empty SmartDate
/// is the largest date.
/// </summary>
MaxDate
}
#endregion
#region Constructors
static SmartDate()
{
_defaultFormat = "d";
}
/// <summary>
/// Creates a new SmartDate object.
/// </summary>
/// <param name="emptyIsMin">Indicates whether an empty date is the min or max date value.</param>
public SmartDate(bool emptyIsMin)
{
_emptyValue = GetEmptyValue(emptyIsMin);
_format = null;
_initialized = false;
// provide a dummy value to allow real initialization
_date = DateTime.MinValue;
SetEmptyDate(_emptyValue);
}
/// <summary>
/// Creates a new SmartDate object.
/// </summary>
/// <param name="emptyValue">Indicates whether an empty date is the min or max date value.</param>
public SmartDate(EmptyValue emptyValue)
{
_emptyValue = emptyValue;
_format = null;
_initialized = false;
// provide a dummy value to allow real initialization
_date = DateTime.MinValue;
SetEmptyDate(_emptyValue);
}
/// <summary>
/// Creates a new SmartDate object.
/// </summary>
/// <remarks>
/// The SmartDate created will use the min possible
/// date to represent an empty date.
/// </remarks>
/// <param name="value">The initial value of the object.</param>
public SmartDate(DateTime value)
{
_emptyValue = EmptyValue.MinDate;
_format = null;
_initialized = false;
_date = DateTime.MinValue;
Date = value;
}
/// <summary>
/// Creates a new SmartDate object.
/// </summary>
/// <param name="value">The initial value of the object.</param>
/// <param name="emptyIsMin">Indicates whether an empty date is the min or max date value.</param>
public SmartDate(DateTime value, bool emptyIsMin)
{
_emptyValue = GetEmptyValue(emptyIsMin);
_format = null;
_initialized = false;
_date = DateTime.MinValue;
Date = value;
}
/// <summary>
/// Creates a new SmartDate object.
/// </summary>
/// <param name="value">The initial value of the object.</param>
/// <param name="emptyValue">Indicates whether an empty date is the min or max date value.</param>
public SmartDate(DateTime value, EmptyValue emptyValue)
{
_emptyValue = emptyValue;
_format = null;
_initialized = false;
_date = DateTime.MinValue;
Date = value;
}
/// <summary>
/// Creates a new SmartDate object.
/// </summary>
/// <param name="value">The initial value of the object.</param>
/// <param name="emptyValue">Indicates whether an empty date is the min or max date value.</param>
/// <param name="kind">One of the DateTimeKind values.</param>
public SmartDate(DateTime value, EmptyValue emptyValue, DateTimeKind kind)
{
_emptyValue = emptyValue;
_format = null;
_initialized = false;
_date = DateTime.MinValue;
Date = DateTime.SpecifyKind(value, kind);
}
/// <summary>
/// Creates a new SmartDate object.
/// </summary>
/// <remarks>
/// The SmartDate created will use the min possible
/// date to represent an empty date.
/// </remarks>
/// <param name="value">The initial value of the object.</param>
public SmartDate(DateTime? value)
{
_emptyValue = EmptyValue.MinDate;
_format = null;
_initialized = false;
_date = DateTime.MinValue;
if (value.HasValue)
Date = value.Value;
}
/// <summary>
/// Creates a new SmartDate object.
/// </summary>
/// <param name="value">The initial value of the object.</param>
/// <param name="emptyIsMin">Indicates whether an empty date is the min or max date value.</param>
public SmartDate(DateTime? value, bool emptyIsMin)
{
_emptyValue = GetEmptyValue(emptyIsMin);
_format = null;
_initialized = false;
_date = DateTime.MinValue;
if (value.HasValue)
Date = value.Value;
}
/// <summary>
/// Creates a new SmartDate object.
/// </summary>
/// <param name="value">The initial value of the object.</param>
/// <param name="emptyValue">Indicates whether an empty date is the min or max date value.</param>
public SmartDate(DateTime? value, EmptyValue emptyValue)
{
_emptyValue = emptyValue;
_format = null;
_initialized = false;
_date = DateTime.MinValue;
if (value.HasValue)
Date = value.Value;
}
/// <summary>
/// Creates a new SmartDate object.
/// </summary>
/// <remarks>
/// <para>
/// The SmartDate created will use the min possible
/// date to represent an empty date.
/// </para><para>
/// SmartDate maintains the date value as a DateTime,
/// so the provided DateTimeOffset is converted to a
/// DateTime in this constructor. You should be aware
/// that this can lead to a loss of precision in
/// some cases.
/// </para>
/// </remarks>
/// <param name="value">The initial value of the object.</param>
public SmartDate(DateTimeOffset value)
{
_emptyValue = EmptyValue.MinDate;
_format = null;
_initialized = false;
_date = DateTime.MinValue;
Date = value.DateTime;
}
/// <summary>
/// Creates a new SmartDate object.
/// </summary>
/// <param name="value">The initial value of the object.</param>
/// <param name="emptyIsMin">Indicates whether an empty date is the min or max date value.</param>
/// <remarks>
/// SmartDate maintains the date value as a DateTime,
/// so the provided DateTimeOffset is converted to a
/// DateTime in this constructor. You should be aware
/// that this can lead to a loss of precision in
/// some cases.
/// </remarks>
public SmartDate(DateTimeOffset value, bool emptyIsMin)
{
_emptyValue = GetEmptyValue(emptyIsMin);
_format = null;
_initialized = false;
_date = DateTime.MinValue;
Date = value.DateTime;
}
/// <summary>
/// Creates a new SmartDate object.
/// </summary>
/// <param name="value">The initial value of the object.</param>
/// <param name="emptyValue">Indicates whether an empty date is the min or max date value.</param>
/// <remarks>
/// SmartDate maintains the date value as a DateTime,
/// so the provided DateTimeOffset is converted to a
/// DateTime in this constructor. You should be aware
/// that this can lead to a loss of precision in
/// some cases.
/// </remarks>
public SmartDate(DateTimeOffset value, EmptyValue emptyValue)
{
_emptyValue = emptyValue;
_format = null;
_initialized = false;
_date = DateTime.MinValue;
Date = value.DateTime;
}
/// <summary>
/// Creates a new SmartDate object.
/// </summary>
/// <remarks>
/// The SmartDate created will use the min possible
/// date to represent an empty date.
/// </remarks>
/// <param name="value">The initial value of the object (as text).</param>
public SmartDate(string value)
{
_emptyValue = EmptyValue.MinDate;
_format = null;
_initialized = true;
_date = DateTime.MinValue;
Text = value;
}
/// <summary>
/// Creates a new SmartDate object.
/// </summary>
/// <param name="value">The initial value of the object (as text).</param>
/// <param name="emptyIsMin">Indicates whether an empty date is the min or max date value.</param>
public SmartDate(string value, bool emptyIsMin)
{
_emptyValue = GetEmptyValue(emptyIsMin);
_format = null;
_initialized = true;
_date = DateTime.MinValue;
Text = value;
}
/// <summary>
/// Creates a new SmartDate object.
/// </summary>
/// <param name="value">The initial value of the object (as text).</param>
/// <param name="emptyValue">Indicates whether an empty date is the min or max date value.</param>
public SmartDate(string value, EmptyValue emptyValue)
{
_emptyValue = emptyValue;
_format = null;
_initialized = true;
_date = DateTime.MinValue;
Text = value;
}
private static EmptyValue GetEmptyValue(bool emptyIsMin)
{
if (emptyIsMin)
return EmptyValue.MinDate;
else
return EmptyValue.MaxDate;
}
private void SetEmptyDate(EmptyValue emptyValue)
{
if (emptyValue == EmptyValue.MinDate)
Date = DateTime.MinValue;
else
Date = DateTime.MaxValue;
}
#endregion
#region Text Support
/// <summary>
/// Sets the global default format string used by all new
/// SmartDate values going forward.
/// </summary>
/// <remarks>
/// The default global format string is "d" unless this
/// method is called to change that value. Existing SmartDate
/// values are unaffected by this method, only SmartDate
/// values created after calling this method are affected.
/// </remarks>
/// <param name="formatString">
/// The format string should follow the requirements for the
/// .NET System.String.Format statement.
/// </param>
public static void SetDefaultFormatString(string formatString)
{
_defaultFormat = formatString;
}
/// <summary>
/// Gets or sets the format string used to format a date
/// value when it is returned as text.
/// </summary>
/// <remarks>
/// The format string should follow the requirements for the
/// .NET System.String.Format statement.
/// </remarks>
/// <value>A format string.</value>
public string FormatString
{
get => _format ??= _defaultFormat;
set => _format = value;
}
/// <summary>
/// Gets or sets the date value.
/// </summary>
/// <remarks>
/// <para>
/// This property can be used to set the date value by passing a
/// text representation of the date. Any text date representation
/// that can be parsed by the .NET runtime is valid.
/// </para><para>
/// When the date value is retrieved via this property, the text
/// is formatted by using the format specified by the
/// <see cref="FormatString" /> property. The default is the
/// short date format (d).
/// </para>
/// </remarks>
public string Text
{
get { return DateToString(Date, FormatString, _emptyValue); }
set { Date = StringToDate(value, _emptyValue); }
}
#endregion
#region Date Support
/// <summary>
/// Gets or sets the date value.
/// </summary>
public DateTime Date
{
get
{
if (!_initialized)
{
_date = _emptyValue == EmptyValue.MinDate ? DateTime.MinValue : DateTime.MaxValue;
_initialized = true;
}
return _date;
}
set
{
_date = value;
_initialized = true;
}
}
/// <summary>
/// Gets the value as a DateTimeOffset.
/// </summary>
public DateTimeOffset ToDateTimeOffset()
{
return new DateTimeOffset(Date);
}
/// <summary>
/// Gets the value as a DateTime?.
/// </summary>
public DateTime? ToNullableDate()
{
if (IsEmpty)
return new DateTime?();
else
return new DateTime?(Date);
}
#endregion
#region System.Object overrides
/// <summary>
/// Returns a text representation of the date value.
/// </summary>
public override string ToString()
{
return Text;
}
/// <summary>
/// Returns a text representation of the date value.
/// </summary>
/// <param name="format">
/// A standard .NET format string.
/// </param>
public string ToString(string format)
{
if (string.IsNullOrEmpty(format))
return ToString();
else
return DateToString(Date, format, _emptyValue);
}
/// <summary>
/// Compares this object to another <see cref="SmartDate"/>
/// for equality.
/// </summary>
/// <param name="obj">Object to compare for equality.</param>
public override bool Equals(object obj)
{
if (obj is SmartDate tmp)
{
if (IsEmpty && tmp.IsEmpty)
return true;
else
return Date.Equals(tmp.Date);
}
else if (obj is DateTime time)
return Date.Equals(time);
else if (obj is string)
return (CompareTo(obj.ToString()) == 0);
else
return false;
}
/// <summary>
/// Returns a hash code for this object.
/// </summary>
public override int GetHashCode()
{
return Date.GetHashCode();
}
#endregion
#region DBValue
/// <summary>
/// Gets a database-friendly version of the date value.
/// </summary>
/// <remarks>
/// <para>
/// If the SmartDate contains an empty date, this returns <see cref="DBNull"/>.
/// Otherwise the actual date value is returned as type Date.
/// </para><para>
/// This property is very useful when setting parameter values for
/// a Command object, since it automatically stores null values into
/// the database for empty date values.
/// </para><para>
/// When you also use the SafeDataReader and its GetSmartDate method,
/// you can easily read a null value from the database back into a
/// SmartDate object so it remains considered as an empty date value.
/// </para>
/// </remarks>
public object DBValue
{
get
{
if (IsEmpty)
return DBNull.Value;
else
return Date;
}
}
#endregion
#region Empty Dates
/// <summary>
/// Gets a value indicating whether this object contains an empty date.
/// </summary>
public bool IsEmpty
{
get
{
if (_emptyValue == EmptyValue.MinDate)
return Date.Equals(DateTime.MinValue);
else
return Date.Equals(DateTime.MaxValue);
}
}
/// <summary>
/// Gets a value indicating whether an empty date is the
/// min or max possible date value.
/// </summary>
/// <remarks>
/// Whether an empty date is considered to be the smallest or largest possible
/// date is only important for comparison operations. This allows you to
/// compare an empty date with a real date and get a meaningful result.
/// </remarks>
public bool EmptyIsMin
{
get { return (_emptyValue == EmptyValue.MinDate); }
}
#endregion
#region Conversion Functions
/// <summary>
/// Gets or sets the custom parser.
///
/// The CustomParser is called first in TryStringToDate to allow custom parsing.
/// The parser method must return null if unable to parse and allow SmartDate to try default parsing.
/// </summary>
/// <value>
/// The custom parser.
/// </value>
public static Func<string, DateTime?> CustomParser
{
get { return _customParser; }
set { _customParser = value; }
}
/// <summary>
/// Converts a string value into a SmartDate.
/// </summary>
/// <param name="value">String containing the date value.</param>
/// <returns>A new SmartDate containing the date value.</returns>
/// <remarks>
/// EmptyIsMin will default to true.
/// </remarks>
public static SmartDate Parse(string value)
{
return new SmartDate(value);
}
/// <summary>
/// Converts a string value into a SmartDate.
/// </summary>
/// <param name="value">String containing the date value.</param>
/// <param name="emptyValue">Indicates whether an empty date is the min or max date value.</param>
/// <returns>A new SmartDate containing the date value.</returns>
public static SmartDate Parse(string value, EmptyValue emptyValue)
{
return new SmartDate(value, emptyValue);
}
/// <summary>
/// Converts a string value into a SmartDate.
/// </summary>
/// <param name="value">String containing the date value.</param>
/// <param name="emptyIsMin">Indicates whether an empty date is the min or max date value.</param>
/// <returns>A new SmartDate containing the date value.</returns>
public static SmartDate Parse(string value, bool emptyIsMin)
{
return new SmartDate(value, emptyIsMin);
}
/// <summary>
/// Converts a string value into a SmartDate.
/// </summary>
/// <param name="value">String containing the date value.</param>
/// <param name="result">The resulting SmartDate value if the parse was successful.</param>
/// <returns>A value indicating if the parse was successful.</returns>
public static bool TryParse(string value, ref SmartDate result)
{
return TryParse(value, EmptyValue.MinDate, ref result);
}
/// <summary>
/// Converts a string value into a SmartDate.
/// </summary>
/// <param name="value">String containing the date value.</param>
/// <param name="emptyValue">Indicates whether an empty date is the min or max date value.</param>
/// <param name="result">The resulting SmartDate value if the parse was successful.</param>
/// <returns>A value indicating if the parse was successful.</returns>
public static bool TryParse(string value, EmptyValue emptyValue, ref SmartDate result)
{
DateTime dateResult = DateTime.MinValue;
if (TryStringToDate(value, emptyValue, ref dateResult))
{
result = new SmartDate(dateResult, emptyValue);
return true;
}
else
{
return false;
}
}
/// <summary>
/// Converts a text date representation into a Date value.
/// </summary>
/// <remarks>
/// An empty string is assumed to represent an empty date. An empty date
/// is returned as the MinValue of the Date datatype.
/// </remarks>
/// <param name="value">The text representation of the date.</param>
/// <returns>A Date value.</returns>
public static DateTime StringToDate(string value)
{
return StringToDate(value, true);
}
/// <summary>
/// Converts a text date representation into a Date value.
/// </summary>
/// <remarks>
/// An empty string is assumed to represent an empty date. An empty date
/// is returned as the MinValue or MaxValue of the Date datatype depending
/// on the EmptyIsMin parameter.
/// </remarks>
/// <param name="value">The text representation of the date.</param>
/// <param name="emptyIsMin">Indicates whether an empty date is the min or max date value.</param>
/// <returns>A Date value.</returns>
public static DateTime StringToDate(string value, bool emptyIsMin)
{
return StringToDate(value, GetEmptyValue(emptyIsMin));
}
/// <summary>
/// Converts a text date representation into a Date value.
/// </summary>
/// <remarks>
/// An empty string is assumed to represent an empty date. An empty date
/// is returned as the MinValue or MaxValue of the Date datatype depending
/// on the EmptyIsMin parameter.
/// </remarks>
/// <param name="value">The text representation of the date.</param>
/// <param name="emptyValue">Indicates whether an empty date is the min or max date value.</param>
/// <returns>A Date value.</returns>
public static DateTime StringToDate(string value, EmptyValue emptyValue)
{
DateTime result = DateTime.MinValue;
if (TryStringToDate(value, emptyValue, ref result))
return result;
else
throw new ArgumentException(Resources.StringToDateException);
}
private static bool TryStringToDate(string value, EmptyValue emptyValue, ref DateTime result)
{
DateTime tmp;
// call custom parser if set...
var tmpValue = _customParser?.Invoke(value);
// i f custom parser returned a value then parsing succeeded
if (tmpValue.HasValue)
{
result = tmpValue.Value;
return true;
}
if (String.IsNullOrEmpty(value))
{
result = emptyValue == EmptyValue.MinDate ? DateTime.MinValue : DateTime.MaxValue;
return true;
}
if (DateTime.TryParse(value, out tmp))
{
result = tmp;
return true;
}
string ldate = value.Trim().ToLower();
if (ldate == Resources.SmartDateT ||
ldate == Resources.SmartDateToday ||
ldate == ".")
{
result = DateTime.Now;
return true;
}
if (ldate == Resources.SmartDateY ||
ldate == Resources.SmartDateYesterday ||
ldate == "-")
{
result = DateTime.Now.AddDays(-1);
return true;
}
if (ldate == Resources.SmartDateTom ||
ldate == Resources.SmartDateTomorrow ||
ldate == "+")
{
result = DateTime.Now.AddDays(1);
return true;
}
return false;
}
/// <summary>
/// Converts a date value into a text representation.
/// </summary>
/// <remarks>
/// The date is considered empty if it matches the min value for
/// the Date datatype. If the date is empty, this
/// method returns an empty string. Otherwise it returns the date
/// value formatted based on the FormatString parameter.
/// </remarks>
/// <param name="value">The date value to convert.</param>
/// <param name="formatString">The format string used to format the date into text.</param>
/// <returns>Text representation of the date value.</returns>
public static string DateToString(
DateTime value, string formatString)
{
return DateToString(value, formatString, true);
}
/// <summary>
/// Converts a date value into a text representation.
/// </summary>
/// <remarks>
/// Whether the date value is considered empty is determined by
/// the EmptyIsMin parameter value. If the date is empty, this
/// method returns an empty string. Otherwise it returns the date
/// value formatted based on the FormatString parameter.
/// </remarks>
/// <param name="value">The date value to convert.</param>
/// <param name="formatString">The format string used to format the date into text.</param>
/// <param name="emptyIsMin">Indicates whether an empty date is the min or max date value.</param>
/// <returns>Text representation of the date value.</returns>
public static string DateToString(
DateTime value, string formatString, bool emptyIsMin)
{
return DateToString(value, formatString, GetEmptyValue(emptyIsMin));
}
/// <summary>
/// Converts a date value into a text representation.
/// </summary>
/// <remarks>
/// Whether the date value is considered empty is determined by
/// the EmptyIsMin parameter value. If the date is empty, this
/// method returns an empty string. Otherwise it returns the date
/// value formatted based on the FormatString parameter.
/// </remarks>
/// <param name="value">The date value to convert.</param>
/// <param name="formatString">The format string used to format the date into text.</param>
/// <param name="emptyValue">Indicates whether an empty date is the min or max date value.</param>
/// <returns>Text representation of the date value.</returns>
public static string DateToString(
DateTime value, string formatString, EmptyValue emptyValue)
{
if (emptyValue == EmptyValue.MinDate)
{
if (value == DateTime.MinValue)
return string.Empty;
}
else
{
if (value == DateTime.MaxValue)
return string.Empty;
}
return string.Format("{0:" + formatString + "}", value);
}
#endregion
#region Manipulation Functions
/// <summary>
/// Compares one SmartDate to another.
/// </summary>
/// <remarks>
/// This method works the same as the DateTime.CompareTo method
/// on the Date datetype, with the exception that it
/// understands the concept of empty date values.
/// </remarks>
/// <param name="value">The date to which we are being compared.</param>
/// <returns>A value indicating if the comparison date is less than, equal to or greater than this date.</returns>
public int CompareTo(SmartDate value)
{
if (IsEmpty && value.IsEmpty)
return 0;
else
return _date.CompareTo(value.Date);
}
/// <summary>
/// Compares one SmartDate to another.
/// </summary>
/// <remarks>
/// This method works the same as the DateTime.CompareTo method
/// on the Date datetype, with the exception that it
/// understands the concept of empty date values.
/// </remarks>
/// <param name="value">The date to which we are being compared.</param>
/// <returns>A value indicating if the comparison date is less than, equal to or greater than this date.</returns>
int IComparable.CompareTo(object value)
{
if (value is SmartDate date)
return CompareTo(date);
else
throw new ArgumentException(Resources.ValueNotSmartDateException);
}
/// <summary>
/// Compares a SmartDate to a text date value.
/// </summary>
/// <param name="value">The date to which we are being compared.</param>
/// <returns>A value indicating if the comparison date is less than, equal to or greater than this date.</returns>
public int CompareTo(string value)
{
return Date.CompareTo(StringToDate(value, _emptyValue));
}
/// <summary>
/// Compares a SmartDate to a date value.
/// </summary>
/// <param name="value">The date to which we are being compared.</param>
/// <returns>A value indicating if the comparison date is less than, equal to or greater than this date.</returns>
/// <remarks>
/// SmartDate maintains the date value as a DateTime,
/// so the provided DateTimeOffset is converted to a
/// DateTime for this comparison. You should be aware
/// that this can lead to a loss of precision in
/// some cases.
/// </remarks>
public int CompareTo(DateTimeOffset value)
{
return Date.CompareTo(value.DateTime);
}
/// <summary>
/// Compares a SmartDate to a date value.
/// </summary>
/// <param name="value">The date to which we are being compared.</param>
/// <returns>A value indicating if the comparison date is less than, equal to or greater than this date.</returns>
public int CompareTo(DateTime value)
{
return Date.CompareTo(value);
}
/// <summary>
/// Adds a TimeSpan onto the object.
/// </summary>
/// <param name="value">Span to add to the date.</param>
public DateTime Add(TimeSpan value)
{
if (IsEmpty)
return Date;
else
return Date.Add(value);
}
/// <summary>
/// Subtracts a TimeSpan from the object.
/// </summary>
/// <param name="value">Span to subtract from the date.</param>
public DateTime Subtract(TimeSpan value)
{
if (IsEmpty)
return Date;
else
return Date.Subtract(value);
}
/// <summary>
/// Subtracts a DateTimeOffset from the object.
/// </summary>
/// <param name="value">DateTimeOffset to subtract from the date.</param>
/// <remarks>
/// SmartDate maintains the date value as a DateTime,
/// so the provided DateTimeOffset is converted to a
/// DateTime for this comparison. You should be aware
/// that this can lead to a loss of precision in
/// some cases.
/// </remarks>
public TimeSpan Subtract(DateTimeOffset value)
{
if (IsEmpty)
return TimeSpan.Zero;
else
return Date.Subtract(value.DateTime);
}
/// <summary>
/// Subtracts a DateTime from the object.
/// </summary>
/// <param name="value">Date to subtract from the date.</param>
public TimeSpan Subtract(DateTime value)
{
if (IsEmpty)
return TimeSpan.Zero;
else
return Date.Subtract(value);
}
#endregion
#region Operators
/// <summary>
/// Equality operator
/// </summary>
/// <param name="obj1">First object</param>
/// <param name="obj2">Second object</param>
public static bool operator ==(SmartDate obj1, SmartDate obj2)
{
return obj1.Equals(obj2);
}
/// <summary>
/// Inequality operator
/// </summary>
/// <param name="obj1">First object</param>
/// <param name="obj2">Second object</param>
public static bool operator !=(SmartDate obj1, SmartDate obj2)
{
return !obj1.Equals(obj2);
}
/// <summary>
/// Convert a SmartDate to a String.
/// </summary>
/// <param name="obj1">SmartDate value.</param>
public static implicit operator string(SmartDate obj1)
{
return obj1.Text;
}
/// <summary>
/// Convert a SmartDate to a DateTime.
/// </summary>
/// <param name="obj1">SmartDate value.</param>
public static implicit operator DateTime(SmartDate obj1)
{
return obj1.Date;
}
/// <summary>
/// Convert a SmartDate to a nullable DateTime.
/// </summary>
/// <param name="obj1">SmartDate value.</param>
public static implicit operator DateTime?(SmartDate obj1)
{
return obj1.ToNullableDate();
}
/// <summary>
/// Convert a SmartDate to a DateTimeOffset.
/// </summary>
/// <param name="obj1">SmartDate value.</param>
public static implicit operator DateTimeOffset(SmartDate obj1)
{
return obj1.ToDateTimeOffset();
}