feisty meow concerns codebase  2.140
earth_time.h
Go to the documentation of this file.
1 #ifndef EARTH_TIME_GROUP
2 #define EARTH_TIME_GROUP
3 
4 /*
5  Name : earth_time
6  Author : Chris Koeritz
7 
8  Copyright (c) 1999-$now By Author. This program is free software; you can
9  redistribute it and/or modify it under the terms of the GNU General Public
10  License as published by the Free Software Foundation; either version 2 of
11  the License or (at your option) any later version. This is online at:
12  http://www.fsf.org/copyleft/gpl.html
13  Please send any updates to: fred@gruntose.com
14 */
15 
16 #include <basis/astring.h>
17 #include <basis/byte_array.h>
18 #include <basis/contracts.h>
19 //#include <basis/definitions.h>
21 
23 
25 
29 namespace timely {
30 
32 
35 
36  days day_now();
37 
38  const char *day_name(days to_name);
40 
44 
45  months month_now();
46 
47  const char *month_name(months to_name);
49  const char *short_month_name(months to_name);
51 
52  extern const time_number days_in_month[12];
54  extern const time_number leap_days_in_month[12];
56 
57  extern const time_number julian_days_in_month[12];
59  extern const time_number julian_leap_days_in_month[12];
61 
66  const time_number MINUTES_IN_DAY = 1440;
67  const time_number SECONDS_IN_DAY = 86400;
68  const time_number DAYS_IN_YEAR = 365;
70  const double APPROX_DAYS_IN_YEAR = 365.2424;
72 
76  enum time_zones {
79  };
80 
81  // now some structures for representing time...
82 
84  class clock_time : public virtual basis::packable
85  {
86  public:
92 
95  : hour(h), minute(m), second(s), millisecond(ms),
96  microsecond(us) {}
98 
99  int packed_size() const { return 5 * structures::PACKED_SIZE_INT32; }
100 
101  virtual void pack(basis::byte_array &packed_form) const;
103  virtual bool unpack(basis::byte_array &packed_form);
105 
106  bool operator < (const clock_time &to_compare) const;
108  bool operator == (const clock_time &to_compare) const;
110 
113  MERIDIAN = 0x1,
114  MILITARY = 0x2,
115  NO_AM_PM = 0x4,
116  SECONDS = 0x8,
117  MILLISECONDS = 0x10
118  };
119 
120  basis::astring text_form(int how = MERIDIAN) const;
122 
123  void text_form(basis::astring &to_stuff, int how = MERIDIAN) const;
125 
128  static time_number normalize(clock_time &to_fix);
129  // ensures that the units in each field are in the proper range by
130  // promoting them upwards. if the clock_time goes above the maximum hour,
131  // then it rolls around. zero is returned for no rollover or a positive
132  // integer is returned for the number of rollovers that occurred. if
133  // there are any negative fields, they are rolled backwards.
134  // the returned rollovers are measured in days.
135  };
136 
138  class day_in_year : public virtual basis::packable
139  {
140  public:
145 
146  int packed_size() const { return 4 * structures::PACKED_SIZE_INT64; }
147 
150  time_number day_o_year = 1) : month(m), day_in_month(dim),
151  day_of_week(dow), day_of_year(day_o_year) {}
152 
153  virtual void pack(basis::byte_array &packed_form) const;
155  virtual bool unpack(basis::byte_array &packed_form);
157 
158  bool operator < (const day_in_year &to_compare) const;
160 
161  bool operator == (const day_in_year &to_compare) const;
163 
167  // note: these classes may need to be revised in the year 9999.
168  SHORT_MONTH = 0x1,
169  LONG_MONTH = 0x2,
170  INCLUDE_DAY = 0x4
171  };
172 
173  basis::astring text_form(int how = SHORT_MONTH) const;
175  void text_form(basis::astring &to_stuff, int how = SHORT_MONTH) const;
177 
178  static time_number normalize(day_in_year &to_fix, bool leap_year = false);
180 
182  };
183 
185 
186  class time_locus : public clock_time, public day_in_year,
187  public virtual basis::hoople_standard
188  {
189  public:
191 
193 
194  DEFINE_CLASS_NAME("time_locus");
195 
197  time_locus(const clock_time &ct, const day_in_year &ytd, time_number year_in)
198  : clock_time(ct), day_in_year(ytd), year(year_in) {}
199 
200  int packed_size() const { return clock_time::packed_size()
202 
203  virtual void pack(basis::byte_array &packed_form) const;
205  virtual bool unpack(basis::byte_array &packed_form);
207 
208  // these implement the orderable and equalizable interfaces.
209  virtual bool equal_to(const basis::equalizable &s2) const;
210  virtual bool less_than(const basis::orderable &s2) const;
211 //old bool operator < (const time_locus &to_compare) const;
213 //old bool operator == (const time_locus &to_compare) const;
215 
218  LONG_YEAR = 0x1,
219  SHORT_YEAR = 0x2
220  };
221 
222  // fulfills obligation for text_formable.
223  virtual void text_form(basis::base_string &state_fill) const {
225  }
226 
228  int d = day_in_year::SHORT_MONTH, int y = LONG_YEAR) const;
230  /*< "t" is a combination of time_formats, "d" is a combination of
231  date_formats and "y" is a combination of locus_formats. */
232  void text_form_long(basis::astring &to_stuff, int t = clock_time::MERIDIAN,
233  int d = day_in_year::SHORT_MONTH, int y = LONG_YEAR) const;
235 
236  static time_number normalize(time_locus &to_fix);
238 //hmmm: what are rollovers measured in?
239  };
240 
242  clock_time time_now();
243  day_in_year date_now();
244  time_locus now();
245  time_locus greenwich_now();
246 } // namespace.
247 
248 #endif
249 
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
Defines the base class for all string processing objects in hoople.
Definition: base_string.h:28
virtual base_string & assign(const base_string &s)=0
Sets the contents of this string to "s".
A very common template for a dynamic array of bytes.
Definition: byte_array.h:36
Base class for object that can tell itself apart from other instances.
Definition: contracts.h:44
the base class of the most easily used and tested objects in the library.
Definition: contracts.h:161
A base for objects that can be alphabetically (lexicographically) ordered.
Definition: contracts.h:57
A base class for objects that can pack into an array of bytes.
Definition: byte_array.h:87
A specific point in time as represented by a 24 hour clock.
Definition: earth_time.h:85
bool operator<(const clock_time &to_compare) const
Returns true if this clock_time is earlier than "to_compare".
Definition: earth_time.cpp:86
clock_time(time_number h=0, time_number m=0, time_number s=0, time_number ms=0, time_number us=0)
Constructs a clock_time object given all the parts.
Definition: earth_time.h:94
time_number minute
The number of minutes after the hour.
Definition: earth_time.h:88
basis::astring text_form(int how=MERIDIAN) const
Prints the clock_time according to "how".
Definition: earth_time.cpp:105
static time_number normalize(clock_time &to_fix)
Definition: earth_time.cpp:144
time_formats
An enumeration of time formatting modes used when printing the time.
Definition: earth_time.h:112
@ NO_AM_PM
use 12 hour time but don't include AM/PM.
Definition: earth_time.h:115
@ MERIDIAN
default: uses 12 hour with AM/PM and no seconds.
Definition: earth_time.h:113
@ MILITARY
use military 24 hour time.
Definition: earth_time.h:114
@ MILLISECONDS
milliseconds are fourth field (after secs).
Definition: earth_time.h:117
@ SECONDS
include the number of seconds as a third field.
Definition: earth_time.h:116
virtual bool unpack(basis::byte_array &packed_form)
Unpacks a clock time from an array of bytes.
Definition: earth_time.cpp:72
virtual void pack(basis::byte_array &packed_form) const
Packs a clock time into an array of bytes.
Definition: earth_time.cpp:63
bool operator==(const clock_time &to_compare) const
Returns true if this clock_time is equal to "to_compare".
Definition: earth_time.cpp:96
time_number second
The number of seconds after the current minute.
Definition: earth_time.h:89
time_number microsecond
Number of microseconds elapsed in this millisecond.
Definition: earth_time.h:91
time_number hour
The hour represented in military time: 0 through 23.
Definition: earth_time.h:87
time_number millisecond
The number of milliseconds elapsed in this second.
Definition: earth_time.h:90
int packed_size() const
Estimates the space needed for the packed structure.
Definition: earth_time.h:99
An object that represents a particular day in a year.
Definition: earth_time.h:139
bool operator==(const day_in_year &to_compare) const
Returns true if this day is equal to "to_compare".
Definition: earth_time.cpp:191
time_number day_of_year
Numerical day, where January 1st is equal to zero.
Definition: earth_time.h:144
date_formats
An enumeration of ways to print out the current date.
Definition: earth_time.h:166
@ LONG_MONTH
uses full month name.
Definition: earth_time.h:169
@ INCLUDE_DAY
adds the name of the day.
Definition: earth_time.h:170
@ SHORT_MONTH
default: three letter month.
Definition: earth_time.h:168
time_number day_in_month
The day number within the month (starting at one).
Definition: earth_time.h:142
basis::astring text_form(int how=SHORT_MONTH) const
Prints the day according to "how".
Definition: earth_time.cpp:197
days day_of_week
The day of the week.
Definition: earth_time.h:143
virtual void pack(basis::byte_array &packed_form) const
Packs a day object into an array of bytes.
Definition: earth_time.cpp:161
static time_number normalize(day_in_year &to_fix, bool leap_year=false)
normalizes the day as needed and returns the adjustment in years.
Definition: earth_time.cpp:229
months month
The current month.
Definition: earth_time.h:141
day_in_year(months m=JANUARY, time_number dim=1, days dow=SUNDAY, time_number day_o_year=1)
Constructs a representation of the day specified.
Definition: earth_time.h:149
virtual bool unpack(basis::byte_array &packed_form)
Unpacks a day object from an array of bytes.
Definition: earth_time.cpp:171
int packed_size() const
Estimates the space needed for the packed structure.
Definition: earth_time.h:146
bool operator<(const day_in_year &to_compare) const
Returns true if this day is earlier than "to_compare".
Definition: earth_time.cpp:184
An object that represents a particular point in time.
Definition: earth_time.h:188
virtual bool less_than(const basis::orderable &s2) const
Definition: earth_time.cpp:277
static time_number normalize(time_locus &to_fix)
Same as text_form() above, but stores into "to_stuff".
Definition: earth_time.cpp:307
locus_formats
< Returns true if this time_locus is earlier than "to_compare"
Definition: earth_time.h:217
@ SHORT_YEAR
use only last two digits of year. ugh–Y2K danger.
Definition: earth_time.h:219
@ LONG_YEAR
default: full four digit year (problems in 9999).
Definition: earth_time.h:218
virtual void pack(basis::byte_array &packed_form) const
Packs a time_locus object into an array of bytes.
Definition: earth_time.cpp:247
basis::astring text_form_long(int t=clock_time::MERIDIAN, int d=day_in_year::SHORT_MONTH, int y=LONG_YEAR) const
Definition: earth_time.cpp:262
time_number year
The year, using the gregorian calendar.
Definition: earth_time.h:190
int packed_size() const
Estimates the space needed for the packed structure.
Definition: earth_time.h:200
time_locus(const clock_time &ct, const day_in_year &ytd, time_number year_in)
Constructs a location in time given its components.
Definition: earth_time.h:197
virtual void text_form(basis::base_string &state_fill) const
Provides a text view of all the important info owned by this object.
Definition: earth_time.h:223
virtual bool equal_to(const basis::equalizable &s2) const
Definition: earth_time.cpp:269
virtual bool unpack(basis::byte_array &packed_form)
Unpacks a time_locus object from an array of bytes.
Definition: earth_time.cpp:254
DEFINE_CLASS_NAME("time_locus")
long int signed_long
Abbreviated name for signed long integers.
Definition: definitions.h:68
const int PACKED_SIZE_INT64
const int PACKED_SIZE_INT32
#include <time.h>
Definition: earth_time.cpp:37
const char * month_name(months to_name)
Returns the name of the month "to_name".
Definition: earth_time.cpp:410
time_zones
An enumeration of time zones, both relative and absolute.
Definition: earth_time.h:76
@ GREENWICH_ZONE
The time zone of Greenwich Mean Time.
Definition: earth_time.h:78
@ LOCAL_ZONE
The time zone this computer is configured to report.
Definition: earth_time.h:77
basis::signed_long time_number
Definition: earth_time.h:31
time_locus greenwich_now()
returns Greenwich Mean Time (their now).
Definition: earth_time.cpp:369
time_locus now()
returns our current locus in the time continuum.
Definition: earth_time.cpp:352
@ SATURDAY
Definition: earth_time.h:33
@ WEDNESDAY
Definition: earth_time.h:33
@ MONDAY
Definition: earth_time.h:33
@ FRIDAY
Definition: earth_time.h:33
@ THURSDAY
Definition: earth_time.h:33
@ TUESDAY
Definition: earth_time.h:33
@ SUNDAY
Definition: earth_time.h:33
const char * day_name(days to_name)
Returns the name of the day "to_name".
Definition: earth_time.cpp:396
const time_number DAYS_IN_YEAR
Number of days in a standard year.
Definition: earth_time.h:68
const time_number MINUTES_IN_HOUR
Number of minutes in an hour.
Definition: earth_time.h:63
const time_number SECONDS_IN_DAY
Number of seconds in a day.
Definition: earth_time.h:67
@ DECEMBER
Definition: earth_time.h:42
@ OCTOBER
Definition: earth_time.h:42
@ SEPTEMBER
Definition: earth_time.h:42
@ JANUARY
Definition: earth_time.h:41
@ NOVEMBER
Definition: earth_time.h:42
@ FEBRUARY
Definition: earth_time.h:41
@ APRIL
Definition: earth_time.h:41
@ AUGUST
Definition: earth_time.h:41
@ JUNE
Definition: earth_time.h:41
@ JULY
Definition: earth_time.h:41
@ MARCH
Definition: earth_time.h:41
time_number year_now()
what year is it?
Definition: earth_time.cpp:392
const double APPROX_DAYS_IN_YEAR
A more accurate measure of the number of days in a year.
Definition: earth_time.h:70
const time_number SECONDS_IN_HOUR
Number of seconds in an hour.
Definition: earth_time.h:64
const time_number leap_days_in_month[12]
The number of days in each month in a leap year.
Definition: earth_time.cpp:52
const time_number julian_days_in_month[12]
Number of days in each month based on the julian calendar.
Definition: earth_time.cpp:55
clock_time time_now()
what time is it?
Definition: earth_time.cpp:386
days day_now()
Returns the current local day.
Definition: earth_time.cpp:388
const time_number HOURS_IN_DAY
Number of hours in a day.
Definition: earth_time.h:65
const char * short_month_name(months to_name)
Returns a shorter, constant-length (3 characters) month name.
Definition: earth_time.cpp:429
const time_number SECONDS_IN_MINUTE
Number of seconds in one minute.
Definition: earth_time.h:62
const time_number MINUTES_IN_DAY
Number of minutes in a day.
Definition: earth_time.h:66
const time_number LEAP_DAYS_IN_YEAR
Number of days in a leap year.
Definition: earth_time.h:69
day_in_year date_now()
what day on the calendar is it?
Definition: earth_time.cpp:394
const time_number julian_leap_days_in_month[12]
Number of days in each month of a leap year in the julian calendar.
Definition: earth_time.cpp:59
const time_number days_in_month[12]
The number of days in each month in the standard year.
Definition: earth_time.cpp:49
months month_now()
returns the local month.
Definition: earth_time.cpp:390