/* * [DaylightSavingEnd.java] * * Summary: calculate when daylight saving ends in North America. * * Copyright: (c) 1999-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 4.2 2008-12-03 add World AIDS day */ package com.mindprod.holidays; import com.mindprod.common18.BigDate; import java.util.Date; import java.util.TimeZone; import java.util.concurrent.TimeUnit; /** * calculate when daylight saving ends in North America. * * @author Roedy Green, Canadian Mind Products * @version 4.2 2008-12-03 add World AIDS day * @since 1999 */ public final class DaylightSavingEnd extends HolInfo { /** * @inheritDoc */ public String getAuthority() { return "Sun Java TimeZone tables."; } /** * @inheritDoc */ public int getFirstYear( int base ) { return 1916; } /** * @inheritDoc */ public String getName() { return "Daylight Saving Time End"; } /** * @inheritDoc */ public String getRule() { return "DST end date varies with your locale. In Canada and the USA it is usually the first Sunday in " + "November. Set your clock back an hour at 2AM."; } /** * @inheritDoc */ public int when( int year, boolean shift, int base ) { if ( !isYearValid( year, base ) ) { return BigDate.NULL_ORDINAL; } TimeZone tzHere = TimeZone.getDefault(); if ( !tzHere.useDaylightTime() ) { /* don't use DST ever */ return BigDate.NULL_ORDINAL; } final int fromOrd = BigDate.toOrdinal( year, 1, 1 ); final int toOrd = BigDate.toOrdinal( year, 12, 31 ); // find first day in year that DST stopped for ( int ord = fromOrd; ord <= toOrd; ord++ ) { BigDate b = new BigDate( ord ); // midnight Date d = b.getDate( tzHere ); // adjust to noon d.setTime( TimeUnit.HOURS.toMillis( 12 ) + d.getTime() ); if ( !tzHere.inDaylightTime( d ) ) { if ( ord == fromOrd ) { // we are in the northern hemisphere. Start looking in fall ord = BigDate.toOrdinal( year, 8, 1 ); } else { return shiftSatToFriSunToMon( ord, shift ); } } } return BigDate.NULL_ORDINAL; } // end when. }