34 private bool _initialized;
36 private string _format;
37 private static string _defaultFormat;
43 private static Func<string, DateTime?> _customParser;
45#region EmptyValue enum
80 _emptyValue = GetEmptyValue(emptyIsMin);
85 SetEmptyDate(_emptyValue);
94 _emptyValue = emptyValue;
99 SetEmptyDate(_emptyValue);
114 _initialized =
false;
126 _emptyValue = GetEmptyValue(emptyIsMin);
128 _initialized =
false;
140 _emptyValue = emptyValue;
142 _initialized =
false;
155 _emptyValue = emptyValue;
157 _initialized =
false;
174 _initialized =
false;
187 _emptyValue = GetEmptyValue(emptyIsMin);
189 _initialized =
false;
202 _emptyValue = emptyValue;
204 _initialized =
false;
230 _initialized =
false;
232 Date = value.DateTime;
249 _emptyValue = GetEmptyValue(emptyIsMin);
251 _initialized =
false;
253 Date = value.DateTime;
270 _emptyValue = emptyValue;
272 _initialized =
false;
274 Date = value.DateTime;
301 _emptyValue = GetEmptyValue(emptyIsMin);
315 _emptyValue = emptyValue;
322 private static EmptyValue GetEmptyValue(
bool emptyIsMin)
330 private void SetEmptyDate(
EmptyValue emptyValue)
332 if (emptyValue ==
SmartDate.EmptyValue.MinDate)
358 _defaultFormat = formatString;
375 _format = _defaultFormat;
451#region System.Object overrides
469 if (
string.IsNullOrEmpty(format))
491 return this.
Date.Equals((DateTime)obj);
492 else if (obj is
string)
493 return (this.
CompareTo(obj.ToString()) == 0);
503 return this.
Date.GetHashCode();
569 get {
return (_emptyValue ==
EmptyValue.MinDate); }
574#region Conversion Functions
587 get {
return _customParser; }
588 set { _customParser = value; }
647 System.DateTime dateResult =
DateTime.MinValue;
648 if (TryStringToDate(value, emptyValue, ref dateResult))
650 result =
new SmartDate(dateResult, emptyValue);
703 if (TryStringToDate(value, emptyValue, ref result))
709 private static bool TryStringToDate(
string value,
EmptyValue emptyValue, ref DateTime result)
715 if (_customParser !=
null)
717 var tmpValue = _customParser.Invoke(value);
719 if (tmpValue.HasValue)
721 result = tmpValue.Value;
726 if (
String.IsNullOrEmpty(value))
731 if (
DateTime.TryParse(value, out tmp))
737 string ldate = value.Trim().ToLower();
776 DateTime value,
string formatString)
795 DateTime value,
string formatString,
bool emptyIsMin)
797 return DateToString(value, formatString, GetEmptyValue(emptyIsMin));
814 DateTime value,
string formatString,
EmptyValue emptyValue)
826 return string.Format(
"{0:" + formatString +
"}", value);
831#region Manipulation Functions
848 return _date.CompareTo(value.
Date);
861 int IComparable.CompareTo(
object value)
893 return this.
Date.CompareTo(value.DateTime);
903 return this.
Date.CompareTo(value);
915 return this.
Date.Add(value);
927 return this.
Date.Subtract(value);
946 return this.
Date.Subtract(value.DateTime);
958 return this.
Date.Subtract(value);
984 return !obj1.
Equals(obj2);
1000 public static implicit
operator System.DateTime(
SmartDate obj1)
1009 public static implicit
operator System.DateTime?(
SmartDate obj1)
1036 public static implicit
operator SmartDate(System.DateTime dateValue)
1045 public static implicit
operator SmartDate(System.DateTime? dateValue)
1054 public static explicit operator SmartDate(DateTimeOffset dateValue)
1067 return obj1.
Equals(obj2);
1078 return !obj1.
Equals(obj2);
1089 return obj1.
Equals(obj2);
1100 return !obj1.
Equals(obj2);
1273 System.TypeCode IConvertible.GetTypeCode()
1275 return ((IConvertible)_date).GetTypeCode();
1278 bool IConvertible.ToBoolean(System.IFormatProvider provider)
1280 return ((IConvertible)_date).ToBoolean(provider);
1283 byte IConvertible.ToByte(System.IFormatProvider provider)
1285 return ((IConvertible)_date).ToByte(provider);
1288 char IConvertible.ToChar(System.IFormatProvider provider)
1290 return ((IConvertible)_date).ToChar(provider);
1293 System.DateTime IConvertible.ToDateTime(System.IFormatProvider provider)
1295 return ((IConvertible)_date).ToDateTime(provider);
1298 decimal IConvertible.ToDecimal(System.IFormatProvider provider)
1300 return ((IConvertible)_date).ToDecimal(provider);
1303 double IConvertible.ToDouble(System.IFormatProvider provider)
1305 return ((IConvertible)_date).ToDouble(provider);
1308 short IConvertible.ToInt16(System.IFormatProvider provider)
1310 return ((IConvertible)_date).ToInt16(provider);
1313 int IConvertible.ToInt32(System.IFormatProvider provider)
1315 return ((IConvertible)_date).ToInt32(provider);
1318 long IConvertible.ToInt64(System.IFormatProvider provider)
1320 return ((IConvertible)_date).ToInt64(provider);
1323 sbyte IConvertible.ToSByte(System.IFormatProvider provider)
1325 return ((IConvertible)_date).ToSByte(provider);
1328 float IConvertible.ToSingle(System.IFormatProvider provider)
1330 return ((IConvertible)_date).ToSingle(provider);
1333 string IConvertible.ToString(System.IFormatProvider provider)
1335 return ((IConvertible)
Text).ToString(provider);
1338 object IConvertible.ToType(System.Type conversionType, System.IFormatProvider provider)
1340 if (conversionType.Equals(typeof(
string)))
1341 return ((IConvertible)
Text).ToType(conversionType, provider);
1342 else if (conversionType.Equals(typeof(
SmartDate)))
1345 return ((IConvertible)_date).ToType(conversionType, provider);
1348 ushort IConvertible.ToUInt16(System.IFormatProvider provider)
1350 return ((IConvertible)_date).ToUInt16(provider);
1353 uint IConvertible.ToUInt32(System.IFormatProvider provider)
1355 return ((IConvertible)_date).ToUInt32(provider);
1358 ulong IConvertible.ToUInt64(System.IFormatProvider provider)
1360 return ((IConvertible)_date).ToUInt64(provider);
1367#region IFormattable Members
1369 string IFormattable.ToString(
string format, IFormatProvider formatProvider)
1376#region IMobileObject Members
1380 info.
AddValue(
"SmartDate._date", _date);
1381 info.
AddValue(
"SmartDate._defaultFormat", _defaultFormat);
1382 info.
AddValue(
"SmartDate._emptyValue", _emptyValue.ToString());
1383 info.
AddValue(
"SmartDate._initialized", _initialized);
1384 info.
AddValue(
"SmartDate._format", _format);
1389 _date = info.GetValue<
DateTime>(
"SmartDate._date");
1390 _defaultFormat = info.GetValue<
string>(
"SmartDate._defaultFormat");
1391 _emptyValue = (
EmptyValue)System.Enum.Parse(typeof(
EmptyValue), info.GetValue<
string>(
"SmartDate._emptyValue"),
true);
1392 _format = info.GetValue<
string>(
"SmartDate._format");
1393 _initialized = info.GetValue<
bool>(
"SmartDate._initialized");
Converts values to and from a SmartDate.
A strongly-typed resource class, for looking up localized strings, etc.
static string SmartDateToday
Looks up a localized string similar to today.
static string StringToDateException
Looks up a localized string similar to String value can not be converted to a date.
static string SmartDateYesterday
Looks up a localized string similar to yesterday.
static string SmartDateY
Looks up a localized string similar to y.
static string ValueNotSmartDateException
Looks up a localized string similar to Value is not a SmartDate.
static string SmartDateTom
Looks up a localized string similar to tom.
static string SmartDateTomorrow
Looks up a localized string similar to tomorrow.
static string SmartDateT
Looks up a localized string similar to t.
Object containing the serialization data for a specific object.
void AddValue(string name, object value)
Adds a value to the serialization stream.
Interface to be implemented by any object that supports serialization by the SerializationFormatterFa...
void GetChildren(SerializationInfo info, MobileFormatter formatter)
Method called by MobileFormatter when an object should serialize its child references.
void GetState(SerializationInfo info)
Method called by MobileFormatter when an object should serialize its data.
void SetChildren(SerializationInfo info, MobileFormatter formatter)
Method called by MobileFormatter when an object should deserialize its child references.
void SetState(SerializationInfo info)
Method called by MobileFormatter when an object should be deserialized.
@ DateTimeOffset
Date/time plus time zone / DateTimeOffset
@ TimeSpan
TimeSpan - time span
@ Serializable
Prevents updating or inserting until the transaction is complete.
Provides a date data type that understands the concept of an empty date value.
static bool operator<(SmartDate obj1, SmartDate obj2)
Less than operator
static SmartDate Parse(string value, EmptyValue emptyValue)
Converts a string value into a SmartDate.
bool IsEmpty
Gets a value indicating whether this object contains an empty date.
static bool operator<=(SmartDate obj1, SmartDate obj2)
Less than or equals operator
DateTime? ToNullableDate()
Gets the value as a DateTime?.
static DateTime StringToDate(string value, EmptyValue emptyValue)
Converts a text date representation into a Date value.
SmartDate(bool emptyIsMin)
Creates a new SmartDate object.
static string DateToString(DateTime value, string formatString, EmptyValue emptyValue)
Converts a date value into a text representation.
static DateTime StringToDate(string value, bool emptyIsMin)
SmartDate(DateTime? value, bool emptyIsMin)
Creates a new SmartDate object.
SmartDate(EmptyValue emptyValue)
Creates a new SmartDate object.
SmartDate(DateTimeOffset value, bool emptyIsMin)
Creates a new SmartDate object.
SmartDate(DateTimeOffset value, EmptyValue emptyValue)
Creates a new SmartDate object.
EmptyValue
Indicates the empty value of a SmartDate.
DateTime? Date
Gets or sets the date value.
static DateTime StringToDate(string value)
Converts a text date representation into a Date value.
TimeSpan Subtract(DateTimeOffset value)
Subtracts a DateTimeOffset from the object.
override string ToString()
Returns a text representation of the date value.
int CompareTo(SmartDate value)
Compares one SmartDate to another.
static bool operator>(SmartDate obj1, SmartDate obj2)
Greater than operator
DateTime Subtract(TimeSpan value)
Subtracts a TimeSpan from the object.
DateTimeOffset ToDateTimeOffset()
Gets the value as a DateTimeOffset.
override int GetHashCode()
Returns a hash code for this object.
static SmartDate operator-(SmartDate start, TimeSpan span)
Subtraction operator
static bool TryParse(string value, ref SmartDate result)
Converts a string value into a SmartDate.
static SmartDate operator+(SmartDate start, TimeSpan span)
Addition operator
bool EmptyIsMin
Gets a value indicating whether an empty date is the min or max possible date value.
int CompareTo(DateTime value)
Compares a SmartDate to a date value.
static SmartDate Parse(string value, bool emptyIsMin)
Converts a string value into a SmartDate.
object DBValue
Gets a database-friendly version of the date value.
string ToString(string format)
Returns a text representation of the date value.
static bool TryParse(string value, EmptyValue emptyValue, ref SmartDate result)
Converts a string value into a SmartDate.
int CompareTo(string value)
Compares a SmartDate to a text date value.
SmartDate(DateTime value, bool emptyIsMin)
Creates a new SmartDate object.
SmartDate(string value)
Creates a new SmartDate object.
int CompareTo(DateTimeOffset value)
Compares a SmartDate to a date value.
SmartDate(DateTime? value)
Creates a new SmartDate object.
SmartDate(DateTime value)
Creates a new SmartDate object.
static SmartDate Parse(string value)
Converts a string value into a SmartDate.
static string DateToString(DateTime value, string formatString)
Converts a date value into a text representation.
string FormatString
Gets or sets the format string used to format a date value when it is returned as text.
override bool Equals(object obj)
Compares this object to another SmartDate for equality.
SmartDate(DateTime value, EmptyValue emptyValue)
Creates a new SmartDate object.
SmartDate(string value, EmptyValue emptyValue)
Creates a new SmartDate object.
SmartDate(DateTime value, EmptyValue emptyValue, DateTimeKind kind)
Creates a new SmartDate object.
static void SetDefaultFormatString(string formatString)
Sets the global default format string used by all new SmartDate values going forward.
string Text
Gets or sets the date value.
SmartDate(DateTime? value, EmptyValue emptyValue)
Creates a new SmartDate object.
static bool operator!=(SmartDate obj1, SmartDate obj2)
Inequality operator
static bool operator>=(SmartDate obj1, SmartDate obj2)
Greater than or equals operator
TimeSpan Subtract(DateTime value)
Subtracts a DateTime from the object.
DateTime Add(TimeSpan value)
Adds a TimeSpan onto the object.
SmartDate(string value, bool emptyIsMin)
Creates a new SmartDate object.
SmartDate(DateTimeOffset value)
Creates a new SmartDate object.
static Func< string, DateTime?> CustomParser
Gets or sets the custom parser.
static bool operator==(SmartDate obj1, SmartDate obj2)
Equality operator
static string DateToString(DateTime value, string formatString, bool emptyIsMin)
Converts a date value into a text representation.