Clock Change Info - a possible addition to Joda-Time

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Clock Change Info - a possible addition to Joda-Time

Joda forum Bill Comer
Hi all,

I was recently had a requirement where I needed to determine if a day was a LONG, SHORT or NORMAL day,
& I wondered if it would be a useful addition to Joda-Time.


But here it is as well.

public enum ClockChangeInfo {
  
  LONG_DAY(90000000, 25), SHORT_DAY(82800000, 23), NORMAL_DAY(86400000, 24), INDETERMINATE(0, 0);

  private int mMsecs;
  private int mHrs;
  private static final Map<Integer, ClockChangeInfo> MSEC = new HashMap<Integer, ClockChangeInfo>();
  
  static {
    for (ClockChangeInfo cci : values()) {
      MSEC.put(new Integer(cci.mMsecs), cci);
    }
  }

  ClockChangeInfo(int aMsecs, int aHrs) {
    mMsecs = aMsecs;
    mHrs = aHrs;
  }

  public int getHrs(ClockChangeInfo aCci)  {
    return aCci.mHrs;
  }

  public static ClockChangeInfo getDayType(DateMidnight dateToCheck) {
    DateMidnight nextDay = dateToCheck.plusDays(1);
    
    //this is a safe convert as it is only for day(n) - day(n-1)
    Integer millis = (int)(nextDay.getMillis() - dateToCheck.getMillis());
    ClockChangeInfo cci = MSEC.get(millis);
    if (cci == null) {
      return INDETERMINATE;
    }
    return cci;
  }
}

public class ClockChangeUtil {
  
  public static ClockChangeInfo getDayType(DateMidnight dateToCheck) {
    return ClockChangeInfo.getDayType((dateToCheck));
  }

  public static int getNumHoursInDay(DateMidnight dateToCheck) {
    ClockChangeInfo dayType = ClockChangeUtil.getDayType(dateToCheck);
    return dayType.getHrs(dayType);
  }

  public static int getNumHalfHoursINMonthSoFar(DateMidnight dateToCheck) {
    int numHours = 0;
    int day = dateToCheck.getDayOfMonth();

    if (day == 1) {
      return 0;
    }

    DateMidnight dm = dateToCheck.minusDays(1);

    do {
      numHours += getNumHoursInDay(dm);

      dm = dm.minusDays(1);
      day--;
    } while (day > 1);

    return numHours * 2;
  }
}


& some tests

public class ClockChangeUtilUnitTest
{

  @Test
  public void test_isLongDay() throws Exception
  {
    DateMidnight dm28 = new DateMidnight(2011, 10, 28);
    DateMidnight dm29 = new DateMidnight(2011, 10, 29);
    DateMidnight dm30 = new DateMidnight(2011, 10, 30);
    DateMidnight dm31 = new DateMidnight(2011, 10, 31);  

    assertEquals(ClockChangeInfo.NORMAL_DAY, ClockChangeUtil.getDayType(dm28));
    assertEquals(ClockChangeInfo.NORMAL_DAY, ClockChangeUtil.getDayType(dm29));
    assertEquals(ClockChangeInfo.LONG_DAY, ClockChangeUtil.getDayType(dm30));
    assertEquals(ClockChangeInfo.NORMAL_DAY, ClockChangeUtil.getDayType(dm31));
  }
  

  @Test
  public void test_isShortDay() throws Exception
  {
    DateMidnight dm26 = new DateMidnight(2011, 3, 26);
    DateMidnight dm27 = new DateMidnight(2011, 3, 27);
    DateMidnight dm28 = new DateMidnight(2011, 3, 28);
    DateMidnight dm29 = new DateMidnight(2011, 3, 29);

    assertEquals(ClockChangeInfo.NORMAL_DAY, ClockChangeUtil.getDayType(dm26));
    assertEquals(ClockChangeInfo.SHORT_DAY, ClockChangeUtil.getDayType(dm27));
    assertEquals(ClockChangeInfo.NORMAL_DAY, ClockChangeUtil.getDayType(dm28));
    assertEquals(ClockChangeInfo.NORMAL_DAY, ClockChangeUtil.getDayType(dm29));
  }
  

  @Test
  public void testClockChange() throws Exception
  {
    long millisInANormalDay = 24 * 60 * 60 * 1000;
    long millisInALongDay = 25 * 60 * 60 * 1000;
    
    DateMidnight dm28 = new DateMidnight(2011, 10, 28);
    DateMidnight dm29 = new DateMidnight(2011, 10, 29);
    DateMidnight dm30 = new DateMidnight(2011, 10, 30);
    DateMidnight dm31 = new DateMidnight(2011, 10, 31);
    
    assertEquals(millisInANormalDay, dm29.getMillis() - dm28.getMillis());
    assertEquals(millisInANormalDay, dm30.getMillis() - dm29.getMillis());
    assertEquals(millisInALongDay, dm31.getMillis() - dm30.getMillis());
    

    DateTime dt28 = new DateTime(dm28);
    DateTime dt29 = new DateTime(dm29);
    DateTime dt30 = new DateTime(dm30);
    DateTime dt31 = new DateTime(dm31);
    
    assertEquals(millisInANormalDay, dt29.getMillis() - dt28.getMillis());
    assertEquals(millisInANormalDay, dt30.getMillis() - dt29.getMillis());
    assertEquals(millisInALongDay, dt31.getMillis() - dt30.getMillis());
    
    assertTrue(dt28.getZone().equals(dt29.getZone()));
    assertTrue(dt28.getZone().equals(dt30.getZone()));
    assertTrue(dt28.getZone().equals(dt31.getZone()));
  }
  
