earth_time.h

Go to the documentation of this file.
00001 #ifndef EARTH_TIME_GROUP
00002 #define EARTH_TIME_GROUP
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : earth_time                                                        *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1999-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "packable.h"
00019 
00021 
00026 namespace earth_time {
00027 
00028   enum days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY };
00030 
00031   days day_now();  
00032 
00033   const char *day_name(days to_name);
00035 
00036   enum months { JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST,
00037       SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER };
00039 
00040   months month_now();  
00041 
00042   const char *month_name(months to_name);
00044   const char *short_month_name(months to_name);
00046 
00047   extern const int days_in_month[12];
00049   extern const int leap_days_in_month[12];
00051 
00052   extern const int julian_days_in_month[12];
00054   extern const int julian_leap_days_in_month[12];
00056 
00057   const int SECONDS_IN_MINUTE = 60;  
00058   const int MINUTES_IN_HOUR = 60;  
00059   const int HOURS_IN_DAY = 24;  
00060   const int DAYS_IN_YEAR = 365;  
00061   const int LEAP_DAYS_IN_YEAR = 366;  
00062   const double APPROX_DAYS_IN_YEAR = 365.2424;
00064 
00067 
00068   enum time_zones {
00069     LOCAL_ZONE,      
00070     GREENWICH_ZONE   
00071   };
00072 
00073   // now some structures for representing time...
00074 
00076   class clock_time : public packable
00077   {
00078   public:
00079     int hour;  
00080     int minute;  
00081     int second;  
00082     int millisecond;  
00083     int microsecond;  
00084 
00086     clock_time(int h = 0, int m = 0, int s = 0, int ms = 0, int us = 0)
00087              : hour(h), minute(m), second(s), millisecond(ms),
00088                microsecond(us) {}
00089 
00090     virtual void pack(byte_array &packed_form) const;
00092     virtual bool unpack(byte_array &packed_form);
00094 
00095     bool operator < (const clock_time &to_compare) const;
00097     bool operator == (const clock_time &to_compare) const;
00099 
00101     enum time_formats {
00102       MERIDIAN = 0x1,  
00103       MILITARY = 0x2,  
00104       NO_AM_PM = 0x4,  
00105       SECONDS = 0x8,   
00106       MILLISECONDS = 0x10  
00107     };
00108 
00109     istring text_form(int how = MERIDIAN) const;
00111 
00112     void text_form(istring &to_stuff, int how = MERIDIAN) const;
00114 
00117     static int normalize(clock_time &to_fix);
00118       // ensures that the units in each field are in the proper range by
00119       // promoting them upwards.  if the clock_time goes above the maximum hour,
00120       // then it rolls around.  zero is returned for no rollover or a positive
00121       // integer is returned for the number of rollovers that occurred.  if
00122       // there are any negative fields, they are rolled backwards.
00123       // the returned rollovers are measured in days.
00124   };
00125 
00127   class day_in_year : public packable
00128   {
00129   public:
00130     months month;  
00131     int day_in_month;  
00132     days day_of_week;  
00133     int day_of_year;  
00134 
00136     day_in_year(months m = JANUARY, int dim = 1, days dow = SUNDAY,
00137             int day_o_year = 1) : month(m), day_in_month(dim),
00138             day_of_week(dow), day_of_year(day_o_year) {}
00139 
00140     virtual void pack(byte_array &packed_form) const;
00142     virtual bool unpack(byte_array &packed_form);
00144 
00145     bool operator < (const day_in_year &to_compare) const;
00147 
00148     bool operator == (const day_in_year &to_compare) const;
00150 
00152 
00153     enum date_formats {
00154       // note: these classes may need to be revised in the year 9999.
00155       SHORT_MONTH = 0x1,    
00156       LONG_MONTH = 0x2,     
00157       INCLUDE_DAY = 0x4     
00158     };
00159 
00160     istring text_form(int how = SHORT_MONTH) const;
00162     void text_form(istring &to_stuff, int how = SHORT_MONTH) const;
00164 
00165     static int normalize(day_in_year &to_fix, bool leap_year = false);
00167 
00169   };
00170 
00172 
00173   class time_locus : public clock_time, public day_in_year
00174   {
00175   public:
00176     int year;  
00177 
00179     time_locus(const clock_time &ct = clock_time(),
00180             const day_in_year &ytd = day_in_year(), int year_in = 0)
00181             : clock_time(ct), day_in_year(ytd), year(year_in) {}
00182 
00183     virtual void pack(byte_array &packed_form) const;
00185     virtual bool unpack(byte_array &packed_form);
00187 
00188     bool operator < (const time_locus &to_compare) const;
00190     bool operator == (const time_locus &to_compare) const;
00192 
00194     enum locus_formats {
00195       LONG_YEAR = 0x1,  
00196       SHORT_YEAR = 0x2  
00197     };
00198 
00199     istring text_form(int t = clock_time::MERIDIAN,
00200             int d = day_in_year::SHORT_MONTH, int y = LONG_YEAR) const;
00202       /*< "t" is a combination of time_formats, "d" is a combination of
00203       date_formats and "y" is a combination of locus_formats. */
00204     void text_form(istring &to_stuff, int t = clock_time::MERIDIAN,
00205             int d = day_in_year::SHORT_MONTH, int y = LONG_YEAR) const;
00207 
00208     static int normalize(time_locus &to_fix);
00210 //hmmm: what are rollovers measured in?
00211   };
00212 
00213   int year_now();  
00214   clock_time time_now();  
00215   day_in_year date_now();  
00216   time_locus now();  
00217   time_locus greenwich_now();  
00218 
00219   bool set_time(const time_locus &new_time);
00221 
00224 } // namespace.
00225 
00226 #endif
00227 

Generated on Fri Oct 10 04:28:41 2008 for HOOPLE Libraries by  doxygen 1.5.1