24 #if defined(__WIN32__) || defined(__UNIX__)
33 using namespace basis;
40 #ifdef DEBUG_EARTH_TIME
41 #define LOG(tpr) printf("%s", (astring("earth_time::") + func + ": " + tpr + parser_bits::platform_eol_to_chars()).s())
49 = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
52 = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
55 = { 31, 29, 31, 30, 31, 30, 31, 30, 31, 30, 31, 30 };
59 = { 31, 30, 31, 30, 31, 30, 31, 30, 31, 30, 31, 30 };
66 attach(packed_form, minute);
67 attach(packed_form, second);
68 attach(packed_form, millisecond);
69 attach(packed_form, microsecond);
74 if (!
detach(packed_form, hour))
return false;
75 if (!
detach(packed_form, minute))
return false;
76 if (!
detach(packed_form, second))
return false;
77 if (!
detach(packed_form, millisecond))
return false;
78 if (!
detach(packed_form, microsecond))
return false;
82 #define EASY_LT(x, y) \
83 if (x < y) return true; \
84 if (x > y) return false
86 bool clock_time::operator < (
const clock_time &to_compare)
const
96 bool clock_time::operator == (
const clock_time &to_compare)
const
98 return (hour == to_compare.
hour)
99 && (minute == to_compare.
minute)
100 && (second == to_compare.
second)
108 text_form(to_return, how);
112 void clock_time::text_form(
astring &to_return,
int how)
const
116 to_return +=
a_sprintf(
"%02ld:%02ld", hour, minute);
119 if (uhr > 12) uhr -= 12;
120 to_return +=
a_sprintf(
"%2ld:%02ld", uhr, minute);
122 if ( (how & SECONDS) || (how & MILLISECONDS) )
123 to_return +=
a_sprintf(
":%02ld", second);
124 if (how & MILLISECONDS)
125 to_return +=
a_sprintf(
":%03ld", millisecond);
126 if (how & MERIDIAN) {
127 if (hour >= 12) to_return +=
"PM";
128 else to_return +=
"AM";
134 #define limit_value(val, max) \
138 val += max * -rolls; \
139 } else if (val >= max) { \
141 val -= max * rolls; \
142 } else { rolls = 0; }
154 to_fix.
hour += rolls;
163 attach(packed_form, day_of_year);
166 attach(packed_form, day_in_month);
173 if (!
detach(packed_form, day_of_year))
return false;
175 if (!
detach(packed_form, temp))
return false;
176 day_of_week =
days(temp);
177 if (!
detach(packed_form, temp))
return false;
179 if (!
detach(packed_form, day_in_month))
return false;
180 if (!
detach(packed_form, temp))
return false;
184 bool day_in_year::operator < (
const day_in_year &to_compare)
const
191 bool day_in_year::operator == (
const day_in_year &to_compare)
const
193 return (month == to_compare.
month)
200 text_form(to_return, how);
204 void day_in_year::text_form(
astring &to_stuff,
int how)
const
207 if (how & INCLUDE_DAY) to_stuff +=
astring(
day_name(day_of_week)) +
" ";
209 if (how & LONG_MONTH)
213 to_stuff +=
a_sprintf(
" %02ld", day_in_month);
222 }
else if (day < 1) {
223 day += days_in_prev_month;
235 if (prev_month < 0) prev_month = 11;
249 attach(packed_form, year);
256 if (!
detach(packed_form, year))
return false;
262 astring time_locus::text_form_long(
int t,
int d,
int y)
const
265 text_form_long(to_return, t, d, y);
271 if (!s2_cast)
throw "error: time_locus::==: unknown type";
272 return (year == s2_cast->
year)
279 if (!s2_cast)
throw "error: time_locus::<: unknown type";
281 if (day_in_year::operator < (*s2_cast))
return true;
282 if (!(day_in_year::operator == (*s2_cast)))
return false;
283 if (clock_time::operator < (*s2_cast))
return true;
287 void time_locus::text_form_long(
astring &to_stuff,
int t,
int d,
int y)
const
291 text_form_long(to_stuff, t, d);
295 day_in_year::text_form(to_stuff, d);
299 to_stuff +=
a_sprintf(
"%2ld", year % 100);
304 clock_time::text_form(to_stuff, t);
313 bool leaping = !(to_fix.
year % 4);
314 if (!(to_fix.
year % 100)) leaping =
false;
315 if (!(to_fix.
year % 400)) leaping =
true;
317 rolls = day_in_year::normalize(to_fix, leaping);
318 to_fix.
year += rolls;
327 const tm &cal_values)
335 r.
hour = cal_values.tm_hour;
336 r.
minute = cal_values.tm_min;
337 r.
second = cal_values.tm_sec;
340 r.
year = cal_values.tm_year + 1900;
346 day_in_year::LONG_MONTH | day_in_year::INCLUDE_DAY,
347 time_locus::LONG_YEAR).
s()));
358 LOG(
"failed to gettimeofday!?");
360 time_t currtime_secs = currtime.tv_sec;
362 tm *tz_ptr = localtime_r(&currtime_secs, &result);
363 if (tz_ptr != &result) {
364 LOG(
"failed to get time for local area with localtime_r");
366 return convert(currtime.tv_sec, currtime.tv_usec, result);
375 LOG(
"failed to gettimeofday!?");
377 time_t currtime_secs = currtime.tv_sec;
379 tm *tz_ptr = gmtime_r(&currtime_secs, &result);
380 if (tz_ptr != &result) {
381 LOG(
"failed to get time for local area with gmtime_r");
383 return convert(currtime.tv_sec, currtime.tv_usec, result);
399 case SUNDAY:
return "Sunday";
400 case MONDAY:
return "Monday";
401 case TUESDAY:
return "Tuesday";
404 case FRIDAY:
return "Friday";
406 default:
return "Not_a_day";
413 case JANUARY:
return "January";
415 case MARCH:
return "March";
416 case APRIL:
return "April";
417 case MAY:
return "May";
418 case JUNE:
return "June";
419 case JULY:
return "July";
420 case AUGUST:
return "August";
422 case OCTOBER:
return "October";
425 default:
return "Not_a_month";
434 case MARCH:
return "Mar";
435 case APRIL:
return "Apr";
436 case MAY:
return "May";
437 case JUNE:
return "Jun";
438 case JULY:
return "Jul";
439 case AUGUST:
return "Aug";
444 default:
return "Not";
a_sprintf is a specialization of astring that provides printf style support.
Provides a dynamically resizable ASCII character string.
const char * s() const
synonym for observe. the 's' stands for "string", if that helps.
A very common template for a dynamic array of bytes.
Base class for object that can tell itself apart from other instances.
A base for objects that can be alphabetically (lexicographically) ordered.
A specific point in time as represented by a 24 hour clock.
time_number minute
The number of minutes after the hour.
time_number second
The number of seconds after the current minute.
time_number microsecond
Number of microseconds elapsed in this millisecond.
time_number hour
The hour represented in military time: 0 through 23.
time_number millisecond
The number of milliseconds elapsed in this second.
An object that represents a particular day in a year.
time_number day_of_year
Numerical day, where January 1st is equal to zero.
time_number day_in_month
The day number within the month (starting at one).
days day_of_week
The day of the week.
months month
The current month.
An object that represents a particular point in time.
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.
#define NULL_POINTER
The value representing a pointer to nothing.
#define limit_value(val, max)
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
The guards collection helps in testing preconditions and reporting errors.
unsigned char abyte
A fairly important unit which is seldom defined...
void attach(byte_array &packed_form, const char *to_attach)
Packs a character string "to_attach" into "packed_form".
bool detach(byte_array &packed_form, astring &to_detach)
Unpacks a character string "to_attach" from "packed_form".
A dynamic container class that holds any kind of object via pointers.
bool unpack(basis::byte_array &packed_form, set< contents > &to_unpack)
provides a way to unpack any set that stores packable objects.
void pack(basis::byte_array &packed_form, const set< contents > &to_pack)
provides a way to pack any set that stores packable objects.
const char * month_name(months to_name)
Returns the name of the month "to_name".
basis::signed_long time_number
time_locus greenwich_now()
returns Greenwich Mean Time (their now).
time_locus now()
returns our current locus in the time continuum.
const char * day_name(days to_name)
Returns the name of the day "to_name".
time_number year_now()
what year is it?
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 char * short_month_name(months to_name)
Returns a shorter, constant-length (3 characters) month name.
time_locus convert(time_number seconds, time_number useconds, const tm &cal_values)
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.
time_number limit_day_of_month(time_number &day, time_number days_in_month, time_number days_in_prev_month)
Support for unicode builds.