earth_time.h

Go to the documentation of this file.
00001 #ifndef EARTH_TIME_GROUP
00002 #define EARTH_TIME_GROUP
00003 
00004 // Name   : earth_time
00005 // Author : Chris Koeritz
00006 /******************************************************************************
00007 * Copyright (c) 1999-$now By Author.  This program is free software; you can  *
00008 * redistribute it and/or modify it under the terms of the GNU General Public  *
00009 * License as published by the Free Software Foundation; either version 2 of   *
00010 * the License or (at your option) any later version.  This is online at:      *
00011 *     http://www.fsf.org/copyleft/gpl.html                                    *
00012 * Please send any updates to: fred@gruntose.com                               *
00013 \*****************************************************************************/
00014 
00015 #include <basis/astring.h>
00016 #include <basis/byte_array.h>
00017 #include <basis/contracts.h>
00018 #include <structures/object_packers.h>
00019 
00021 
00026 namespace timely {
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 virtual basis::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     ~clock_time() {}
00090 
00091     int packed_size() const { return 5 * structures::PACKED_SIZE_INT32; }
00092 
00093     virtual void pack(basis::byte_array &packed_form) const;
00095     virtual bool unpack(basis::byte_array &packed_form);
00097 
00098     bool operator < (const clock_time &to_compare) const;
00100     bool operator == (const clock_time &to_compare) const;
00102 
00104     enum time_formats {
00105       MERIDIAN = 0x1,  
00106       MILITARY = 0x2,  
00107       NO_AM_PM = 0x4,  
00108       SECONDS = 0x8,   
00109       MILLISECONDS = 0x10  
00110     };
00111 
00112     basis::astring text_form(int how = MERIDIAN) const;
00114 
00115     void text_form(basis::astring &to_stuff, int how = MERIDIAN) const;
00117 
00120     static int normalize(clock_time &to_fix);
00121       // ensures that the units in each field are in the proper range by
00122       // promoting them upwards.  if the clock_time goes above the maximum hour,
00123       // then it rolls around.  zero is returned for no rollover or a positive
00124       // integer is returned for the number of rollovers that occurred.  if
00125       // there are any negative fields, they are rolled backwards.
00126       // the returned rollovers are measured in days.
00127   };
00128 
00130   class day_in_year : public virtual basis::packable
00131   {
00132   public:
00133     months month;  
00134     int day_in_month;  
00135     days day_of_week;  
00136     int day_of_year;  
00137 
00138     int packed_size() const { return 4 * structures::PACKED_SIZE_INT32; }
00139 
00141     day_in_year(months m = JANUARY, int dim = 1, days dow = SUNDAY,
00142             int day_o_year = 1) : month(m), day_in_month(dim),
00143             day_of_week(dow), day_of_year(day_o_year) {}
00144 
00145     virtual void pack(basis::byte_array &packed_form) const;
00147     virtual bool unpack(basis::byte_array &packed_form);
00149 
00150     bool operator < (const day_in_year &to_compare) const;
00152 
00153     bool operator == (const day_in_year &to_compare) const;
00155 
00157 
00158     enum date_formats {
00159       // note: these classes may need to be revised in the year 9999.
00160       SHORT_MONTH = 0x1,    
00161       LONG_MONTH = 0x2,     
00162       INCLUDE_DAY = 0x4     
00163     };
00164 
00165     basis::astring text_form(int how = SHORT_MONTH) const;
00167     void text_form(basis::astring &to_stuff, int how = SHORT_MONTH) const;
00169 
00170     static int normalize(day_in_year &to_fix, bool leap_year = false);
00172 
00174   };
00175 
00177 
00178   class time_locus : public clock_time, public day_in_year,
00179       public virtual basis::hoople_standard
00180   {
00181   public:
00182     int year;  
00183 
00184     time_locus() : clock_time(), day_in_year(), year() {}
00185 
00186     DEFINE_CLASS_NAME("time_locus");
00187 
00189     time_locus(const clock_time &ct, const day_in_year &ytd, int year_in)
00190             : clock_time(ct), day_in_year(ytd), year(year_in) {}
00191 
00192     int packed_size() const { return clock_time::packed_size()
00193             + day_in_year::packed_size() + structures::PACKED_SIZE_INT32; }
00194 
00195     virtual void pack(basis::byte_array &packed_form) const;
00197     virtual bool unpack(basis::byte_array &packed_form);
00199 
00200     // these implement the orderable and equalizable interfaces.
00201     virtual bool equal_to(const basis::equalizable &s2) const;
00202     virtual bool less_than(const basis::orderable &s2) const;
00203 //old    bool operator < (const time_locus &to_compare) const;
00205 //old    bool operator == (const time_locus &to_compare) const;
00207 
00209     enum locus_formats {
00210       LONG_YEAR = 0x1,  
00211       SHORT_YEAR = 0x2  
00212     };
00213 
00214     // fulfills obligation for text_formable.
00215     virtual void text_form(basis::base_string &state_fill) const {
00216       state_fill.assign(text_form_long(clock_time::MERIDIAN, day_in_year::SHORT_MONTH, LONG_YEAR));
00217     }
00218 
00219     basis::astring text_form_long(int t = clock_time::MERIDIAN,
00220             int d = day_in_year::SHORT_MONTH, int y = LONG_YEAR) const;
00222       /*< "t" is a combination of time_formats, "d" is a combination of
00223       date_formats and "y" is a combination of locus_formats. */
00224     void text_form_long(basis::astring &to_stuff, int t = clock_time::MERIDIAN,
00225             int d = day_in_year::SHORT_MONTH, int y = LONG_YEAR) const;
00227 
00228     static int normalize(time_locus &to_fix);
00230 //hmmm: what are rollovers measured in?
00231   };
00232 
00233   int year_now();  
00234   clock_time time_now();  
00235   day_in_year date_now();  
00236   time_locus now();  
00237   time_locus greenwich_now();  
00238 } // namespace.
00239 
00240 #endif
00241 
Generated on Sat Jan 28 04:22:33 2012 for hoople2 project by  doxygen 1.6.3