Convert CST to EST with daylight savings time - c#

I'm trying to convert a DateTime that is in CST to EST while adjusting for daylight savings. My local machine is on EST.
Found this on msdn but user fokasu
recommended using DateTimeOffset to adjust for DST, but i dont know how to incorporate that into my code. thanks for any help.
this is what i have so far which works, but i dont think it's adjusting for daylight savings.
DateTime cstTime, utcTime;
DateTime estTime;
string cstZoneId, estZoneId;
TimeZoneInfo cstZone, estZone;
estZoneId = "Eastern Standard Time";
cstZoneId = "Central Standard Time";
estZone = TimeZoneInfo.FindSystemTimeZoneById(estZoneId);
cstZone = TimeZoneInfo.FindSystemTimeZoneById(cstZoneId);
//define d as CST
cstTime = d;
try
{
//convert CST to UTC
utcTime = TimeZoneInfo.ConvertTimeToUtc(cstTime, cstZone);
//convert UTC to EST
estTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, estZone);
return estTime;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

It is not clear from your question what the problem is; you have not described the results your code produces nor your expected result.
Except for two periods each year, of an hour's duration each, Central and Eastern time are always one hour apart. The exceptions are at the transitions into and out of daylight saving time. At 2 AM EST, on the second Sunday of March, the Eastern time zone's clocks move to 3 AM EDT. For the next 60 minutes, Eastern Time is two hours ahead of Central Time. Then, at 2 AM CST, the Central time zone's clocks move to 3 AM CDT, and the difference returns to one hour.
Similarly, on the first Sunday in November, at 2 AM EDT, clocks are set to 1 AM EST. For the next 60 minutes, the Central zone remains on daylight saving time, so the time in both places is the same. An hour later, the Central zone's clocks move back one hour, and the normal time difference is restored.
Since your times will never fall in either of these periods, you can safely assume that Central times are always one hour before Eastern times.

Related

C# Handling Daylight Saving in Time Conversion

I have a date string 2020-11-26T14:24:29-08:00 which is passed to the backend ASP NET MVC application.
I am parsing the date/time string using the below code:
DateTime dateTime = DateTimeOffset.ParseExact("2020-11-26T14:24:29-08:00",
"yyyy'-'MM'-'dd'T'HH':'mm':'sszzz",
CultureInfo.InvariantCulture).DateTime;
This code is giving me time which is 1 hour less than the actual time. This is because of Daylight Savings.
Now, I want to know if daylight saving is active in the timezone or not. So I wrote the below code:
var offset = DateTimeOffset.ParseExact("2020-11-26T14:24:29-08:00",
"yyyy'-'MM'-'dd'T'HH':'mm':'sszzz",
CultureInfo.InvariantCulture);
TimeZoneInfo zone = null;
foreach (TimeZoneInfo z in TimeZoneInfo.GetSystemTimeZones())
{
if(zone.BaseUtcOffset == offset.Offset)
zone = z; // this runs multiple times
}
The above code if condition is hitting multiple times with the following zones satisfying condition.
Yukon Standard Time
Pacific Standard Time (Mexico)
UTC-08
Pacific Standard Time
So, I further narrowed down the code
var offset = DateTimeOffset.ParseExact("2020-11-26T14:24:29-08:00",
"yyyy'-'MM'-'dd'T'HH':'mm':'sszzz",
CultureInfo.InvariantCulture);
TimeZoneInfo zone = null;
foreach (TimeZoneInfo z in TimeZoneInfo.GetSystemTimeZones())
{
if(zone.BaseUtcOffset == offset.Offset && zone.SupportsDaylightSavingTime) // added second condition
zone = z; // this runs multiple times
}
This time I got three:
Yukon Standard Time
Pacific Standard Time (Mexico)
Pacific Standard Time
Is it possible to get the exact timezone? (I know this user is in Pacific Standard Time)
Or is it okay to use any (first one) and add one hour to time to get the correct time?
If that datetime string is all you're able to use, I suspect you have to hope the offset they've given you is in line with their time zones daylight savings time (and so could be different for the same location).
I think more information might be needed before a useful answer can be given. Where is this value coming from, what use is the daylight savings time?

Get time until local time is N hours from now, taking summer-time changes into account

