Reputation: 21
I am currently making a small timer application for Windows Phone 7 with data logging capabilities so people can see how long they spent performing a task in a given day.
They need be able to close the application, answer phone calls, etc. and the application will appear to continue counting (since there's no multitasking in WP7 for 3rd party). I am currently doing this by storing the time the timer was last updated and calculating the difference between the time now.
Right now I have the following code which is executed when the timer updates (every second / on Application Activation):
TimeSpan tempNowLastDiff = DateTime.Now - LastUpdateTime;
CumulativeTime += tempNowStartDiff;
LastUpdateTime = DateTime.Now;
The problem is if the person exits the application and changes the clock, when the application starts again the the CumulativeTime is no longer accurate. If I change the clock back, it can result in a negative TimeSpan, if I change the clock forward it can result in an overly positive TimeSpan.
The only solution I can think of is to 0 the difference if tempNowStartDiff is negative and to just assume the task was for 24 hours every day if positive (which is possible if, say, they go on a road trip).
Is there any way to get an absolute time which will not be affected by phone time changes? If not, is there a way to check if the time was changed and by how much?
Edit: I've uploaded a copy of the Timer code here with instructions on how to replicate issue.
Upvotes: 2
Views: 672
Reputation: 4592
I would use DateTime.UtcNow to have a absolute value because DateTime.Now is relative to the current timezone and so the phone can change the time automatically if they change of timezome (from the GSM network).
Upvotes: 1
Reputation: 18430
In that case the only solution i can think of is to get the time from somewhere outside like from timeanddate.com
Upvotes: 0