Build DateTimeOffset from unspecified DateTime and Timezone name - c#

I have a DateTime dt, which has a date and time of some local instant and a String tz specifying the timezone name for that datetime. How do I get a DateTimeOffset struct fully representing the DateTime?
I can get the timezone info with TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(csf.TimezoneName);
but I'm now unsure how to get the DateTimeOffset I want from these two elements

You can convert a local time in a particular zone to a DateTimeOffset like this:
DateTime dt = new DateTime(2013, 1, 1, 0, 0, 0);
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTimeOffset dto = new DateTimeOffset(dt, tzi.GetUtcOffset(dt));
Just be aware that if the input time is ambiguous or invalid due to daylight saving time, it will use the zone's standard offset.

Related

Create DateTime in specific time zone then convert to utc

I need to create a DateTime with a set date and time which will be in a specific time zone(West Asia Standard Time, W. Europe Standard Time etc).
DST must be preserved, so offset is out because for half of the year for a given time zone it will be for example +2h and for the other half +3h.
Then I want to convert the date to UTC.
I tried to do it in such a way that I could add this timezone offset later. However, firstly I do not like this solution and I am afraid that I will lose one hour when the time changes twice a year, and secondly I get an error:
"The UTC Offset for Utc DateTime instances must be 0."
var testTime = new DateTime(testDate.Year, testDate.Month, testDate.Day, 4, 0, 0, DateTimeKind.Utc);
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("West Asia Standard Time");
var timezoneTime = TimeZoneInfo.ConvertTimeFromUtc(runTime, timeZoneInfo);
var offset = timeZoneInfo.GetUtcOffset(timezoneTime);
I would like to get this kind of code
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("West Asia Standard Time");
var testTime = new DateTime(testDate.Year, testDate.Month, testDate.Day, 4, 0, 0, timeZoneInfo);
var utcTime = testTime.ToUniversalTime();
So, to sum up, I want to have method, where I pass year, month, day, hour, minute and timeZone and in return I will get DateTime that is in UTC
In javascript there are libraries, where the given time zone is given as a parameter but I have to do it on the server side.
You'd basically need TimeZoneInfo.ConvertTimeToUtc method.
Just make sure the Kind property of the passed DateTime is Unspecified, otherwise the method has special expectations for the sourceTimeZone argument and will throw exception.
e.g.
var testTime = new DateTime(testDate.Year, testDate.Month, testDate.Day, 4, 0, 0);
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("West Asia Standard Time");
var utcTime = TimeZoneInfo.ConvertTimeToUtc(testTime, timeZoneInfo);;

ERROR: Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks

I am using Noda Time (Date and Time Api) for my date conversions from UTC to a provided timezone.
Below is a method that returns a localized DateTime object based on the specified noda time zone id.
"utcDateTime" is the DateTime to convert and "timeZoneId" is the id of the time zone
public static DateTime ConvertDateTimeFromUtc(DateTime utcDateTime, string timeZoneId)
{
utcDateTime = DateTime.SpecifyKind(utcDateTime, DateTimeKind.Utc);
var timeZone = DateTimeZoneProviders.Tzdb[timeZoneId];
var convertedDateTime = Instant.FromDateTimeUtc(utcDateTime)
.InZone(timeZone)
.ToDateTimeUnspecified();
return convertedDateTime;
}
I get a date which is in utc from a collection and trying to convert it to the provided time zone but i get this error
Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks.
DateUtility.ConvertDateTimeFromUtc(collection.Select(c => c.date).DefaultIfEmpty(DateTime.MinValue).First(), time_zone)

Get DateTime object given local time as string, date as UTC DateTime and a timezone-locale string?

