0

I'm developing a method that will receive UNIX timestamp and culture:

public DateTime unixEpochToDateTime(long unixTimeStamp, string culture) {
    ...
}

Call example:

DateTime t = unixEpochToDateTime(1478181983, "es-ES");

I need convert this UTC time between many cultures, without using TimeZone.CurrentTimeZone because this method will be run in server and the server culture is allways the same.

For example, if two user request my method in EE.UU and in Great Britain, I get the same UTC?

I'm very confused.

  • As am I by your question (at the moment). You have a unix timestamp, can I assume that the timestamp is also from UTC or is there an offset stored with this timestamp? – Igor Nov 03 '16 at 15:19
  • @Igor Timestamp is UTC. Thanks! –  Nov 03 '16 at 15:25
  • Then why not create the `DateTime` instance in utc? Also the datetime has a property for [Kind](https://msdn.microsoft.com/en-us/library/shx7s921(v=vs.110).aspx) that you can set which will allow you to specify UTC (there is also an overload in the constructor where you can set it as well). See static method `DateTime.SpecifyKind`. – Igor Nov 03 '16 at 15:28
  • I'm trying this: `DateTime datetime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); datetime = datetime.AddSeconds(unixTimeStamp)` But allways return time different of my culture (one hour less) –  Nov 03 '16 at 15:31
  • Not sure what your local offset is but to get your local time you can use https://msdn.microsoft.com/en-us/library/system.datetime.tolocaltime(v=vs.110).aspx. `var mylocal = datetime.ToLocalTime();` – Igor Nov 03 '16 at 15:35
  • @Igor But.... this ToLocalTime() depends on current culture... user that invoke this method will be passing unix epoch and her culture –  Nov 03 '16 at 15:41

2 Answers2

0

I think that you can try nodatime. It has many usefull methods.

IgorM
  • 31
  • 7
0

Problem solved...

If .NET does not provide a mechanism is because it is impossible to determine the time difference UTC based on culture.

Basically because a culture can have different time zones.

Culture "es-ES" for example, has 2 zones: GTM + 0 for Canary Islands and UTC + 1 for Iberian Peninsula.