public void Should_be_the_same_time_for_all_time_zones_outside_daylight_savings() { var dt = new DateTime(2020, 1, 26, 0, 0, 0); var eastern = new EasternDateTime(dt.Year, dt.Month, dt.Day, 11, 0, 0); var central = new CentralDateTime(dt.Year, dt.Month, dt.Day, 10, 0, 0); var mountain = new MountainDateTime(dt.Year, dt.Month, dt.Day, 9, 0, 0); var pacific = new PacificDateTime(dt.Year, dt.Month, dt.Day, 8, 0, 0); var alaska = new AlaskaDateTime(dt.Year, dt.Month, dt.Day, 7, 0, 0); var hawaii = new HawaiiDateTime(dt.Year, dt.Month, dt.Day, 6, 0, 0); var list = new List <int>(); list.Add(Compare.Dates(eastern, central)); list.Add(Compare.Dates(eastern, mountain)); list.Add(Compare.Dates(eastern, pacific)); list.Add(Compare.Dates(eastern, alaska)); list.Add(Compare.Dates(eastern, hawaii)); list.Add(Compare.Dates(central, mountain)); list.Add(Compare.Dates(central, pacific)); list.Add(Compare.Dates(central, alaska)); list.Add(Compare.Dates(central, hawaii)); list.Add(Compare.Dates(mountain, pacific)); list.Add(Compare.Dates(mountain, alaska)); list.Add(Compare.Dates(mountain, hawaii)); list.Add(Compare.Dates(pacific, alaska)); list.Add(Compare.Dates(pacific, hawaii)); list.Add(Compare.Dates(alaska, hawaii)); list.ForEach(x => { Assert.Equal(0, x); }); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var dateTime = (DateTime?)base.ReadJson(reader, typeof(DateTime?), existingValue, serializer); if (dateTime.HasValue) { if (objectType == typeof(EasternDateTime)) { return(EasternDateTime.Convert(dateTime.Value)); } else if (objectType == typeof(CentralDateTime)) { return(CentralDateTime.Convert(dateTime.Value)); } else if (objectType == typeof(MountainDateTime)) { return(MountainDateTime.Convert(dateTime.Value)); } else if (objectType == typeof(PacificDateTime)) { return(PacificDateTime.Convert(dateTime.Value)); } else if (objectType == typeof(AlaskaDateTime)) { return(AlaskaDateTime.Convert(dateTime.Value)); } else if (objectType == typeof(HawaiiDateTime)) { return(HawaiiDateTime.Convert(dateTime.Value)); } else if (objectType == typeof(UniversalDateTime)) { return(UniversalDateTime.Convert(dateTime.Value)); } } return(null); }
public void Should_be_the_same_time_for_all_time_zones_inside_a() { var dt = new DateTime(2020, 6, 26, 0, 0, 0); var eastern = new EasternDateTime(dt.Year, dt.Month, dt.Day, 11, 0, 0); var central = new CentralDateTime(dt.Year, dt.Month, dt.Day, 10, 0, 0); var mountain = new MountainDateTime(dt.Year, dt.Month, dt.Day, 9, 0, 0); var pacific = new PacificDateTime(dt.Year, dt.Month, dt.Day, 8, 0, 0); var alaska = new AlaskaDateTime(dt.Year, dt.Month, dt.Day, 7, 0, 0); //since hawaii doesnt observe... need to tack it on var hawaii = new HawaiiDateTime(dt.Year, dt.Month, dt.Day, 5, 0, 0); var list = new List <int>(); list.Add(Compare.Dates(eastern, central)); list.Add(Compare.Dates(eastern, mountain)); list.Add(Compare.Dates(eastern, pacific)); list.Add(Compare.Dates(eastern, alaska)); list.Add(Compare.Dates(eastern, hawaii)); list.Add(Compare.Dates(central, mountain)); list.Add(Compare.Dates(central, pacific)); list.Add(Compare.Dates(central, alaska)); list.Add(Compare.Dates(central, hawaii)); list.Add(Compare.Dates(mountain, pacific)); list.Add(Compare.Dates(mountain, alaska)); list.Add(Compare.Dates(mountain, hawaii)); list.Add(Compare.Dates(pacific, alaska)); list.Add(Compare.Dates(pacific, hawaii)); list.Add(Compare.Dates(alaska, hawaii)); list.ForEach(x => { Assert.Equal(0, x); }); }
public override DateTimeBase Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType == JsonTokenType.Number) { if (reader.TryGetInt64(out var unixSeconds)) { var utcDateTime = FromUnixTime(unixSeconds); if (typeToConvert == typeof(EasternDateTime)) { return(EasternDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(CentralDateTime)) { return(CentralDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(MountainDateTime)) { return(MountainDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(PacificDateTime)) { return(PacificDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(AlaskaDateTime)) { return(AlaskaDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(HawaiiDateTime)) { return(HawaiiDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(UniversalDateTime)) { return(UniversalDateTime.Convert(utcDateTime)); } } if (reader.TryGetDecimal(out var unixDecimalSeconds)) { var utcDateTime = FromUnixTime(Convert.ToInt64(unixDecimalSeconds)); if (typeToConvert == typeof(EasternDateTime)) { return(EasternDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(CentralDateTime)) { return(CentralDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(MountainDateTime)) { return(MountainDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(PacificDateTime)) { return(PacificDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(AlaskaDateTime)) { return(AlaskaDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(HawaiiDateTime)) { return(HawaiiDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(UniversalDateTime)) { return(UniversalDateTime.Convert(utcDateTime)); } } } if (reader.TokenType == JsonTokenType.String) { var value = reader.GetString(); if (typeToConvert == typeof(EasternDateTime) && EasternDateTime.TryParse(value, out var easternDateTime)) { return(easternDateTime); } else if (typeToConvert == typeof(CentralDateTime) && CentralDateTime.TryParse(value, out var centralDateTime)) { return(centralDateTime); } else if (typeToConvert == typeof(MountainDateTime) && MountainDateTime.TryParse(value, out var mountainDateTime)) { return(mountainDateTime); } else if (typeToConvert == typeof(PacificDateTime) && PacificDateTime.TryParse(value, out var pacificDateTime)) { return(pacificDateTime); } else if (typeToConvert == typeof(AlaskaDateTime) && AlaskaDateTime.TryParse(value, out var alaskaDateTime)) { return(alaskaDateTime); } else if (typeToConvert == typeof(HawaiiDateTime) && HawaiiDateTime.TryParse(value, out var hawaiiDateTime)) { return(hawaiiDateTime); } else if (typeToConvert == typeof(UniversalDateTime) && UniversalDateTime.TryParse(value, out var utcDateTime)) { return(utcDateTime); } if (long.TryParse(value, out var unixSeconds)) { var utcDateTime = FromUnixTime(unixSeconds); if (typeToConvert == typeof(EasternDateTime)) { return(EasternDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(CentralDateTime)) { return(CentralDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(MountainDateTime)) { return(MountainDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(PacificDateTime)) { return(PacificDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(AlaskaDateTime)) { return(AlaskaDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(HawaiiDateTime)) { return(HawaiiDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(UniversalDateTime)) { return(UniversalDateTime.Convert(utcDateTime)); } } if (decimal.TryParse(value, out var unixDecmialSeconds)) { var utcDateTime = FromUnixTime(Convert.ToInt64(unixDecmialSeconds)); if (typeToConvert == typeof(EasternDateTime)) { return(EasternDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(CentralDateTime)) { return(CentralDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(MountainDateTime)) { return(MountainDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(PacificDateTime)) { return(PacificDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(AlaskaDateTime)) { return(AlaskaDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(HawaiiDateTime)) { return(HawaiiDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(UniversalDateTime)) { return(UniversalDateTime.Convert(utcDateTime)); } } } return(null); }