I'm working with data from several sources, and I need to put together an accurate DateTime.
I have:
A) a string representing a time of day, e.g.: "4:00 pm"
B) a DateTime object intended to represent a pure date, by having been created as midnight, zulu time (offset of 00:00) for a particular date.
C) a string representing a timezone locale, e.g.: "America/Los_Angeles"
How do I get a precise DateTime object, with correct number of ticks, representing the time (A) experienced in that locale (C), on that date (B)??
Here you have an example using NodaTime, which is more reliable of any of the framework classes when dealing with calendars, time zones, dates and times:
var timeString = "4:00 pm";
var pureDate = new DateTime(2017, 5, 22, 0, 0, 0, DateTimeKind.Utc);
var timezoneString = "America/Los_Angeles";
var localTime = ParseTimeString(timeString);
var localDate = LocalDate.FromDateTime(pureDate);
var localDateTime = localDate.At(localTime);
var zone = DateTimeZoneProviders.Tzdb[timezoneString];
var zonedDateTime = localDateTime.InZoneStrictly(zone);
Inside zonedDateTime you will find your full date:
"2017-05-22T16:00:00 America/Los_Angeles (-07)"
You may than use zonedDateTime.ToDateTimeUtc() to get a System.DateTime instance in UTC.
ParseTimeString parses your time string using your format specifier:
public static LocalTime ParseTimeString(string timeString)
{
var pattern = LocalTimePattern.CreateWithInvariantCulture("h:mm tt");
return pattern.Parse(timeString).Value;
}
Considerations if you use DateTime, DateTimeOffset and TimeZoneInfo
If you do not want to use NodaTime be aware of the possible pitfalls of the built-in classes:
TimeZoneInfo in Windows uses a different specifier that is not compatible with IANA/TZDB. Your America/Los_Angeles time zone string will not work if you do not convert it before use (see https://stackoverflow.com/tags/timezone/info)
DateTimeOffset (which is more reliable then DateTime) still loses information when created. The time zone data cannot be persisted and you will have just a date with an offset from UTC.
You'll need to manually parse your custom time string (maybe using regexes).
Here's how I ended up working it out w/o NodaTime:
public static DateTime Combine(DateTime date, string time, string timeZone)
{
TimeZoneInfo tzInfo = TimeZoneInfo.FindSystemTimeZoneById(TimezoneDictionary[timeZone]);
var timeOfDay = DateTime.ParseExact(time, "h:mm tt", null).TimeOfDay;
var combined = date.Add(timeOfDay).Subtract(tzInfo.BaseUtcOffset);
if (tzInfo.IsDaylightSavingTime(combined))
combined = combined.Subtract(TimeSpan.FromHours(1));
return combined;
}
I also need this dictionary to convert IANA timezones to Microsoft:
private static Dictionary<string, string> TimezoneDictionary = new Dictionary<string, string>
{
{ "America/Los_Angeles", "Pacific Standard Time" },
{ "America/Denver", "Mountain Standard Time" },
{ "America/Chicago", "Central Standard Time" },
{ "America/New_York", "Eastern Standard Time" },
//many more of course, just clipping this list for brevity.
};

Get origin time from a datetime string with offset

If I run:
// 7:10 am at a location which has a +2 offset from UTC
string timeString = "2011-06-15T07:10:25.894+02:00";
DateTime time = DateTime.Parse(timeString);
It gives me time = 6/14/2011 10:10:25 PM. This is the local time where I am at (Pacific time i.e. UTC -7).
Is there an elegant way of getting the local time at the origin i.e. 6/15/2011 07:10:25 AM?
You can use TimeZoneInfo:
DateTime localTime = DateTime.Now;
TimeZoneInfo targetTimeZone =
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTime targetTime = TimeZoneInfo.ConvertTime(localTime, targetTimeZone);
Actually, the ConvertTimeBySystemTimeZoneId method would be even more succinct:
DateTime targetTime =
TimeZoneInfo.ConvertTimeBySystemTimeZoneId(localTime, "Eastern Standard Time");
You can get information for time zones available using TimeZoneInfo.GetSystemTimeZones().
The DateTimeOffset structure seems to be built to specifically handle timezones. It includes most of the functionality of the DateTime type.
string timeString = "2011-06-15T07:10:25.894+02:00";
DateTimeOffset time = DateTimeOffset.Parse(timeString);
As this article illustrates, you should DateTimeOffset instead of DateTime whenever you need to unambiguously identify a single point in time.
Lock into using TimeZoneInfo - http://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx to do conversions. FindSystemTimeZoneById and ConvertTimeFromUtc should be enough. You may need to convert your local DateTime to UTC first with DateTime.ToUniversalTime.
You can format the way DateTime is Parse.
For example, if I want the DateTime to be format in french Canadian format :
IFormatProvider culture = new CultureInfo("fr-CA", true);
DateTime dt = DateTime.ParseExact(dateString, "dd-MM-yyyy", culture);
You can do it the same way for a en-US culture and add the time format to specify the format you want ...

Convert UTC DateTime to another Time Zone

I have a UTC DateTime value coming from a database record. I also have a user-specified time zone (an instance of TimeZoneInfo). How do I convert that UTC DateTime to the user's local time zone? Also, how do I determine if the user-specified time zone is currently observing DST? I'm using .NET 3.5.
Thanks,
Mark
The best way to do this is simply to use TimeZoneInfo.ConvertTimeFromUtc.
// you said you had these already
DateTime utc = new DateTime(2014, 6, 4, 12, 34, 0);
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
// it's a simple one-liner
DateTime pacific = TimeZoneInfo.ConvertTimeFromUtc(utc, tzi);
The only catch is that the incoming DateTime value may not have the DateTimeKind.Local kind. It must either be Utc, or Unspecified.
You can use a dedicated function within TimeZoneInfo if you want to convert a DateTimeOffset into another DateTimeOffset:
DateTimeOffset newTime = TimeZoneInfo.ConvertTime(
DateTimeOffset.UtcNow,
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")
);
Have a look at the DateTimeOffset structure:
// user-specified time zone
TimeZoneInfo southPole =
TimeZoneInfo.FindSystemTimeZoneById("Antarctica/South Pole Standard Time");
// an UTC DateTime
DateTime utcTime = new DateTime(2007, 07, 12, 06, 32, 00, DateTimeKind.Utc);
// DateTime with offset
DateTimeOffset dateAndOffset =
new DateTimeOffset(utcTime, southPole.GetUtcOffset(utcTime));
Console.WriteLine(dateAndOffset);
For DST see the TimeZoneInfo.IsDaylightSavingTime method.
bool isDst = southpole.IsDaylightSavingTime(DateTime.UtcNow);
The Antartica answer only works for the timezones matching UTC. I'm quite traumatized with this DateTimeOffset function and after hours of trial and error, I've managed to produce a practical conversion extension function that works with all timezones.
static public class DateTimeFunctions
{
static public DateTimeOffset ConvertUtcTimeToTimeZone(this DateTime dateTime, string toTimeZoneDesc)
{
if (dateTime.Kind != DateTimeKind.Utc) throw new Exception("dateTime needs to have Kind property set to Utc");
var toUtcOffset = TimeZoneInfo.FindSystemTimeZoneById(toTimeZoneDesc).GetUtcOffset(dateTime);
var convertedTime = DateTime.SpecifyKind(dateTime.Add(toUtcOffset), DateTimeKind.Unspecified);
return new DateTimeOffset(convertedTime, toUtcOffset);
}
}
Example:
var currentTimeInPacificTime = DateTime.UtcNow.ConvertUtcTimeToTimeZone("Pacific Standard Time");
Here's another gotcha: If you're running your code on a Linux server, you need to use the Linux system for timezone names. For example, "Central Standard Time" would be "America/Chicago". The tz list is here: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
Here's an example with the isWindows switch:
public static class DateTimeHelper
{
public static string ConvertUtcToCst(this string dateTimeString)
{
if (string.IsNullOrWhiteSpace(dateTimeString))
{
return string.Empty;
}
if (DateTime.TryParse(dateTimeString, out DateTime outputDateTime))
{
try
{
var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
TimeZoneInfo cstZone = null;
if (isWindows)
{
cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
}
else
{
cstZone = TimeZoneInfo.FindSystemTimeZoneById("America/Chicago");
}
var cstDateTime = TimeZoneInfo.ConvertTimeFromUtc(outputDateTime, cstZone);
return cstDateTime.ToString();
}
catch (TimeZoneNotFoundException)
{
return "The registry does not define the Central Standard Time zone.";
}
catch (InvalidTimeZoneException)
{
return "Registry data on the Central Standard Time zone has been corrupted.";
}
catch (Exception ex)
{
return $"Error :: {ex.Message} :: {ex.ToString()}";
}
}
return string.Empty;
}
}
// TO get Currrent Time in current Time Zone of your System
var dt = DateTime.Now;
Console.WriteLine(dt);
// Display Time Zone of your System
Console.WriteLine(TimeZoneInfo.Local);
// Convert Current Date Time to UTC Date Time
var utc = TimeZoneInfo.ConvertTimeToUtc(dt, TimeZoneInfo.Local);
Console.WriteLine(utc);
// Convert UTC Time to Current Time Zone
DateTime pacific = TimeZoneInfo.ConvertTimeFromUtc(utc, TimeZoneInfo.Local);
Console.WriteLine(pacific);
Console.ReadLine();

Categories