tags:

views:

43

answers:

2

I have a few datapoints that include the offset from gmt (in seconds). I would like to send a message via a socket at midnight. Sending the message is no problem, im just having trouble determining the time based on the offset.

Anyone have any suggestions for this?

+1  A: 

If you are trying to get a current time given an offset in seconds, you can do:

TimeSpan offset = TimeSpan.FromSeconds(offsetInSecondsFromMidnight);

DateTime initial = DateTime.Now.Date; // Midnight, today - for time, the date doesn't really matter, but we want midnight

DateTime timeWithOffset = initial + offset;  // This will have the correct time of day now
Reed Copsey
+1  A: 

Since UTC and GMT are the same you can use this code.

int secondsOffset = 100;
    DateTime utcMidnight = DateTime.SpecifyKind(DateTime.Today, DateTimeKind.Utc);

DateTime utcWithOffset = utcMidnight.AddSeconds(secondsOffset);

Console.WriteLine("Offset on UTC: " + utcWithOffset);
Console.WriteLine("Offset on local time: " + utcWithOffset.ToLocalTime());

The first WriteLine shows the time at UTC timezone. The second Writeline shows the time at the local timezone. The tricky part may be finding out what is the base time, was it today at midnight or yesterday at midnight?

Paulo Manuel Santos