Jump to content
Xtreme .Net Talk

Determining Elapsed time taking into account Daylight Savings Time [C#]


Recommended Posts

Posted

Oddly enough I was sitting at home writting this application to calculate the Elapsed time worked on a project when all of a sudden the clock went back an hour (daylight savings time), and that got me to thinking....

So, the way it works, when a user presses start I write the timestamp (datetime), when they pause I calculate now-start and save that value as Elapsed, then they can restart/unpause, and finally End.

 

Now I realized something, if they have a project running when we go over a daylight saving time period they will loose/gain an hour, this is unacceptable because I use this process to calcuate the hours worked on a project for billing customers - therefore it must be accurate.

 

Problem is I can't seem to find a way to fix this, only idea I had was to somehow check every X minutes to see if we are before a daylight savings time, forcefully pause all jobs (thus saving the elapsed time) and then forcefully restarting them after the switch, however I find this greatly inefficient and I don't even know how to code it.

Also, there is the problem about determining the daylight savings time, hardcoding it is not really an option seeing as it can change (as it will for my end of the world in a few years), is there a way to get this information programatically? Can Windows supply it somehow?

 

Anyways, hopefully that explains my problem - and the more I think about it the bigger it sounds...

Any hints/help would be greatly appreciated. thanks

  • Administrators
Posted

If you want absolute time differences then use the UTC time property of the DateTime object rather than the local time which may be influenced by DST.

 

If you need to programatically access this look under System.Globalisation, you should find classes like System.Globalization.DaylightTime and System.Globalization. which will be of use.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Amazing idea - one question, how do I convert the UTC DateTime to the correct local time on the users machine?

 

I mean, I was thinking of just storing the START and END time as UTC (in the DB) and when the user loads the program have it modify the UTC -> Local Time (somehow???) to display it in the correct TimeZone.

 

Using UTC removes the need to handle Daylight Savings Time (right?).

This would be a better method then using the System.Globalization.DayLightSavingTime right?

 

Unless you can recommend a more efficient method.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...