-3

The thing I am trying to achieve is, by using int values, change UTC datetime and receive time from different timezones.

Int values supposed to be:

0 = UTC+00:00
1 = UTC+01:00
...

By logic, it supposed to be something like:

int timezoneInt = 1;
var newDate = DateTime.UtcNow.AddMinutes(timezoneInt*60);

But the problem is that this not include summer/winter time.

For example:

My location is in UTC+02:00 and time is 09:20 AM. I need to get UTC+00:00 (which is equal to DateTime.UtcNow and supposed to be(?) 07:20 AM). Because of summer time, right now .UtcNow is 06:20 AM, so I can't just multiply 60 minutes by int value, I also need to include summer time factor somehow.

How I suposed to accomplish that, or what I am missing or understanded whong?

EDIT: Was marked as dublicate. Well, here I don't see anything which will help change time by using int value as timezone.

Community
  • 1
  • 1
Olegs Jasjko
  • 2,128
  • 6
  • 27
  • 47
  • Possible duplicate of [How to convert DateTime in Specific timezone?](http://stackoverflow.com/questions/9869051/how-to-convert-datetime-in-specific-timezone) – kevintjuh93 Oct 14 '15 at 06:34
  • Use [`TimeZoneInfo`](https://msdn.microsoft.com/en-us/library/system.timezoneinfo%28v=vs.110%29.aspx). Adding hour can be done by `DateTime.UtcNow.AddHours(1)`. It makes no sense to add 60m, like you do – Matyas Oct 14 '15 at 06:34
  • Mark, You didn't read first question part, didn't you? – Olegs Jasjko Oct 14 '15 at 06:35
  • @OlegsJasjko my bad misunderstood the question - but the question i.e. answer from kevinintjuh93 link seems to be your answer – Mark Oct 14 '15 at 06:38
  • No, it isn't. I added comment why. – Olegs Jasjko Oct 14 '15 at 06:43

2 Answers2

1

Adjustments for daylight savings require an extensive timezone database which also records the dates and times when that zone changes to daylight savings, and what the new UTC offset is.

DST databases are not easy to build (or even use, in some cases) and must be manually researched and maintained as they are political, not technical - countries can change their daylight savings dates as they wish and so the database also needs to record historical dates.

The .NET Framework has a built-in TimeZoneInfo class (which obsoletes the TimeZone class) which makes use of the timezone database built-in to Windows or whatever the host OS is. Windows' timezone database uses full names to identify zones, whereas Linux and tzdb use IDs like America/New_York.

Note that generally you should never perform these calculations yourself as there are always numerous edge-cases to be aware of. Just use DateTimeOffset.

Also, there is not a 1:1 mapping between UTC offsets and timezones: different timezones share the same UTC offset but have different daylight savings rules (e.g. the British Time timezone uses UTC+0 as their normal UTC offset but UTC+1 in summer, but if you see "UTC+1" that could be either the British zone in summer or a West-African timezone such as Algeria's which is UTC+1, but does not use daylight savings at all.

Dai
  • 141,631
  • 28
  • 261
  • 374
0

Copied verbatim from the timezone tag wiki:

Time Zone != Offset

A time zone can not be represented solely by an offset from UTC. Many time zones have more than one offset due to daylight saving time (aka "summer time") rules. The dates that offsets change are also part of the rules for the time zone, as are any historical offset changes. Many software programs, libraries, and web services disregard this important detail, and erroneously call the standard or current offset the "zone". This can lead to confusion, and misuse of the data. Please use the correct terminology whenever possible.

  • An offset is just a number that represents how far a particular date/time value is ahead or behind UTC.
    • Most offsets are expressed in whole hours.
    • But there are many that are 30-minute offset.
    • And there are a few that are 45-minute offset.
  • A time zone contains much more:
    • A name or ID that can be used to identify the zone.
    • One or more offsets from UTC
    • The specific dates and times that the zone transitions between offsets.
    • Sometimes, a separate language-specific display name that can be presented to a user.

One can determine the correct offset, given a time zone and a datetime. But one cannot determine the correct time zone given just an offset.

You are asking how to ignore this fact and make it work anyway - which is not possible. You cannot use an integer offset and expect it to understand daylight saving time. Any solution that tries to defeat this will be taking a bias towards one particular set of daylight saving time rules, which are quite different from country to country.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575