Menu

#65 LocalDateTime Ambiguity

open
nobody
Joda-Time (26)
5
2012-10-08
2011-02-16
No

The DateTimeZone object already has the isLocalDateTimeGap() method that is quite useful since every place with Daylight Saving Time have on time gap by year.
This gap occurs every time that the standard time changes to DST.
It's also true that there's at least one LocalDateTime ambiguity by year.

Any time that the DST changes to standard time there will be duplicated valid values hours for hour+minute+second+millis.
Usually from 23:00:00.000 to 23:59:59.999.
It would be great to have an isLocalDateTimeAmbiguity ( or isLocalDateTimeAmbiguous or isAmbiguousLoaclDateTime or something like that) on the DateTimeZone object (the same place as isLocalDateTimeGap() ) .

I'm suggesting a possible implementation below:

public boolean isLocalDateTimeAmbiguity( LocalDateTime ldt )
{
final DateTime dt = ldt.toDateTime(dateTimeZone);
final long currentTime = dt.getMillis();
final int currentOffset = dateTimeZone.getOffset( currentTime );
final long nextTransitionTime = dateTimeZone.nextTransition( currentTime );

    int nextOffset = dateTimeZone.getOffset( nextTransitionTime );

    if( nextOffset < currentOffset )
    {
        int timeBackMillis = nextOffset - currentOffset;
        if( (nextTransitionTime + timeBackMillis) <= currentTime )
            return true;
    }

    return false;

}

I tested this on some timezones and it seems to work fine.
As an example try to use:
dateTimeZone = DateTimeZone.forID("America/Sao_Paulo");
This year DST of "America/Sao_Paulo" ( The TimeZone where i live) will end just after 2011/02/20so the last DST millisecond will be
2011/02/19 23:59:59.999

Try the isLocalDateTimeAmbiguity for anything between new LocalDateTime( 2011,2,19,23,00,00,000 ) and new LocalDateTime( 2011,2,19,23,59,59,999 )
and you will a "true" return.

Any fixed timezone like UTC will always return false.

Discussion


Log in to post a comment.