Timezones and summer-time changes in particular confuse me. In the UK we have GMT/BST:
In the UK the clocks go forward 1 hour at 1am on the last Sunday in
March, and back 1 hour at 2am on the last Sunday in October. The
period when the clocks are 1 hour ahead is called British Summer Time
(BST).
Given a local time say 00:00 I want to be able to calculate how long until it is 03:00 in local time. Normally this is trivially 3 hours but on March 26th (last Sunday in March) from 00:00 - 03:00 is actually two hours. And similarly when the clocks go back in October from 00:00 - 03:00 is four hours.
Do the .Net DateTime class and its methods do this trivially for me or do I need to be careful?
In my case specifically I'm working from strings so I'm after a method doing:
TimeSpan DifferenceBetweenLocalTimes(string startDateTime,string endDateTime)
I can see things like TimeZoneInfo.IsDaylightSavingTime but how to use this to do as I wish is not obvious. My application works treating each calendar day's local midnight as a strict boundary i.e. not every day is 24 hours long, once a year I get a 23 hour day and a 25 hour day.
You can use TimeZoneInfo class to get the offset from your local date time to UTC (including daylight tricks). For example
var timeZone =TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
var date1 = DateTime.Parse("2017-03-26 00:00:00");
var date2 = DateTime.Parse("2017-03-26 03:00:00");
var dto1 = new DateTimeOffset(date1, timeZone.GetUtcOffset(date1));
var dto2 = new DateTimeOffset(date2, timeZone.GetUtcOffset(date2));
var diff1 = (dto2 - dto1).TotalHours;
Console.WriteLine(diff1); // this is 2 hours
The GetUtcOffset method returns difference between time in that time zone and UTC
While tchrikch's answer is perfectly reasonable (and should be accepted, IMHO), it's worth adding a Noda Time based solution.
// Parse input as LocalDateTime values (since they represent a local date and time)
var pattern = LocalDateTimePattern.CreateWithInvariantCulture("yyyy-MM-dd HH:mm:ss");
LocalDateTime ldt1 = pattern.Parse("2017-03-26 00:00:00").Value;
LocalDateTime ldt2 = pattern.Parse("2017-03-26 03:00:00").Value;
// Apply a specific time zone, now making them ZonedDateTime values
// Using "lenient" conversions allows for default handling of ambiguous/invalid values
DateTimeZone tz = DateTimeZoneProviders.Tzdb["Europe/London"];
ZonedDateTime zdt1 = ldt1.InZoneLeniently(tz);
ZonedDateTime zdt2 = ldt2.InZoneLeniently(tz);
// Now simply determine the elapsed duration between these
Duration result = zdt2 - zdt1;
Note that the subtraction between ZonedDateTime values was added in NodaTime 2.0. If you're on an older version, you'll need to do this instead:
Duration result = zdt2.ToInstant() - zdt1.ToInstant();

Getting the Arizona Standard Time in .net

I have an application in which time zones are treated as string, by using the system name so we can make an actual System.TimeZoneInfo object by doing:
var tz = TimeZoneInfo.FindSystemTimeZoneById(timeZone);
Such values are persisted to DB, now we are facing an issue where one such object is requested to be on Arizona Time which is not a standard timezone. From what I have investigated the Arizona Time changes Time Zones due to the fact that it doesn't observes "Day Light Savings".
I am looking for a way to set one value in DB so that there is no need to change it according to day light savings changes.
Is there a way to do this?
Even if I have to change a bit the code to get the TimeZoneInfo object. What really matters to me is a way to determine the actual timezone corresponding to Arizona Time
About Arizona time zones
From timeanddate.com:
There is a common misconception that Arizona is on Pacific Daylight
Time (PDT) during the summer and on Mountain Standard Time (MST)
during the winter. Because MST and PDT have the same UTC offset of
minus 7 hours (UTC-7), Arizona has the same local time as neighboring
states California and Nevada during the summer season. Although the
time is the same, Arizona uses standard time (MST) all year.
“Daylight” time zones, such as MDT, are mostly used for areas that
switch to DST every year
IANA (tz database) time zone database contains two time zones for Arizona:
America/Phoenix (Mountain Standard Time - Arizona, except Navajo), which does not observe daylight saving changes (DST), and
America/Shiprock, which observes DST.
Arizona time zones in .NET
Depending on your users' exact location in Arizona, you should use either America/Phoenix or America/Shiprock time zone, so you will need two values in the database. However, if you try to get time zones with TimeZoneInfo.FindSystemTimeZoneById using tz database names, you will get System.TimeZoneNotFoundException.
In order to get Arizona time zone that does not observe DST (America/Phoenix), you can use:
TimeZoneInfo.FindSystemTimeZoneById("US Mountain Standard Time")
In order to get Arizona time zone that does observe DST (America/Shiprock), you can use:
TimeZoneInfo.FindSystemTimeZoneById("Mountain Standard Time")
So, you would have both ids in your database, US Mountain Standard Time and Mountain Standard Time, or alternatively some other strings that you would later map to these .NET time zone ids.
Check out NodaTime, it can help you a lot when it comes to dealing with date, time and time zones.
And finally, here is a sample program (with NodaTime) that demonstrates the difference between .NET US Mountain Standard Time (America/Phoenix, Arizona without DST) and Mountain Standard Time (America/Shiprock, Arizona with DST).
using System;
using NodaTime;
using NodaTime.TimeZones;
namespace TimeZoneExample
{
class Program
{
static void Main(string[] args)
{
// Arizona without daylight saving time (TZ: America/Phoenix)
var mstWithoutDstTz = TimeZoneInfo.FindSystemTimeZoneById("US Mountain Standard Time");
// Arizona with daylight saving time (TZ: America/Shiprock)
var mstWithDstTz = TimeZoneInfo.FindSystemTimeZoneById("Mountain Standard Time");
// NodaTime BclDateTimeZone for Arizona without daylight saving time
var mstWithoutDstNodaTz = BclDateTimeZone.FromTimeZoneInfo(mstWithoutDstTz);
// NodaTime BclDateTimeZone for Arizona with daylight saving time
var mstWithDstNodaTz = BclDateTimeZone.FromTimeZoneInfo(mstWithDstTz);
// January 1, 2017, 15:00, local winter date
var localWinterDate = new LocalDateTime(2017, 01, 01, 15, 00);
// NodaTime ZonedDateTime for Arizona without daylight saving time: January 1, 2017, 15:00
var winterTimeWithoutDst = mstWithoutDstNodaTz.AtStrictly(localWinterDate);
// NodaTime ZonedDateTime for Arizona with daylight saving time: January 1, 2017, 15:00
var winterTimeWithDst = mstWithDstNodaTz.AtStrictly(localWinterDate);
// Both time zones have the same time during winter
Console.WriteLine($"Winter w/o DST: {winterTimeWithoutDst}"); // 2017-01-01T15:00:00 US Mountain Standard Time (-07)
Console.WriteLine($"Winter w/ DST: {winterTimeWithDst}"); // 2017-01-01T15:00:00 Mountain Standard Time (-07)
// add 180 days to get June 30, 2017
var sixMonthsToSummer = Duration.FromTimeSpan(new TimeSpan(180, 0, 0, 0));
// During summer, e.g. on June 30, Arizona without daylight saving time is 1 hour behind.
Console.WriteLine($"Summer w/o DST: {winterTimeWithoutDst + sixMonthsToSummer}"); // 2017-06-30T15:00:00 US Mountain Standard Time (-07)
Console.WriteLine($"Summer w/ DST: {winterTimeWithDst + sixMonthsToSummer}"); // 2017-06-30T16:00:00 Mountain Standard Time (-06)
}
}
}
If I understand your problem correctly, you want to create a custom time zone representing "Arizona Time" which has a constant offset from UTC regardless the date of the year.
If so, you should be able to use the static method
TimeZoneInfo.CreateCustomTimeZone
Just set the TimeSpan to the number of hours from UTC that you need it to be (-7 hours from what I can tell).
https://msdn.microsoft.com/en-us/library/bb309898(v=vs.110).aspx
EDIT: You might also have some success by simply using the named timezone
"US Mountain Standard Time"
which should represent the same.

DateTime parsing error: The supplied DateTime represents an invalid time

