如何获取具有本地时间信息的 DateTimeOffset
How to get a DateTimeOffset having local time information
我有这些输入字符串:
var timeStr = "03:22";
var dateStr = "2018/01/12";
var format = "yyyy/MM/dd";
var timeZone = "Asia/Tehran";
这是我掌握的时间信息,timeStr
是Asia/Tehran
时区,不是UTC。
如何使用 NodaTime 获取包含正确偏移量的信息的 DateTimeOffset 对象?
如果您正在寻找 nodatime DateTimeOffset 对象,您可以这样做:
var timeStr = "03:22";
var dateStr = "2018/01/12";
// DateTime.Parse can deal with this format without specs
// var format = "yyyy/MM/dd";
var timeZone = "Asia/Tehran";
var date = DateTime.Parse(timeStr + " " + dateStr);
var zone = DateTimeZoneProviders.Tzdb[timeZone];
var timespanOffset = zone.GetUtcOffset(SystemClock.Instance.Now).ToTimeSpan();
var result = new DateTimeOffset(date, timespanOffset);
Console.Write(result.ToUniversalTime());
结果是:1/11/2018 10:52:00 PM +00:00
对应德黑兰时间为 GMT +4,5
让我们将您的所有信息转换为适当的 Noda Time 类型:
// Input values (as per the question)
var timeStr = "03:22";
var dateStr = "2018/01/12";
var format = "yyyy/MM/dd";
var timeZone = "Asia/Tehran";
// The patterns we'll use to parse input values
LocalTimePattern timePattern = LocalTimePattern.CreateWithInvariantCulture("HH:mm");
LocalDatePattern datePattern = LocalDatePattern.CreateWithInvariantCulture(format);
// The local parts, parsed with the patterns and then combined.
// Note that this will throw an exception if the values can't be parsed -
// use the ParseResult<T> return from Parse to check for success before
// using Value if you want to avoid throwing.
LocalTime localTime = timePattern.Parse(timeStr).Value;
LocalDate localDate = datePattern.Parse(dateStr).Value;
LocalDateTime localDateTime = localDate + localTime;
// Now get the time zone by ID
DateTimeZone zone = DateTimeZoneProviders.Tzdb[timeZone];
// Work out the zoned date/time being represented by the local date/time. See below for the "leniently" part.
ZonedDateTime zonedDateTime = localDateTime.InZoneLeniently(zone);
// The Noda Time type you want would be OffsetDateTime
OffsetDateTime offsetDateTime = zonedDateTime.ToOffsetDateTime();
// If you really want the BCL type...
DateTimeOffset dateTimeOffset = zonedDateTime.ToDateTimeOffset();
请注意 "InZoneLeniently" 处理不明确或跳过的本地 date/time 值,如下所示:
ambiguous values map to the earlier of the alternatives, and "skipped" values are shifted forward by the duration of the "gap".
这可能就是您想要的。还有 InZoneStrictly
如果给定的本地 date/time 没有表示一个时间点,它会抛出异常,或者你可以调用 InZone
并传入你自己的 [=13] =].
我有这些输入字符串:
var timeStr = "03:22";
var dateStr = "2018/01/12";
var format = "yyyy/MM/dd";
var timeZone = "Asia/Tehran";
这是我掌握的时间信息,timeStr
是Asia/Tehran
时区,不是UTC。
如何使用 NodaTime 获取包含正确偏移量的信息的 DateTimeOffset 对象?
如果您正在寻找 nodatime DateTimeOffset 对象,您可以这样做:
var timeStr = "03:22";
var dateStr = "2018/01/12";
// DateTime.Parse can deal with this format without specs
// var format = "yyyy/MM/dd";
var timeZone = "Asia/Tehran";
var date = DateTime.Parse(timeStr + " " + dateStr);
var zone = DateTimeZoneProviders.Tzdb[timeZone];
var timespanOffset = zone.GetUtcOffset(SystemClock.Instance.Now).ToTimeSpan();
var result = new DateTimeOffset(date, timespanOffset);
Console.Write(result.ToUniversalTime());
结果是:1/11/2018 10:52:00 PM +00:00
对应德黑兰时间为 GMT +4,5
让我们将您的所有信息转换为适当的 Noda Time 类型:
// Input values (as per the question)
var timeStr = "03:22";
var dateStr = "2018/01/12";
var format = "yyyy/MM/dd";
var timeZone = "Asia/Tehran";
// The patterns we'll use to parse input values
LocalTimePattern timePattern = LocalTimePattern.CreateWithInvariantCulture("HH:mm");
LocalDatePattern datePattern = LocalDatePattern.CreateWithInvariantCulture(format);
// The local parts, parsed with the patterns and then combined.
// Note that this will throw an exception if the values can't be parsed -
// use the ParseResult<T> return from Parse to check for success before
// using Value if you want to avoid throwing.
LocalTime localTime = timePattern.Parse(timeStr).Value;
LocalDate localDate = datePattern.Parse(dateStr).Value;
LocalDateTime localDateTime = localDate + localTime;
// Now get the time zone by ID
DateTimeZone zone = DateTimeZoneProviders.Tzdb[timeZone];
// Work out the zoned date/time being represented by the local date/time. See below for the "leniently" part.
ZonedDateTime zonedDateTime = localDateTime.InZoneLeniently(zone);
// The Noda Time type you want would be OffsetDateTime
OffsetDateTime offsetDateTime = zonedDateTime.ToOffsetDateTime();
// If you really want the BCL type...
DateTimeOffset dateTimeOffset = zonedDateTime.ToDateTimeOffset();
请注意 "InZoneLeniently" 处理不明确或跳过的本地 date/time 值,如下所示:
ambiguous values map to the earlier of the alternatives, and "skipped" values are shifted forward by the duration of the "gap".
这可能就是您想要的。还有 InZoneStrictly
如果给定的本地 date/time 没有表示一个时间点,它会抛出异常,或者你可以调用 InZone
并传入你自己的 [=13] =].