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
29namespace timely {
30
32
35
36 days day_now();
37
38 const char *day_name(days to_name);
40
44
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];
61
70 const double APPROX_DAYS_IN_YEAR = 365.2424;
72
80
81 // now some structures for representing time...
82
84 class clock_time : public virtual basis::packable
85 {
86 public:
92
93 DEFINE_CLASS_NAME("clock_time");
94
97 : hour(h), minute(m), second(s), millisecond(ms),
98 microsecond(us) {}
100
101 int packed_size() const { return 5 * structures::PACKED_SIZE_INT32; }
102
103 virtual void pack(basis::byte_array &packed_form) const;
105 virtual bool unpack(basis::byte_array &packed_form);
107
108 bool operator < (const clock_time &to_compare) const;
110 bool operator == (const clock_time &to_compare) const;
112
115 MERIDIAN = 0x1,
116 MILITARY = 0x2,
117 NO_AM_PM = 0x4,
118 SECONDS = 0x8,
119 MILLISECONDS = 0x10
120 };
121
122 basis::astring text_form(int how = MERIDIAN) const;
124
125 void text_form(basis::astring &to_stuff, int how = MERIDIAN) const;
127
130 static time_number normalize(clock_time &to_fix);
131 // ensures that the units in each field are in the proper range by
132 // promoting them upwards. if the clock_time goes above the maximum hour,
133 // then it rolls around. zero is returned for no rollover or a positive
134 // integer is returned for the number of rollovers that occurred. if
135 // there are any negative fields, they are rolled backwards.
136 // the returned rollovers are measured in days.
137 };
138
140 class day_in_year : public virtual basis::packable
141 {
142 public:
147
148 DEFINE_CLASS_NAME("day_in_year");
149
150 int packed_size() const { return 4 * structures::PACKED_SIZE_INT64; }
151
154 time_number day_o_year = 1) : month(m), day_in_month(dim),
155 day_of_week(dow), day_of_year(day_o_year) {}
156
157 virtual void pack(basis::byte_array &packed_form) const;
159 virtual bool unpack(basis::byte_array &packed_form);
161
162 bool operator < (const day_in_year &to_compare) const;
164
165 bool operator == (const day_in_year &to_compare) const;
167
171 // note: these classes may need to be revised in the year 9999.
174 INCLUDE_DAY = 0x4
175 };
176
177 basis::astring text_form(int how = SHORT_MONTH) const;
179 void text_form(basis::astring &to_stuff, int how = SHORT_MONTH) const;
181
182 static time_number normalize(day_in_year &to_fix, bool leap_year = false);
184
186 };
187
189
190 class time_locus : public clock_time, public day_in_year,
191 public virtual basis::hoople_standard
192 {
193 public:
195
197
198 DEFINE_CLASS_NAME("time_locus");
199
201 time_locus(const clock_time &ct, const day_in_year &ytd, time_number year_in)
202 : clock_time(ct), day_in_year(ytd), year(year_in) {}
203
206
207 virtual void pack(basis::byte_array &packed_form) const;
209 virtual bool unpack(basis::byte_array &packed_form);
211
212 // these implement the orderable and equalizable interfaces.
213 virtual bool equal_to(const basis::equalizable &s2) const;
214 virtual bool less_than(const basis::orderable &s2) const;
215//old bool operator < (const time_locus &to_compare) const;
217//old bool operator == (const time_locus &to_compare) const;
219
222 LONG_YEAR = 0x1,
223 SHORT_YEAR = 0x2
224 };
225
226 // fulfills obligation for text_formable.
230
232 int d = day_in_year::SHORT_MONTH, int y = LONG_YEAR) const;
234 /*< "t" is a combination of time_formats, "d" is a combination of
235 date_formats and "y" is a combination of locus_formats. */
236 void text_form_long(basis::astring &to_stuff, int t = clock_time::MERIDIAN,
237 int d = day_in_year::SHORT_MONTH, int y = LONG_YEAR) const;
239
240 static time_number normalize(time_locus &to_fix);
242//hmmm: what are rollovers measured in?
243 };
244
246 clock_time time_now();
247 day_in_year date_now();
248 time_locus now();
249 time_locus greenwich_now();
250} // namespace.
251
252#endif
253
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".
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:96
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".
static time_number normalize(clock_time &to_fix)
time_formats
An enumeration of time formatting modes used when printing the time.
Definition earth_time.h:114
@ NO_AM_PM
use 12 hour time but don't include AM/PM.
Definition earth_time.h:117
@ MERIDIAN
default: uses 12 hour with AM/PM and no seconds.
Definition earth_time.h:115
@ MILITARY
use military 24 hour time.
Definition earth_time.h:116
@ MILLISECONDS
milliseconds are fourth field (after secs).
Definition earth_time.h:119
@ SECONDS
include the number of seconds as a third field.
Definition earth_time.h:118
virtual bool unpack(basis::byte_array &packed_form)
Unpacks a clock time from an array of bytes.
virtual void pack(basis::byte_array &packed_form) const
Packs a clock time into an array of bytes.
bool operator==(const clock_time &to_compare) const
Returns true if this clock_time is equal to "to_compare".
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
DEFINE_CLASS_NAME("clock_time")
int packed_size() const
Estimates the space needed for the packed structure.
Definition earth_time.h:101
An object that represents a particular day in a year.
Definition earth_time.h:141
bool operator==(const day_in_year &to_compare) const
Returns true if this day is equal to "to_compare".
time_number day_of_year
Numerical day, where January 1st is equal to zero.
Definition earth_time.h:146
date_formats
An enumeration of ways to print out the current date.
Definition earth_time.h:170
@ LONG_MONTH
uses full month name.
Definition earth_time.h:173
@ INCLUDE_DAY
adds the name of the day.
Definition earth_time.h:174
@ SHORT_MONTH
default: three letter month.
Definition earth_time.h:172
time_number day_in_month
The day number within the month (starting at one).
Definition earth_time.h:144
basis::astring text_form(int how=SHORT_MONTH) const
Prints the day according to "how".
days day_of_week
The day of the week.
Definition earth_time.h:145
virtual void pack(basis::byte_array &packed_form) const
Packs a day object into an array of bytes.
static time_number normalize(day_in_year &to_fix, bool leap_year=false)
normalizes the day as needed and returns the adjustment in years.
months month
The current month.
Definition earth_time.h:143
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:153
virtual bool unpack(basis::byte_array &packed_form)
Unpacks a day object from an array of bytes.
int packed_size() const
Estimates the space needed for the packed structure.
Definition earth_time.h:150
bool operator<(const day_in_year &to_compare) const
Returns true if this day is earlier than "to_compare".
DEFINE_CLASS_NAME("day_in_year")
An object that represents a particular point in time.
Definition earth_time.h:192
virtual bool less_than(const basis::orderable &s2) const
static time_number normalize(time_locus &to_fix)
Same as text_form() above, but stores into "to_stuff".
locus_formats
< Returns true if this time_locus is earlier than "to_compare"
Definition earth_time.h:221
@ SHORT_YEAR
use only last two digits of year. ugh–Y2K danger.
Definition earth_time.h:223
@ LONG_YEAR
default: full four digit year (problems in 9999).
Definition earth_time.h:222
virtual void pack(basis::byte_array &packed_form) const
Packs a time_locus object into an array of bytes.
basis::astring text_form_long(int t=clock_time::MERIDIAN, int d=day_in_year::SHORT_MONTH, int y=LONG_YEAR) const
time_number year
The year, using the gregorian calendar.
Definition earth_time.h:194
int packed_size() const
Estimates the space needed for the packed structure.
Definition earth_time.h:204
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:201
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:227
virtual bool equal_to(const basis::equalizable &s2) const
virtual bool unpack(basis::byte_array &packed_form)
Unpacks a time_locus object from an array of bytes.
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>
const char * month_name(months to_name)
Returns the name of the month "to_name".
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).
time_locus now()
returns our current locus in the time continuum.
@ SATURDAY
Definition earth_time.h:33
@ WEDNESDAY
Definition earth_time.h:33
@ THURSDAY
Definition earth_time.h:33
@ TUESDAY
Definition earth_time.h:33
const char * day_name(days to_name)
Returns the name of the day "to_name".
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
time_number year_now()
what year is it?
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.
const time_number julian_days_in_month[12]
Number of days in each month based on the julian calendar.
clock_time time_now()
what time is it?
days day_now()
Returns the current local day.
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.
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?
const time_number julian_leap_days_in_month[12]
Number of days in each month of a leap year in the julian calendar.
const time_number days_in_month[12]
The number of days in each month in the standard year.
months month_now()
returns the local month.