Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Settings plugin not working properly with DateTime property

Tags:

I am using the settings plugin and I have it working to store some booleans. Now I wanted to add managing a DateTime object. I added the following to Settings.cs:

private const string TimeRemainingKey = "time_remaining"; private static readonly DateTime TimeRemainingDefault = DateTime.Now;  public static DateTime TimeRemaining {     get     {         return AppSettings.GetValueOrDefault(TimeRemainingKey, TimeRemainingDefault);     }     set     {         AppSettings.AddOrUpdateValue(TimeRemainingKey, value);     } } 

Originally I used the following in my code:

Settings.TimeRemaining = new DateTime().AddMinutes(30); 

When I added some logging I had this:

DateTime dt = new DateTime(); Debug.WriteLine(dt.ToString()); dt = dt.AddMinutes(30); Debug.WriteLine(dt.ToString()); Settings.TimeRemaining = dt; Debug.WriteLine(Settings.TimeRemaining.ToString()); 

It prints out:

1/1/0001 12:00:00 AM

1/1/0001 12:30:00 AM

1/1/0001 12:00:00 AM

Why does this behavior occur?

like image 489
stefana Avatar asked May 02 '16 10:05

stefana


1 Answers

The settings plugin converts the DateTime to UTC so it looks like in your timezone when it converts 1/1/0001 12:30:00 AM to UTC it gets 1/1/0001 12:00:00 AM. As a result when the value is read back from settings you get 1/1/0001 12:00:00 AM.

If you set the Kind for your date the plugin should work correctly:

Settings.TimeRemaining = DateTime.SpecifyKind(new DateTime().AddMinutes(30), DateTimeKind.Utc); 
like image 132
Giorgi Avatar answered Oct 14 '22 10:10

Giorgi