OffsetTime at NodaTime

I'm looking for some kind of OffsetTime support in NodaTime, but can't see anything. I get the data in the format "17: 13: 00 + 10: 00". I should consider this as a temporary offset, applying it to a specific date (which the user controls) in order to get to local time for display purposes.

The best I could come up with was:

 // 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; 

I think I'm wondering if I'm missing a better way to do this.

+5
source share
1 answer

Update: Now it will be in Noda Time 2.3.


No, there is nothing like that in Noda. This is a rather strange value, since, at least in many time zones, the offset will change throughout the year. I understand that sometimes we need to work with what we have.

I would save it as two fields: Offset and LocalTime . Then you can create OffsetDateTime when you have LocalDate . You can get these two via OffsetDateTime , as you already do, but I would suggest dividing it into two values ​​as soon as possible to avoid a hint that there is a useful date.

If you want to preserve the existing code structure, you can at least make it a lot easier:

 // The date for this OffsetDateTime will be 1/1/2000 // Note: the pattern can be created once and reused; it 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; 

Note that LocalTime here will always be equivalent to desiredLocalDate.At(parsed.TimeOfDay) - this is not the way an offset is added to it.

+3
source

Source: https://habr.com/ru/post/1268796/


All Articles