import java.util.Calendar;
import java.util.GregorianCalendar;

import cmp.business.BigDate;

/**
 * How Long until Christmas?
 */
public class Christmas
   {
   static final int MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000;
   public static void main ( String [] args )
      {
      // How long till Christmas? ( Sun's way.)
      // this code will give a negative number just after Christmas.
      System.out.println( "Your child asks, how long is it until Christmas?" );
      System.out.println( "Here are the possible answers you might give to an American child:" );
      GregorianCalendar now = new GregorianCalendar();
      int thisYear = now.get( Calendar.YEAR );

      // You may open presents at 7 AM December 25, local time
      System.out.println( "You may open your presents at 7 AM Christmas morning." );
      GregorianCalendar christmas = new GregorianCalendar( thisYear, Calendar.DECEMBER, 25, 7, 0, 0 );

      // millis since 1970 Jan 1
      long christmasTimeStamp = christmas.getTime().getTime();
      long nowTimeStamp = now.getTime().getTime();
      double dayUnitsDiff = ( christmasTimeStamp - nowTimeStamp ) / ( double) MILLISECONDS_PER_DAY;
      System.out.println( "1. It is " + dayUnitsDiff + " day units of 24 hours until you may open your presents." );
      System.out.println( "2. It is " + Math.ceil( dayUnitsDiff ) + " day units of 24 hours rounded up until you may open your presents." );
      System.out.println( "3. It is " + Math.round( dayUnitsDiff ) + " day units of 24 hours rounded until you may open your presents." );
      System.out.println( "4. It is " + Math.floor( dayUnitsDiff ) + " day units of 24 hours rounded down until you may open your presents." );

      int gmtChristmasOrdinal = (int)( christmasTimeStamp / MILLISECONDS_PER_DAY );
      int gmtNowOrdinal = (int)( nowTimeStamp / MILLISECONDS_PER_DAY );
      int gmtDiffInDays = gmtChristmasOrdinal - gmtNowOrdinal;
      System.out.println( "5. Children in Greenwich have " + gmtDiffInDays + " sleeps (midnight crossings) to go until\n" + "you may open your presents.\n" + "They may open theirs even sooner." );

      // For children living in the USA, the timezone offset will be negative.
      int christmasZoneOffset = christmas.get( Calendar.ZONE_OFFSET ) + christmas.get( Calendar.DST_OFFSET );
      int localChristmasOrdinal = (int)(( christmasTimeStamp + christmasZoneOffset ) / MILLISECONDS_PER_DAY );
      int nowZoneOffset = now.get( Calendar.ZONE_OFFSET) + now.get( Calendar.DST_OFFSET );
      int localNowOrdinal = (int)( ( nowTimeStamp +nowZoneOffset ) / MILLISECONDS_PER_DAY );
      int localDiffInDays = localChristmasOrdinal - localNowOrdinal;
      System.out.println( "6. You have " + localDiffInDays + " sleeps (midnight crossings) to go." );

      // how long till Christmas? ( with BigDate. )
      // this code will give a negative number just after Christmas.
      BigDate today = BigDate.localToday();
      BigDate nextChristmas = new BigDate( today.getYYYY(), 12, 25 );
      System.out.println( "7. Uncle Roedy's Bigdate says it is " + ( nextChristmas.getOrdinal() - today.getOrdinal()) + " sleeps until Christmas." );
      int [] age = BigDate.age( today, nextChristmas );
      System.out.println( "8. Or put another way " + age[0]+ " years and " + age [+ 1] " months and " + age[+ 2] " days to go." );
      }
   }