我正在寻找OffsetTimeNodaTime的某种支持,但我没有看到任何东西.我正在以"17:13:00 + 10:00"等格式接收数据.我将此视为时间偏移,将其应用于给定日期(用户可以控制)到达当地时间以进行显示.
我能想出的最好的是:
// the date for this OffsetDateTime will be 1/1/2000
var parsed = OffsetDateTimePattern.CreateWithInvariantCulture("HH:mm:sso<G>").Parse(input).Value;
var desiredLocalDate = new LocalDate(2017, 06, 13);
var adjusted = new OffsetDateTime(
new LocalDateTime(desiredLocalDate.Year, desiredLocalDate.Month, desiredLocalDate.Day, parsed.Hour, parsed.Minute, parsed.Second, parsed.Millisecond),
parsed.Offset);
var localTime = adjusted.LocalDateTime;
Run Code Online (Sandbox Code Playgroud)
我想我想知道我是否忽略了更好的方法来做到这一点.
更新:这将出现在 Noda Time 2.3 中。
不,野田时间里没有任何东西可以代表这一点。这是一个相当奇怪的值,因为至少在许多时区,偏移量在一年中会有所不同。我知道有时我们需要利用现有的资源。
我可能会将其保留为两个字段: anOffset和 a LocalTime。一旦你OffsetDateTime有了LocalDate. 您可以OffsetDateTime像您已经在做的那样通过 an 获取这两个值,但我建议尽快将其拆分为两个值,以避免任何暗示其中有有用的日期。
如果你想保留现有的代码结构,你至少可以让它变得更简单:
// The date for this OffsetDateTime will be 1/1/2000
// Note: the pattern can be created once and reused; it's thread-safe.
var parsed = OffsetDateTimePattern.CreateWithInvariantCulture("HH:mm:sso<G>")
.Parse(input).Value;
var desiredLocalDate = new LocalDate(2017, 06, 13);
var adjusted = desiredLocalDate.At(parsed.TimeOfDay).WithOffset(parsed.Offset);
var localTime = adjusted.LocalDateTime;
Run Code Online (Sandbox Code Playgroud)
请注意,localTime这里总是相当于desiredLocalDate.At(parsed.TimeOfDay)- 它不像偏移量被“添加”到它。
| 归档时间: |
|
| 查看次数: |
304 次 |
| 最近记录: |