I have one situation where date is "3/13/2016 2:41:00 AM". When I convert date by time-zone, I get an error.
DateTime dt = DateTime.Parse("3/13/2016 2:41:00 AM");
DateTime Date_Time = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dt, "Eastern Standard Time",
"GMT Standard Time");
Response.Write(dt);
after execution, I get this error:
The supplied DateTime represents an invalid time. For example, when
the clock is adjusted forward, any time in the period that is skipped
is invalid. Parameter name: dateTime
Try to check if the time is ambiguous or a valid time. Due to the daylight change the time you mentioned i.e, 2:41:00 AM doesn not exist since the clock was moved 1 hour ahead and hence the date is invalid or ambiguous.
2016 Sun, 13 Mar, 02:00 CST → CDT +1 hour (DST start) UTC-5h
Sun, 6 Nov, 02:00 CDT → CST -1 hour (DST end) UTC-6h
You can also refer to this blog: System.TimeZoneInfo: Working with Ambiguous and Invalid Points in Time
System.TimeZoneInfo (currently available as part of .NET Framework 3.5
Beta 1) contains methods for checking if a DateTime instance
represents an ambiguous or invalid time in a specific time zone. These
methods are particularly useful for validating user-supplied points in
time.
Background Information
Time zones that adjust their time for Daylight Saving Time (in most
cases by moving the clock time back or forward by 1 hour) have gaps
and repeats in the timeline — wherever the clock time was moved
forward or back by the adjustment. Let’s use Pacific Standard Time as
an example. In 2007 Pacific Standard Time (PST) changes to Pacific
Daylight Time (PDT) at 02:00AM (“spring forward”) on the second Sunday
in March and then returns at 02:00AM (“fall back”) on the first Sunday
in November
To check if the time is valid you can use:
TimeZoneInfo.IsInvalidTime
In my case, I was trying to convert a UTC date (thus, it was valid, as UTC dates don't skip any periods of time with DST).
The problem was that I was loading the date from Entity Framework and the DateKind was set to Unspecified. In that case, ConvertTimeBySystemTimeZoneId assumes it is a local time and may find it invalid.
The solution is to properly set the DateKind to UTC before converting:
var date = DateTime.ParseExact("2019-03-31T03:06:55.7856471", "O", CultureInfo.InvariantCulture);
// Here date.Kind == DateTimeKind.Unspecified
date = DateTime.SpecifyKind(date, DateTimeKind.Utc);
// Now date.Kind == DateTimeKind.Utc
// Now the conversion should work
TimeZoneInfo.ConvertTimeBySystemTimeZoneId(date, "Central Standard Time");

Converting EST to IST gives error in C#

I am trying to convert EST( Eastern Standard Time) to IST (Indian Standard Time) but the conversion is showing incorrect results.
Can anyone help me on that??
I searched on net and found that using Noda time we can solve that.
But I want to solve it using conventional DateTime class.
Here is my code and its output:
DateTime time= new DateTime(1899,12,30, 23, 30 ,0); //some random date and 11:30 PM in EST
TimeZoneInfo estZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"); // Eastern Time Zone
TimeZoneInfo istZone = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time"); // Indian Time Zone
DateTime localTime = TimeZoneInfo.ConvertTime(time, estZone, istZone); // result is 10:00 am while it should be 09:00 am.
A few things:
The TimeZoneInfo identifier "Eastern Standard Time" refers to the North American Eastern Time zone, covering both Eastern Standard Time and Eastern Daylight Time. EST is UTC-5, while EDT is UTC-4. In general, you should not infer too much from the names of these identifiers. See more examples in the timezone tag wiki.
The TimeZoneInfo.ConvertTime method will use whichever offset is appropriate for the supplied date and time, correctly taking the daylight saving time rules into account.
The underlying time zone data from Windows does not go back to 1899. There are actually no sources of data that guarantee historical dates from that time period. Even the IANA time zone database used with Noda Time makes an educated guess. See History of DST in the United States.
Windows will just use the earliest data it has, which for this zone uses the daylight saving time rules that were in effect from 1986 to 2007. These are not the current rules, so it would make better sense to use a modern year, such as DateTime.Today.Year.
Even if you supplied a modern year, the correct converted time would indeed be 10:00 for a date in December. If you want 9:00, try a date in the summer.

Categories

Resources