Reputation: 1112
I've got a timestamp in microseconds since 1.1.1970. I've tried to convert it into
yyyy.MM.dd HH.mm.ss.ffffff
using DateTime. An example is: 1337060932000000 microseconds the result should be May 15 2012, 7.48
But the result I get is 2 hours off. What could be the reason?
Upvotes: 1
Views: 150
Reputation: 1112
Thanks for the hints. I solved the problem this way:
int offset = 2;
DateTime d = new DateTime(1979,1,1, offset,0,0);
This example is simplified. The offset is specified in an external file so you can modify it easily for different time zones.
Upvotes: 1
Reputation: 9323
You are most likely getting a UTC date, since the Unix epoch is this time zone. Make sure you create the base date as so:
var epoch = new DateTime(1970,1,1, 0,0,0, DateTimeKind.Utc);
Once you have that, you can do something like:
var localTime = epoch.AddMilliseconds(microseconds / 1000).ToLocalTime();
If microseconds
is the value you provided above, the value you get is 15/05/2012 07:48:52
which is what you expected I think.
Be careful when using ToLocalTime
though, since you can only be sure that this will be the local time zone of computer your software is running and, from experience, I can tell you it's not always the time zone you think.
Upvotes: 2