Skip to content

Instantly share code, notes, and snippets.

@mishrsud
Created February 19, 2019 11:08
Show Gist options
  • Save mishrsud/391cc79f2c014d68e37aa64e1479edf3 to your computer and use it in GitHub Desktop.
Save mishrsud/391cc79f2c014d68e37aa64e1479edf3 to your computer and use it in GitHub Desktop.
// https://docs.microsoft.com/en-us/dotnet/api/system.timezoneinfo.createcustomtimezone?view=netframework-4.7.2
void Main()
{
TimeZoneInfo
.ConvertTime(
DateTimeOffset.Parse("2019-02-18 19:51 -05:00", CultureInfo.InvariantCulture),
TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time")).Dump("Sydney Time");
TimeZoneInfo
.ConvertTime(
DateTimeOffset.Parse("2019-02-18 19:51 -05:00", CultureInfo.InvariantCulture),
CreateAusEastTimezone()).Dump("Sydney Time");
}
// Define other methods and classes here
private static TimeZoneInfo CreateAusEastTimezone()
{
TimeZoneInfo.TransitionTime startTransition, endTransition;
startTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 2, 0, 0),
10, 1, DayOfWeek.Sunday);
endTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 2, 0, 0),
4, 1, DayOfWeek.Sunday);
var adjustmentRule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(
DateTime.MinValue.Date,
DateTime.MaxValue.Date,
TimeSpan.FromHours(1),
startTransition,
endTransition);
return TimeZoneInfo.CreateCustomTimeZone(
"AU Eastern Standard Time",
TimeSpan.FromHours(10),
"AEST",
"AU Eastern Standard Time",
"AU Eastern Daylight Saving Time",
new[] {adjustmentRule});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment