public ConvertToLocalTime ( System.DateTime utcDateTime, string timeZoneId ) : System.DateTime | ||
utcDateTime | System.DateTime | |
timeZoneId | string | |
Результат | System.DateTime |
public void Resolves_Correct_LocalTime() { 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 helperLocal = tzHelper.ConvertToLocalTime(utcNow, "America/New_York"); Assert.True(bclLocal.Year == helperLocal.Year); Assert.True(bclLocal.Month == helperLocal.Month); Assert.True(bclLocal.Day == helperLocal.Day); Assert.True(bclLocal.Hour == helperLocal.Hour); Assert.True(bclLocal.Minute == helperLocal.Minute); Assert.True(bclLocal.Second == helperLocal.Second); Assert.True(bclLocal.Millisecond == helperLocal.Millisecond); Assert.True(bclLocal.Kind == DateTimeKind.Unspecified); Assert.True(helperLocal.Kind == DateTimeKind.Unspecified); // this one fails but for practical results the above work andarre good enough //Assert.True(bclLocal.Equals(helperLocal)); }
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); }