using System; using Foundation; namespace Xamarin.PropertyEditing.Mac { internal static class DateExtensions { /// The NSDate from Xamarin takes a reference point form January 1, 2001, at 12:00 /// /// It also has calls for NIX reference point 1970 but appears to be problematic /// private static DateTime _nsRef = new DateTime (2001, 1, 1, 0, 0, 0, 0, DateTimeKind.Local); // last zero is millisecond /// Returns the seconds interval for a DateTime from NSDate reference data of January 1, 2001 /// The DateTime to evaluate /// The seconds since NSDate reference date public static double SecondsSinceNsRefenceDate (this DateTime dt) { return (dt - _nsRef).TotalSeconds; } /// Convert a DateTime to NSDate /// The DateTime to convert /// An NSDate public static NSDate ToNSDate (this DateTime dt) { return NSDate.FromTimeIntervalSinceReferenceDate (dt.SecondsSinceNsRefenceDate ()); } /// Convert an NSDate to DateTime /// The NSDate to convert /// A DateTime public static DateTime ToDateTime (this NSDate nsDate) { try { // We loose granularity below millisecond range but that is probably ok return _nsRef.AddSeconds (nsDate.SecondsSinceReferenceDate); } catch (ArgumentOutOfRangeException) { return _nsRef; } } } }