views:

145

answers:

3

I must be doing an obvious mistake but I can't figure it out.

I am importing a date stored in a mysql database (it is stored by the ExpressionEngine CMS). It is a unix timestamp, i.e. seconds since 1/1/1970 00:00.

So I'm doing something like this:

DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
dateTime = dateTime.AddSeconds(orderdate /* int read from the database */);

Unfortunately I don't get the right result. Here is an example:

Value read from the DB: 1258598728 (this is an order date)

Paypal sent an email establishing the order at Nov 18, 2009 12:45:20 PST

The php web site that reads this value in the DB and knows how to display this date correctly displays it as 2009-11-18 03:45 PM (which seems correct since I’m hosted at a server on the east coast)

My code above gives 11/19/2009 2:45:28 AM !! (UTC which gives 11/18/2009 9:45 PM east time, i.e. 6 hours difference with what is expected)

I get the same result if using DateTimeOffset taking care of putting the right timezone.

Any idea what I'm doing wrong?

+4  A: 

Try this:

DateTime epoch = new DateTime(1970,1,1,0,0,0,0, DateTimeKind.Utc);
DateTime myDate = epoch.AddSeconds(1258598728).toLocalTime();
scottm
It prints: 11/18/2009 9:45:28 PM here (GMT-4)
Gonzalo
yes i'ts still the same value I get (6 hours difference with the expected value)
Nicolas Cadilhac
well, then it has to be a problem with the data.
scottm
A: 

Your conversion is correct, the data is wrong. That's the simplest and most likely explanation.

Chris Patterson
+1  A: 

http://www.onlineconversion.com/unix_time.htm confirms your calculations are right. Unix time 1258598728 = "Thu, 19 Nov 2009 02:45:28 GMT"

spoulson