1

i am working with windows phone 8 and need to convert a DateTime Object into a unix timestamp.

I know there are lots of posts on how to do that, but I have an odd mistake i just cant figure out.

I am doing this:

double val = (value.Ticks - New DateTime(1970,1,1).Ticks) / TimeSpan.TicksPerSecond;

where value is the DateTime Object i want to convert to a unix timestamp. I know that value contains the right Time, (during debugging i can see it has the right date+time)

but the converted unix value is always +2 hours off. There has to be an issue with time zones.

Can someone help me? I find dealing with timezones very very annoying under WP, because there are no methods to convert to a specific time zone other than UTC or Local.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
user2078645
  • 189
  • 1
  • 1
  • 9
  • I've added the c# tag, since I presume that's the language you're using. If not, please replace the tag by one for the language you're actually using. – Keith Thompson Aug 14 '13 at 20:35
  • possible duplicate of [Unix time conversions in C#](http://stackoverflow.com/questions/7983441/unix-time-conversions-in-c-sharp) – Matt Johnson-Pint Aug 14 '13 at 22:41
  • 1
    Don't do it like that. [Jon Skeet's answer](http://stackoverflow.com/a/7983514/634824) to this question has all you need. But you need to understand that not all `DateTime`s are created equal. The `.Kind` matters - which probably explains the discrepancy you mentioned. – Matt Johnson-Pint Aug 14 '13 at 22:43
  • Yes i found Jon Skeet's answer, and i tried his solution. Let's say its 12:00 in my country, then UTC time is 10:00 and the result of (DateTime.UtcNow - UnixEpoch).TotalSeconds (where UnixEpoch is utc) would be 12:00, which is the right time for me, but i cant see why, because DateTime.UtcNow is off 2 hours. The problem here is that i need it to work in different timezones as well. – user2078645 Aug 15 '13 at 10:54

1 Answers1

2

According to this documentation, the 3-argument constructor for DateTime:

DateTime(Int32, Int32, Int32)

"Initializes a new instance of the DateTime structure to the specified year, month, and day." It doesn't have a way to specify the time zone, and the documentation doesn't specify the time zone of the result. I'd guess that it's local time.

There's another constructor:

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind)

that "Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, and Coordinated Universal Time (UTC) or local time".

Try replacing

New DateTime(1970,1,1).Ticks

by

New DateTime(1970,1,1,0,0,0,DateTimeKind.Utc).Ticks

to get a DateTime representing 1970-01-01 00:00:00 UTC (if I'm remembering the syntax correctly).

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • thanks, but I already tried that. No matter what i do, the time is always wrong. Now i installed the Noda-Time library, there i can at least get the right time zone, but i haven't figured out how to get the current time in this time zone. – user2078645 Aug 15 '13 at 10:02
  • If you tried it and it didn't work, why did you accept this answer? – Keith Thompson Aug 18 '13 at 19:59
  • I learned that it did indeed work. I thought the unix timestamp depends on the local time, but i learned that the unix timestamp should be the same at every location and that it is not an issue that it does not represent my local time zone – user2078645 Aug 24 '13 at 15:10