3

To keep it short and sweet I have 2 PC's:

PC 1 has DST switched off

PC 2 has DST switched on.

PC 1 sends a DateTime to PC 2 which it takes and uses to set its time.

All the above I have in place but my questions is that when PC 2 receives the DateTime how can I check to see if it needs DST adjustments?

PC's are UK based.

EDIT:- Bit more detail incase there is confusion. When PC 2 retrieves the time from PC 1 it will change the system's time to this value but I need to ensure that if a +/- 1 hour is required (DTS) then it is applied before setting the system date and time.

Gaz83
  • 2,293
  • 4
  • 32
  • 57
  • 3
    Use UTC for persistance and transport – bryanmac Mar 06 '13 at 16:07
  • Re EDIT: you can still send UTC right? When UTC time is sent to the other PC, it knows it's timezone so it can translate from the well known transport of UTC, apply it's timezone offset and update the time. – bryanmac Mar 07 '13 at 02:03
  • @bryanmac Ok so lets say I receive the UTC as a DateTime object, do I just use this to set the system time or do I convert it back to, I'm guessing, GMT time? – Gaz83 Mar 07 '13 at 13:30

2 Answers2

2

I would recommend using UTC for transport and persistance.

Also take care to not use date time as a critical part of your algorithm - it's just data. Dates and time shift and correct themselves. I've seen apps that crash when the PC corrects it's time :)

Here's another post with more information on the topic: Daylight saving time and time zone best practices

Community
  • 1
  • 1
bryanmac
  • 38,941
  • 11
  • 91
  • 99
1

You didn't show any code, but you tagged your question as , so I will answer from that perspective.

If you just need to make sure you're talking about the same moment in time, then use a DateTime with .Kind = DateTimeKind.Utc, such as is obtained from DateTime.UtcNow.

If you actually need to know that PC1 thought it was one time while PC2 thought it was another, and you still want to know that those two things represent the same moment in time, then use a DateTimeOffset. This will include the local date and time of the computer, as well as the offset from UTC that the date and time represent.

With either approach, you should use the ISO8601 format for transport and persistence, so that your meaning is clear regardless of culture or time zone of the observer. In .Net, this is obtained with .ToString("o") from either a DateTime or DateTimeOffset.

A UTC DateTime in ISO8601 format would look like this:

2013-03-06T09:00:00Z

A DateTimeOffset in ISO8601 format would look like this:

2013-03-06T10:00:00+01:00

.Net calls this the Round Trip pattern - because it is designed for the exact purpose you described.

For further clarification on DateTimeOffset - see this post.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575