/* Demo on 19 Feb 2008 */
/** An instance represents the time of day in a time zone, in terms of
  * hours, minutes, and seconds. The implemented time zones are:
  * 
  GMT: Greenwich Mean Time, GMT
  BST: British Summer Time, GMT+1
  EST: Eastern Standard Time, GMT-5 hours (NY)
  EDT: Eastern Daylight Savings Time, GMT-4 hours (NY)
  CST: Central Standard Time, GMT-6 hours (Chicago)
  CDT: Central Daylight Savings Time, GMT-5 hours (Chicago)
  MST: Mountain Standard Time, GMT-7 hours (Phoenix)
  MDT: Mountain Daylight Savings Time, GMT-6 (Phoenix)
  PST: Pacific Standard Time, GMT-8 hours (LA)
  PDT: Pacific Daylight Saving Time, GMT-7 hours (LA)
  IND: India time, GMT+5:30 hours (New Delhi)
  
  India (IND) is included only to show that times are not always on
  hourly boundaries from GMT.
  
  A time may appear negative or greater than 24 hours.
  This is because we allow a conversion of a time from one time zone to
  another, and a time of 0 hours GMT is -7 hours PDT (for example),
  while a time of 23:59 GMT is 29:29 IND.
  
  An instance of the class can show the time using a 24-hour clock
  or using the AM-PM designation; it is the user's choice.  */
public class Time {
    public static final String GMT= "GMT";
    public static final String BST= "BST";
    public static final String EST= "EST";
    public static final String EDT= "EDT";
    public static final String CST= "CST";
    public static final String CDT= "CDT";
    public static final String MST= "MST";
    public static final String MDT= "MDT";
    public static final String PST= "PST";
    public static final String PDT= "PDT";
    public static final String IND= "IND";
    
    /** Class invariant: Variable time is a time in seconds on a day in
      time zone zone. The time may be negative or greater than 24
      hours, as indicated in class specification (which says why). Field 
      display12Hr has the meaning
      "the time should be viewed as a 12-hour clockÓ.
      */
    private int time= 0;
    private String zone= "GMT";
    private boolean display12Hr= false;
    
    
    /** Constructor: instance with time 0 in GMT and a 24-hour clock */
    public Time() { }
    
    /** Constructor: s seconds, GMT, with 24-hour clock.
      Precondition: s should be in range  >-24 hours and <48 hours;
      when translated from any time zone.*/
    public Time(int s) {  
        this();
        time= s;
    } 
    
    /** Constructor: s seconds, zone z, with 12-hour clock iff b is true.
      * Precondition: s should be in range  >-24 hours and <48 hours;
      * when translated from any time zone.*/
    public Time(int s, String z, boolean b) {
        this(s);
        zone= z;
        display12Hr= b;
    }
    
    /** Constructor: h hours, m minutes, and s seconds in zone z.
      The time should be >-24 hours and <+48 hours; if not, 0 is used.
      If z is not a legal zone, make it GMT. 
      The time should be displayed as am-pm iff b is true */
    public Time(int h, int m, int s, String z, boolean b) {       
    }
    
    /** = a string representation of the time. This is basically in the
      form "hours:minutes:seconds zone", but it differs depending on
      whether a 12- or 24-hour clock is wanted. 
      We describe the difference with examples:
      In AM-PM mode, output could be:
      06:20:05AM DST or  06:20:05PM DST
      In 24-hour mode:
      06:20:05 DST   or   18:20:05 DST
      
      If the time is negative or at least 24 hours, print it using the
      24-hour mode, even if 12-hour mode is indicated.
      */
    public String toString() {
        int sec= 0;   // Field time contains the time in seconds. Local
        int min= 0;   // variables hr, min, and sec will contain the corres-
        int hr= 0;    // ponding time broken into hours, minutes and seconds,
                      // in absolute value.
        String result= ""; // The string to be returned
        
        // Fix local var amPM --depend on display12Hr but also whether time
        // is in the current day ( 0 <= time < 24*60*60)
        boolean amPM= display12Hr;
        if (time < 0 || time >= 24*60*60)
            amPM= false;
        
        // Compute sec, min, hr
        int timeabs= Math.abs(time);
        hr= timeabs / (60*60);
        System.out.println("hr: " + hr);
        min= (timeabs - hr*60*60) / 60;
        System.out.println("min: " + min);
        sec= (timeabs - hr*60*60) % 60;
        System.out.println("sec: " + sec);
        
        // Append '-' to result if needed
        if (time < 0)
            result= result + "-";
        // Append hr to result
        result=  result +
            prepend0(amPM && hr>= 12 ? hr-12: hr);
        
        // Append ':' and min to result
        result=  result + ":" + prepend0(min);
        
        // Append ':' and sec to result
        result=  result + ":" + prepend0(sec);
       
        
        // Append 'AM-PM' to result indication if needed
        if (amPM) 
            result= result + (hr < 12 ? "AM" : "PM");
        
        // Append blank and zone to result
        
        return result;
    }
    
    /** =  int i as a String with 2 chars.
      Precondition: 0 <= i < 100. */
    private static String prepend0(int i) {
        if (i >= 10)
            return "" + i;
        return "0" + i;
    }
    
    /** r is not null and this rhino is older than r*/
    public static boolean isOlder(Time t) {
        return false;
    }
    
    /** r1 is not null and r2 is not null and
        r1 is older than r2*/
    public static boolean isOlder(Time r1, Time r2) {
        return r1 != null  &&  r1.isOlder(r2);
    }
    
}