JodaTime - how can I know whether a daylight saving occurs within a specified period of time?
Asked Answered
P

4

4

I need to know whether the period defined by:

DateTime start;
DateTime end;

has a DST inside.

I am iterating over collection of periods defined by {start,end} and shifting start and end 24 hours forward in every iteration. The resulting period starts at midnight and ends at 1 ms before next midnight. I found that if the period has a daylight saving point inside the shift produces incorrect result, e.g:

having:

Duration targetDuration = new Duration(24*60*60*1000L-1);
DateTime start = new DateTime("2012-03-10T00:00:00.000-08:00");
DateTime end = new DateTime("2012-03-10T23:59:59.999-08:00");   

then the shift is done:

start = end.plusMillis(1);
end = start.plus(targetDuration);

produces:

start = "2012-03-11T00:00:00.000-08:00"
end = "2012-03-12T00:59:59.999-07:00"   

I wonder is there any standard API in JodaTime that can check whether the period of time has a DST inside?

Proportioned answered 9/7, 2012 at 6:16 Comment(2)
See: https://mcmap.net/q/393676/-algorithm-to-determine-daylight-saving-time-of-a-dateAmenable
@MicSim, thanks but the question was about JodaTime standard API. I am looking for one-liner not for the long arithmetics on unix time.Proportioned
T
4

Use the DateTimeZone.nextTransition method. If the start is less than the end DateTime, then at least one time zone transition has occurred in between. This does not account for rule changes vs. DST. That is, a time zone might have a new rule indicating that standard time has a new offset, and this would appear as a time zone transition.

if (start.getZone().nextTransition(start.getMillis()) < end.getMillis()) {
    // Time zone transition occurred, possibly due to DST
    ...
}
Terrance answered 11/7, 2012 at 4:21 Comment(0)
I
2

As long as start and end are in the correct time zone (e.g., created using this constructor) then the Interval created using them should take DST for that time zone into account. If the Duration of that Interval is not equal to 24 hours, then you've crossed the DST point.

Implosive answered 9/7, 2012 at 8:54 Comment(2)
This is not a good general purpose solution for arbitrary dates. If there are multiple DST transitions, then the duration can still end up being evenly divisible by 24.Terrance
I don't see how there can be multiple DST transitions within a single 24-hour period, as was asked. Nonetheless, yours is indeed better for arbitrary dates.Implosive
C
1

If you have the DateTimeZone in a variable called, say dateTimeZone, then you can use dateTimeZone.isStandardOffset(start.getMillis()) and dateTimeZone.isStandardOffset(end.getMillis()) to determine whether Daylight Savings Time started or ended within the time period represented.

public boolean isStandardOffset(long instant)

Checks whether, at a particular instant, the offset is raw or not. This method can be used to estimate whether Summer Time (DST) applies at the specified instant. As a general rule, if the actual offset equals the raw offset at the specified instant then either winter time applies or the zone does not have DST rules. If the actual offset does not equal the raw offset, then some form of Summer Time applies.

The implementation of the method is simply whether getOffset(long) equals getStandardOffset(long) at the specified instant.

This method should be named isRawOffsetInUse() but cannot be renamed for compatibility reasons.

Parameters: instant - milliseconds from 1970-01-01T00:00:00Z to get the offset for

Returns: true if the offset at the given instant is the same as the raw offset

Since: 1.5

https://www.joda.org/joda-time/apidocs/org/joda/time/DateTimeZone.html#isStandardOffset-long-

Carducci answered 8/7 at 23:7 Comment(0)
C
0

Because Daylight Savings Time is heavily reliant on TimeZones (some areas don't practice DST, some move clocks 1 hour, some 2 etc) your time variables are going to have to account for location as well.

As such, you might have to have a look at the DateTimeZone class

DateTimeZone

Cadelle answered 9/7, 2012 at 6:25 Comment(1)
How does it help to handle a DST?Proportioned

© 2022 - 2024 — McMap. All rights reserved.