public ConvertToUtc ( System.DateTime localDateTime, string timeZoneId, ZoneLocalMappingResolver resolver = null ) : System.DateTime | ||
localDateTime | System.DateTime | |
timeZoneId | string | |
resolver | ZoneLocalMappingResolver | |
Résultat | System.DateTime |
public void Round_Trip_Utc_From_Time() { var tzHelper = new TimeZoneHelper(new DateTimeZoneCache(TzdbDateTimeZoneSource.Default)); var localTime = DateTime.Parse("09/03/2016 1:46 PM"); // central time but local time is eastern 2:26 Assert.True(localTime.Kind == DateTimeKind.Unspecified); Assert.True(localTime.Hour == 13); Assert.True(localTime.Minute == 46); var helperUtc = tzHelper.ConvertToUtc(localTime, "America/Chicago"); var serialized = helperUtc.ToString("O"); Assert.True(serialized == "2016-09-03T18:46:00.0000000Z"); var localTime2 = DateTime.Parse(serialized); //parsed with TZ convert to local system time which is eastern Assert.True(localTime2.Kind == DateTimeKind.Local); Assert.True(localTime2.Hour == 14); // local is eastern time so 1 hour later Assert.True(localTime2.Minute == 46); var helperLocal = tzHelper.ConvertToLocalTime(helperUtc, "America/New_York"); Assert.True(helperLocal.Kind == DateTimeKind.Unspecified); Assert.True(helperLocal.Hour == 14); // should be 1 hour later in NY Assert.True(helperLocal.Minute == 46); //"yyyy-MM-dd'T'HH:mm:ss.fffffff'Z'" var parsedUtc = DateTime.ParseExact(serialized, "O", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal); Assert.True(parsedUtc.Kind == DateTimeKind.Utc); Assert.True(parsedUtc.Hour == 18); // same is in serialzed string ie utc Assert.True(parsedUtc.Minute == 46); }
public void Resolves_Correct_Utc_From_Time() { var tzHelper = new TimeZoneHelper(new DateTimeZoneCache(TzdbDateTimeZoneSource.Default)); var utcNow = DateTime.UtcNow; var easternTzBcl = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"); var bclLocal = TimeZoneInfo.ConvertTime(DateTime.SpecifyKind(utcNow, DateTimeKind.Utc), easternTzBcl); //var bclLocal = DateTime.SpecifyKind( // TimeZoneInfo.ConvertTime(DateTime.SpecifyKind(utcNow, DateTimeKind.Utc), easternTzBcl), // DateTimeKind.Local); var helperUtc = tzHelper.ConvertToUtc(bclLocal, "America/New_York"); Assert.True(utcNow.Year == helperUtc.Year); Assert.True(utcNow.Month == helperUtc.Month); Assert.True(utcNow.Day == helperUtc.Day); Assert.True(utcNow.Hour == helperUtc.Hour); Assert.True(utcNow.Minute == helperUtc.Minute); Assert.True(utcNow.Second == helperUtc.Second); Assert.True(utcNow.Millisecond == helperUtc.Millisecond); Assert.True(utcNow.Kind == DateTimeKind.Utc); Assert.True(helperUtc.Kind == DateTimeKind.Utc); }