  @Test
  public void test_getNumHoursInDay() throws Exception
  {

    DateMidnight dm26 = new DateMidnight(2011, 3, 26);
    DateMidnight dm27 = new DateMidnight(2011, 3, 27);
    DateMidnight dm28 = new DateMidnight(2011, 3, 28);
    DateMidnight dm29 = new DateMidnight(2011, 3, 29);
    
    assertEquals(24, ClockChangeUtil.getNumHoursInDay(dm26));
    assertEquals(23, ClockChangeUtil.getNumHoursInDay(dm27));
    assertEquals(24, ClockChangeUtil.getNumHoursInDay(dm28));
    assertEquals(24, ClockChangeUtil.getNumHoursInDay(dm29));
  }


------------------------------------------------------------------------------
Monitor your physical, virtual and cloud infrastructure from a single
web console. Get in-depth insight into apps, servers, databases, vmware,
SAP, cloud infrastructure, etc. Download 30-day Free Trial.
Pricing starts from $795 for 25 servers or applications!
http://p.sf.net/sfu/zoho_dev2dev_nov
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Clock Change Info - a possible addition to Joda-Time

jodastephen
I can see how this might be useful. It is essentially the Duration
between the start of two dates.

There are complications that this code doesn't handle. Firstly,
DateMidnight is effectively deprecated as it doesn't handle the case
where 00:00 to 01:00 does not exist due to DST. There are definitely
other cases than 23, 24 and 35 hours days.

Although I've never seen it, it would be possible for DST to skip
23:30 to 00:30, or repeat those times. That would haveto be handled as
well.

Finally, a Period is probably the right result type, once the data is
calculated.

So, yes poentially useful, but hard to implement (harder than the code below ;-)
thanks
Stephen


On 22 November 2012 10:41, Joda forum Bill Comer
<[hidden email]> wrote:

> Hi all,
>
> I was recently had a requirement where I needed to determine if a day was a
> LONG, SHORT or NORMAL day,
> & I wondered if it would be a useful addition to Joda-Time.
>
> I have written it up here -
> http://billcomer.blogspot.co.uk/2012/11/clock-changing-is-this-long-short-or.html
>
> But here it is as well.
>
> public enum ClockChangeInfo {
>
>   LONG_DAY(90000000, 25), SHORT_DAY(82800000, 23), NORMAL_DAY(86400000, 24),
> INDETERMINATE(0, 0);
>
>   private int mMsecs;
>   private int mHrs;
>   private static final Map<Integer, ClockChangeInfo> MSEC = new
> HashMap<Integer, ClockChangeInfo>();
>
>   static {
>     for (ClockChangeInfo cci : values()) {
>       MSEC.put(new Integer(cci.mMsecs), cci);
>     }
>   }
>
>   ClockChangeInfo(int aMsecs, int aHrs) {
>     mMsecs = aMsecs;
>     mHrs = aHrs;
>   }
>
>   public int getHrs(ClockChangeInfo aCci)  {
>     return aCci.mHrs;
>   }
>
>   public static ClockChangeInfo getDayType(DateMidnight dateToCheck) {
>     DateMidnight nextDay = dateToCheck.plusDays(1);
>
>     //this is a safe convert as it is only for day(n) - day(n-1)
>     Integer millis = (int)(nextDay.getMillis() - dateToCheck.getMillis());
>     ClockChangeInfo cci = MSEC.get(millis);
>     if (cci == null) {
>       return INDETERMINATE;
>     }
>     return cci;
>   }
> }
>
> public class ClockChangeUtil {
>
>   public static ClockChangeInfo getDayType(DateMidnight dateToCheck) {
>     return ClockChangeInfo.getDayType((dateToCheck));
>   }
>
>   public static int getNumHoursInDay(DateMidnight dateToCheck) {
>     ClockChangeInfo dayType = ClockChangeUtil.getDayType(dateToCheck);
>     return dayType.getHrs(dayType);
>   }
>
>   public static int getNumHalfHoursINMonthSoFar(DateMidnight dateToCheck) {
>     int numHours = 0;
>     int day = dateToCheck.getDayOfMonth();
>
>     if (day == 1) {
>       return 0;
>     }
>
>     DateMidnight dm = dateToCheck.minusDays(1);
>
>     do {
>       numHours += getNumHoursInDay(dm);
>
>       dm = dm.minusDays(1);
>       day--;
>     } while (day > 1);
>
>     return numHours * 2;
>   }
> }
>
>
> & some tests
>
> public class ClockChangeUtilUnitTest
> {
>
>   @Test
>   public void test_isLongDay() throws Exception
>   {
>     DateMidnight dm28 = new DateMidnight(2011, 10, 28);
>     DateMidnight dm29 = new DateMidnight(2011, 10, 29);
>     DateMidnight dm30 = new DateMidnight(2011, 10, 30);
>     DateMidnight dm31 = new DateMidnight(2011, 10, 31);
>
>     assertEquals(ClockChangeInfo.NORMAL_DAY,
> ClockChangeUtil.getDayType(dm28));
>     assertEquals(ClockChangeInfo.NORMAL_DAY,
> ClockChangeUtil.getDayType(dm29));
>     assertEquals(ClockChangeInfo.LONG_DAY,
> ClockChangeUtil.getDayType(dm30));
>     assertEquals(ClockChangeInfo.NORMAL_DAY,
> ClockChangeUtil.getDayType(dm31));
>   }
>
>
>   @Test
>   public void test_isShortDay() throws Exception
>   {
>     DateMidnight dm26 = new DateMidnight(2011, 3, 26);
>     DateMidnight dm27 = new DateMidnight(2011, 3, 27);
>     DateMidnight dm28 = new DateMidnight(2011, 3, 28);
>     DateMidnight dm29 = new DateMidnight(2011, 3, 29);
>
>     assertEquals(ClockChangeInfo.NORMAL_DAY,
> ClockChangeUtil.getDayType(dm26));
>     assertEquals(ClockChangeInfo.SHORT_DAY,
> ClockChangeUtil.getDayType(dm27));
>     assertEquals(ClockChangeInfo.NORMAL_DAY,
> ClockChangeUtil.getDayType(dm28));
>     assertEquals(ClockChangeInfo.NORMAL_DAY,
> ClockChangeUtil.getDayType(dm29));
>   }
>
>
>   @Test
>   public void testClockChange() throws Exception
>   {
>     long millisInANormalDay = 24 * 60 * 60 * 1000;
>     long millisInALongDay = 25 * 60 * 60 * 1000;
>
>     DateMidnight dm28 = new DateMidnight(2011, 10, 28);
>     DateMidnight dm29 = new DateMidnight(2011, 10, 29);
>     DateMidnight dm30 = new DateMidnight(2011, 10, 30);
>     DateMidnight dm31 = new DateMidnight(2011, 10, 31);
>
>     assertEquals(millisInANormalDay, dm29.getMillis() - dm28.getMillis());
>     assertEquals(millisInANormalDay, dm30.getMillis() - dm29.getMillis());
>     assertEquals(millisInALongDay, dm31.getMillis() - dm30.getMillis());
>
>
>     DateTime dt28 = new DateTime(dm28);
>     DateTime dt29 = new DateTime(dm29);
>     DateTime dt30 = new DateTime(dm30);
>     DateTime dt31 = new DateTime(dm31);
>
>     assertEquals(millisInANormalDay, dt29.getMillis() - dt28.getMillis());
>     assertEquals(millisInANormalDay, dt30.getMillis() - dt29.getMillis());
>     assertEquals(millisInALongDay, dt31.getMillis() - dt30.getMillis());
>
>     assertTrue(dt28.getZone().equals(dt29.getZone()));
>     assertTrue(dt28.getZone().equals(dt30.getZone()));
>     assertTrue(dt28.getZone().equals(dt31.getZone()));
>   }
>
>   @Test
>   public void test_getNumHoursInDay() throws Exception
>   {
>
>     DateMidnight dm26 = new DateMidnight(2011, 3, 26);
>     DateMidnight dm27 = new DateMidnight(2011, 3, 27);
>     DateMidnight dm28 = new DateMidnight(2011, 3, 28);
>     DateMidnight dm29 = new DateMidnight(2011, 3, 29);
>
>     assertEquals(24, ClockChangeUtil.getNumHoursInDay(dm26));
>     assertEquals(23, ClockChangeUtil.getNumHoursInDay(dm27));
>     assertEquals(24, ClockChangeUtil.getNumHoursInDay(dm28));
>     assertEquals(24, ClockChangeUtil.getNumHoursInDay(dm29));
>   }
>
> }
>
> ------------------------------------------------------------------------------
> Monitor your physical, virtual and cloud infrastructure from a single
> web console. Get in-depth insight into apps, servers, databases, vmware,
> SAP, cloud infrastructure, etc. Download 30-day Free Trial.
> Pricing starts from $795 for 25 servers or applications!
> http://p.sf.net/sfu/zoho_dev2dev_nov
> _______________________________________________
> Joda-interest mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/joda-interest
>

------------------------------------------------------------------------------
Monitor your physical, virtual and cloud infrastructure from a single
web console. Get in-depth insight into apps, servers, databases, vmware,
SAP, cloud infrastructure, etc. Download 30-day Free Trial.
Pricing starts from $795 for 25 servers or applications!
http://p.sf.net/sfu/zoho_dev2dev_nov
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Loading...