Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In Converting DateTime to NSDate the following extension methods are proposed:

C#
public static DateTime NSDateToDateTime(this NSDate date)
{
    DateTime reference = TimeZone.CurrentTimeZone.ToLocalTime( 
        new DateTime(2001, 1, 1, 0, 0, 0) );
    return reference.AddSeconds(date.SecondsSinceReferenceDate);
}

public static NSDate DateTimeToNSDate(this DateTime date)
{
    DateTime reference = TimeZone.CurrentTimeZone.ToLocalTime(
        new DateTime(2001, 1, 1, 0, 0, 0) );
    return NSDate.FromTimeIntervalSinceReferenceDate(
        (date - reference).TotalSeconds);
}


How can I use nullable DateTime with that conversion?

Edit:

Now I have the following

C#
public static DateTime? NSDateToDateTime(this NSDate date)
{
	if (date == null) {
		return null;
	} else {
		DateTime reference = TimeZone.CurrentTimeZone.ToLocalTime (new DateTime (2001, 1, 1, 0, 0, 0));
		return (DateTime?)reference.AddSeconds (date.SecondsSinceReferenceDate);
	}
}

public static NSDate DateTimeToNSDate(this Nullable<DateTime> date)
{
	if (date == null) {
		return null;
	} else {
		DateTime reference = TimeZone.CurrentTimeZone.ToLocalTime (new DateTime (2001, 1, 1, 0, 0, 0));
		return NSDate.FromTimeIntervalSinceReferenceDate ((date.GetValueOrDefault() - reference).TotalSeconds);
	}
}

public static NSDate DateTimeToNSDate(this DateTime date)
{
	DateTime reference = TimeZone.CurrentTimeZone.ToLocalTime(
	new DateTime(2001, 1, 1, 0, 0, 0) );
	return NSDate.FromTimeIntervalSinceReferenceDate((date - reference).TotalSeconds);
}


Here are my test cases:

C#
			// Testcase 1: Converting from NSDate to DateTime?
//			NSDate testDate = NSDate.Now;
//			DateTime? newDate = testDate.NSDateToDateTime();
//			Console.WriteLine (newDate.ToString ());
			// result: works

			// Testcase 2: Converting from null NSDate to DateTime?
//			NSDate testDate = null;
//			DateTime? newDate = testDate.NSDateToDateTime();
//			Console.WriteLine (newDate.ToString ());
			// result: nothing

			// Testcase 3: Converting from DateTime? to NSDate
//			DateTime? testDate = new DateTime? (DateTime.Now);
//			NSDate newDate = testDate.DateTimeToNSDate ();
//			Console.WriteLine (newDate.ToString ());
			// result: works

			// Testcase 4: Converting from null DateTime? to NSDate
//			DateTime? testDate = null;
//			NSDate newDate = testDate.DateTimeToNSDate ();
//			Console.WriteLine (newDate.ToString ());
			// result: crash because ToString() can't be called on null

			// Testcase 5: Converting from DateTime to NSDate
//			DateTime testDate = DateTime.Now;
//			NSDate newDate = testDate.DateTimeToNSDate ();
//			Console.WriteLine (newDate.ToString ());
			// result: works

			// Testcase 6: Converting from NSDate to DateTime
//			NSDate testDate = NSDate.Now;
//			DateTime newDate = testDate.NSDateToDateTime();
//			Console.WriteLine (newDate.ToString ());
			// result: doesn't work because explicit conversion is needed (DateTime newDate = testDate.NSDateToDateTime().GetValueOrDefault();)
Posted
Updated 14-Jan-15 0:50am
v4
Comments
Philippe Mori 14-Jan-15 8:12am    
Your conversion won't properly handle local time if your program is used while daylight saving time is in effect for standard time and vive versa. The conversion to local time must be done last when converting from NSDate to DateTime.
You should have test cases for DST and STD time.
bfb 14-Jan-15 9:25am    
Thank you for your comment. How can I improve my code?
Philippe Mori 14-Jan-15 10:01am    
I don't know much how NSDate works with local time. It also depends if you want DateTime object in UTC or local time.
Typically, I think that you should have UTC time except for display purpose. In that case, you would want to be in UTC time when adding or substracting from the reference and do Local to UTC conversion before and UTC to local conversion after the conversion between time format.
As far as I understand, the above code would use DST or STD time according to the current time instead of the actual object time (as it would be the case in .NET DateTime) so if you convert a DST time while STD time is in effect, you won't display the expected time to the user.
bfb 15-Jan-15 10:20am    
Surely something to think about. Currently the user are all in the same timezone and I only need the day, month and year. The dates are converted when the app is running (when the class is loaded, the values are stored in NSUserDefaults, or right before displaying the date in the datepicker). At the moment only the current time is used as you said. To make the app better I'd have to add the UTC conversion before and after adding the seconds. Do you have an idea how to make the test cases for this?
Philippe Mori 15-Jan-15 10:46am    
Does NSDate is always in UTC?

It seems you need to write total 4 methods - 2 for each direction of conversion - we need to write an extension method for each type (nullable and not nullable) and you cannot club them in one method as given here:
http://stackoverflow.com/questions/742336/extension-method-on-type-and-nullabletype[^]
 
Share this answer
 
Comments
bfb 14-Jan-15 5:28am    
Thanks for your response. I have edited my question according to this. But I get an error on compiling of NSDateToDateTime. The names are duplicated. How can I solve this?
Snesh Prajapati 14-Jan-15 6:07am    
Welcome....Method "NSDateToDateTime" is written twice as you can not do overloading just by giving different return type (As you are doing in this case)(http://stackoverflow.com/questions/20705643/method-overloading-with-different-return-type-in-c-sharp) and if NSDate is a reference type, there is no need to create different extension method.
bfb 14-Jan-15 6:20am    
What if `NSDate` is `null`? Then the app crashes with `System.NullReferenceException`. Therefore I thought I will return `DateTime?` but you can only return one type.
Snesh Prajapati 14-Jan-15 6:33am    
For Method "NSDateToDateTime" use return type as`DateTime?` only...it can return null value. As you are doing for nullable version of DateTimeToNSDate, handle issue of exceptions in same way.
bfb 14-Jan-15 6:54am    
Now I removed the duplicated method and only use the nullable method. I also included some test cases in my question. Perhaps you have a look on it and tell me if you have some additions or remarks (e.g. improvement proposal). Thanks!
C#
public static DateTime? NSDateToDateTime(this NSDate date)
{
	if (date == null) {
		return null;
	} else {
		DateTime reference = TimeZone.CurrentTimeZone.ToLocalTime (new DateTime (2001, 1, 1, 0, 0, 0));
		return (DateTime?)reference.AddSeconds (date.SecondsSinceReferenceDate);
	}
}

public static NSDate DateTimeToNSDate(this Nullable<datetime> date)
{
	if (date == null) {
		return null;
	} else {
		return (date.GetValueOrDefault ()).DateTimeToNSDate ();
	}
}

public static NSDate DateTimeToNSDate(this DateTime date)
{
	DateTime reference = TimeZone.CurrentTimeZone.ToLocalTime(
			new DateTime(2001, 1, 1, 0, 0, 0) );
	return NSDate.FromTimeIntervalSinceReferenceDate(
			(date - reference).TotalSeconds);
}</datetime>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900