dropped use of ftime and other old time functions. now just using gettimeofday and localtime_r and gmtime_r to get all the time nuggets.
typedef unsigned short un_short;
//! Abbreviated name for unsigned long integers.
typedef unsigned long un_long;
+//! Abbreviated name for signed long integers.
+typedef long int signed_long;
+//! Abbreviated name for signed long long integers.
+typedef long long signed_long_long;
// some maximum and minimum values that are helpful.
#ifndef MAXINT32
bool detach(byte_array &packed_form, int &to_detach)
{ return detach(packed_form, (basis::un_int &)to_detach); }
+void attach(byte_array &packed_form, signed_long to_attach)
+{ attach(packed_form, basis::signed_long(to_attach)); }
+
+bool detach(byte_array &packed_form, signed_long &to_detach)
+{ return detach(packed_form, (basis::signed_long &)to_detach); }
+
//void attach(byte_array &packed_form, basis::un_long to_attach)
//{ attach(packed_form, basis::un_int(to_attach)); }
const int PACKED_SIZE_BYTE = 1;
const int PACKED_SIZE_INT16 = 2;
const int PACKED_SIZE_INT32 = 4;
+const int PACKED_SIZE_INT64 = 8;
// these functions pack and unpack popular data types.
bool detach(basis::byte_array &packed_form, int &to_detach);
//!< Unpacks an integer "to_attach" from "packed_form".
+void attach(basis::byte_array &packed_form, long int to_attach);
+ //!< Packs an integer "to_attach" into "packed_form".
+ /*!< This method and the other simple numerical storage methods use a little
+ endian ordering of the bytes. They are platform independent with respect to
+ endianness and will reassemble the number properly on any platform. */
+bool detach(basis::byte_array &packed_form, long int &to_detach);
+ //!< Unpacks an integer "to_attach" from "packed_form".
+
void obscure_attach(basis::byte_array &packed_form, basis::un_int to_attach);
//!< like the normal attach but shifts in some recognizable sentinel data.
/*!< this is slightly more sure than a simple integer attachment. it can
}
}
}
+ return 0;
}
int t_amorph::execute()
void run_test_01();
void run_test_02();
+ void run_test_03();
};
//////////////
// test leap years
// test lotso things.
+}
+
+void test_earth_time::run_test_03()
+{
+ FUNCDEF("run_test_03");
+ // test out the now and greenwich_now methods.
+
+//hmmm: what are some ways to test this?
+
+ time_locus curr_now = now();
+LOG(astring("now() returned locus: ") + curr_now.text_form_long(clock_time::MILITARY, day_in_year::LONG_MONTH | day_in_year::INCLUDE_DAY, time_locus::LONG_YEAR));
+
+ time_locus curr_green = greenwich_now();
+LOG(astring("greenwich_now() returned locus: ") + curr_green.text_form_long(clock_time::MILITARY, day_in_year::LONG_MONTH | day_in_year::INCLUDE_DAY, time_locus::LONG_YEAR));
+
+
}
int test_earth_time::execute()
run_test_01();
run_test_02();
+ run_test_03();
return final_report();
}
\*****************************************************************************/
#include "earth_time.h"
+#include "time_stamp.h"
#include <basis/astring.h>
#include <basis/utf_conversion.h>
+#include <textual/parser_bits.h>
#include <time.h>
+#include <sys/time.h>
#if defined(__WIN32__) || defined(__UNIX__)
- #include <sys/timeb.h>
+// #include <sys/timeb.h>
#endif
+#include <stdio.h>
+
+// uncomment for noisy code.
+//#define DEBUG_EARTH_TIME
+
using namespace basis;
using namespace structures;
+using namespace textual;
namespace timely {
-const int days_in_month[12]
+#undef LOG
+#ifdef DEBUG_EARTH_TIME
+ #define LOG(tpr) printf("%s", (astring("earth_time::") + func + ": " + tpr + parser_bits::platform_eol_to_chars()).s())
+#else
+ #define LOG(tpr)
+#endif
+
+//////////////
+
+const time_number days_in_month[12]
= { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
-const int leap_days_in_month[12]
+const time_number leap_days_in_month[12]
= { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
-const int julian_days_in_month[12]
+const time_number julian_days_in_month[12]
= { 31, 29, 31, 30, 31, 30, 31, 30, 31, 30, 31, 30 };
//hmmm: is this right?
-const int julian_leap_days_in_month[12]
+const time_number julian_leap_days_in_month[12]
= { 31, 30, 31, 30, 31, 30, 31, 30, 31, 30, 31, 30 };
//////////////
{
if (!how) return; // enforce use of the default.
if (how & MILITARY)
- to_return += a_sprintf("%02d:%02d", hour, minute);
+ to_return += a_sprintf("%02ld:%02ld", hour, minute);
else {
- int uhr = hour;
+ time_number uhr = hour;
if (uhr > 12) uhr -= 12;
- to_return += a_sprintf("%2d:%02d", uhr, minute);
+ to_return += a_sprintf("%2ld:%02ld", uhr, minute);
}
if ( (how & SECONDS) || (how & MILLISECONDS) )
- to_return += a_sprintf(":%02d", second);
+ to_return += a_sprintf(":%02ld", second);
if (how & MILLISECONDS)
- to_return += a_sprintf(":%03d", millisecond);
+ to_return += a_sprintf(":%03ld", millisecond);
if (how & MERIDIAN) {
if (hour >= 12) to_return += "PM";
else to_return += "AM";
val -= max * rolls; \
} else { rolls = 0; }
-int clock_time::normalize(clock_time &to_fix)
+time_number clock_time::normalize(clock_time &to_fix)
{
- int rolls = 0; // rollover counter.
+ time_number rolls = 0; // rollover counter.
limit_value(to_fix.microsecond, 1000);
to_fix.millisecond += rolls;
limit_value(to_fix.millisecond, 1000);
monat = month_name(month);
//hmmm: more formatting, like euro?
to_stuff += monat;
- to_stuff += a_sprintf(" %02d", day_in_month);
+ to_stuff += a_sprintf(" %02ld", day_in_month);
}
// note: this only works when adjusting across one month, not multiples.
-int limit_day_of_month(int &day, int days_in_month, int days_in_prev_month)
+time_number limit_day_of_month(time_number &day, time_number days_in_month, time_number days_in_prev_month)
{
if (day > days_in_month) {
day -= days_in_month;
return 0; // no rolling.
}
-int day_in_year::normalize(day_in_year &to_fix, bool leap_year)
+time_number day_in_year::normalize(day_in_year &to_fix, bool leap_year)
{
- int rolls = 0; // rollover counter.
- int daysinm = leap_year?
+ time_number rolls = 0; // rollover counter.
+ time_number daysinm = leap_year?
leap_days_in_month[to_fix.month] : days_in_month[to_fix.month];
- int prev_month = to_fix.month - 1;
+ time_number prev_month = to_fix.month - 1;
if (prev_month < 0) prev_month = 11;
- int daysinpm = leap_year?
+ time_number daysinpm = leap_year?
leap_days_in_month[prev_month] : days_in_month[prev_month];
rolls = limit_day_of_month(to_fix.day_in_month, daysinm, daysinpm);
- int monat = to_fix.month + rolls;
+ time_number monat = to_fix.month + rolls;
limit_value(monat, 12); // months are zero based.
to_fix.month = months(monat);
return rolls;
to_stuff += " ";
// add the year.
if (y & SHORT_YEAR)
- to_stuff += a_sprintf("%2d", year % 100);
+ to_stuff += a_sprintf("%2ld", year % 100);
else
- to_stuff += a_sprintf("%4d", year);
+ to_stuff += a_sprintf("%4ld", year);
// add the time.
to_stuff += " ";
clock_time::text_form(to_stuff, t);
}
-int time_locus::normalize(time_locus &to_fix)
+time_number time_locus::normalize(time_locus &to_fix)
{
- int rolls = clock_time::normalize(to_fix);
+ time_number rolls = clock_time::normalize(to_fix);
to_fix.day_in_month += rolls;
//hmmm: this little gem should be abstracted to a method.
//////////////
-time_locus convert(const tm &to_convert, int ms)
+time_locus convert(time_number seconds, time_number useconds,
+ const tm &cal_values)
{
+ FUNCDEF("convert");
time_locus r;
- // we lack the resolution for this, currently.
- r.microsecond = 0;
-
- r.second = to_convert.tm_sec;
- r.minute = to_convert.tm_min;
- r.hour = to_convert.tm_hour;
- r.day_in_month = to_convert.tm_mday;
- r.month = months(to_convert.tm_mon);
- r.year = to_convert.tm_year + 1900;
- r.day_of_week = days(to_convert.tm_wday);
- r.day_of_year = to_convert.tm_yday;
- r.millisecond = ms;
+ r.millisecond = useconds / 1000;
+ r.microsecond = useconds % 1000;
+
+ r.hour = cal_values.tm_hour;
+ r.minute = cal_values.tm_min;
+ r.second = cal_values.tm_sec;
+ r.day_in_month = cal_values.tm_mday;
+ r.month = months(cal_values.tm_mon);
+ r.year = cal_values.tm_year + 1900;
+ r.day_of_week = days(cal_values.tm_wday);
+ r.day_of_year = cal_values.tm_yday;
+
+ LOG(a_sprintf("convert() returning: %s\n",
+ r.text_form_long(clock_time::MILITARY,
+ day_in_year::LONG_MONTH | day_in_year::INCLUDE_DAY,
+ time_locus::LONG_YEAR).s()));
+
return r;
}
time_locus now()
{
- timeb current;
- ftime(¤t);
- tm split_time(*localtime(¤t.time));
- return convert(split_time, current.millitm);
+ FUNCDEF("now")
+ timeval currtime;
+ int okay = gettimeofday(&currtime, NULL_POINTER);
+ if (okay != 0) {
+ LOG("failed to gettimeofday!?");
+ }
+ time_t currtime_secs = currtime.tv_sec;
+ struct tm result;
+ tm *tz_ptr = localtime_r(&currtime_secs, &result);
+ if (tz_ptr != &result) {
+ LOG("failed to get time for local area with localtime_r");
+ }
+ return convert(currtime.tv_sec, currtime.tv_usec, result);
}
time_locus greenwich_now()
{
- timeb current;
- ftime(¤t);
- tm split_time(*gmtime(¤t.time));
- return convert(split_time, current.millitm);
+ FUNCDEF("greenwich_now")
+ timeval currtime;
+ int okay = gettimeofday(&currtime, NULL_POINTER);
+ if (okay != 0) {
+ LOG("failed to gettimeofday!?");
+ }
+ time_t currtime_secs = currtime.tv_sec;
+ tm result;
+ tm *tz_ptr = gmtime_r(&currtime_secs, &result);
+ if (tz_ptr != &result) {
+ LOG("failed to get time for local area with gmtime_r");
+ }
+ return convert(currtime.tv_sec, currtime.tv_usec, result);
}
clock_time time_now() { return now(); }
months month_now() { return now().month; }
-int year_now() { return now().year; }
+time_number year_now() { return now().year; }
day_in_year date_now() { return now(); }
#ifndef EARTH_TIME_GROUP
#define EARTH_TIME_GROUP
-// Name : earth_time
-// Author : Chris Koeritz
-/******************************************************************************
-* Copyright (c) 1999-$now By Author. This program is free software; you can *
-* redistribute it and/or modify it under the terms of the GNU General Public *
-* License as published by the Free Software Foundation; either version 2 of *
-* the License or (at your option) any later version. This is online at: *
-* http://www.fsf.org/copyleft/gpl.html *
-* Please send any updates to: fred@gruntose.com *
-\*****************************************************************************/
+/*
+ Name : earth_time
+ Author : Chris Koeritz
+
+ Copyright (c) 1999-$now By Author. This program is free software; you can
+ redistribute it and/or modify it under the terms of the GNU General Public
+ License as published by the Free Software Foundation; either version 2 of
+ the License or (at your option) any later version. This is online at:
+ http://www.fsf.org/copyleft/gpl.html
+ Please send any updates to: fred@gruntose.com
+*/
#include <basis/astring.h>
#include <basis/byte_array.h>
#include <basis/contracts.h>
+//#include <basis/definitions.h>
#include <structures/object_packers.h>
+///#include <time.h>
+
//! A set of methods for rendering calendrical and clock times.
/*!
It is based on the Gregorian calendar currently in use by the USA and other
countries.
*/
-
namespace timely {
+ typedef basis::signed_long time_number;
+
enum days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY };
//!< The names of the days of the week.
const char *short_month_name(months to_name);
//!< Returns a shorter, constant-length (3 characters) month name.
- extern const int days_in_month[12];
+ extern const time_number days_in_month[12];
//!< The number of days in each month in the standard year.
- extern const int leap_days_in_month[12];
+ extern const time_number leap_days_in_month[12];
//!< The number of days in each month in a leap year.
- extern const int julian_days_in_month[12];
+ extern const time_number julian_days_in_month[12];
//!< Number of days in each month based on the julian calendar.
- extern const int julian_leap_days_in_month[12];
+ extern const time_number julian_leap_days_in_month[12];
//!< Number of days in each month of a leap year in the julian calendar.
- const int SECONDS_IN_MINUTE = 60; //!< Number of seconds in one minute.
- const int MINUTES_IN_HOUR = 60; //!< Number of minutes in an hour.
- const int HOURS_IN_DAY = 24; //!< Number of hours in a day.
- const int DAYS_IN_YEAR = 365; //!< Number of days in a standard year.
- const int LEAP_DAYS_IN_YEAR = 366; //!< Number of days in a leap year.
+ const time_number SECONDS_IN_MINUTE = 60; //!< Number of seconds in one minute.
+ const time_number MINUTES_IN_HOUR = 60; //!< Number of minutes in an hour.
+ const time_number SECONDS_IN_HOUR = 3600; //!< Number of seconds in an hour.
+ const time_number HOURS_IN_DAY = 24; //!< Number of hours in a day.
+ const time_number MINUTES_IN_DAY = 1440; //!< Number of minutes in a day.
+ const time_number SECONDS_IN_DAY = 86400; //!< Number of seconds in a day.
+ const time_number DAYS_IN_YEAR = 365; //!< Number of days in a standard year.
+ const time_number LEAP_DAYS_IN_YEAR = 366; //!< Number of days in a leap year.
const double APPROX_DAYS_IN_YEAR = 365.2424;
//!< A more accurate measure of the number of days in a year.
/*!< This is the more accurate mean length of time in 24 hour days between
class clock_time : public virtual basis::packable
{
public:
- int hour; //!< The hour represented in military time: 0 through 23.
- int minute; //!< The number of minutes after the hour.
- int second; //!< The number of seconds after the current minute.
- int millisecond; //!< The number of milliseconds elapsed in this second.
- int microsecond; //!< Number of microseconds elapsed in this millisecond.
+ time_number hour; //!< The hour represented in military time: 0 through 23.
+ time_number minute; //!< The number of minutes after the hour.
+ time_number second; //!< The number of seconds after the current minute.
+ time_number millisecond; //!< The number of milliseconds elapsed in this second.
+ time_number microsecond; //!< Number of microseconds elapsed in this millisecond.
//! Constructs a clock_time object given all the parts.
- clock_time(int h = 0, int m = 0, int s = 0, int ms = 0, int us = 0)
+ clock_time(time_number h = 0, time_number m = 0, time_number s = 0, time_number ms = 0, time_number us = 0)
: hour(h), minute(m), second(s), millisecond(ms),
microsecond(us) {}
~clock_time() {}
/*!< note that "to_stuff" will be appended to; the existing contents
are retained. */
- static int normalize(clock_time &to_fix);
+ static time_number normalize(clock_time &to_fix);
// ensures that the units in each field are in the proper range by
// promoting them upwards. if the clock_time goes above the maximum hour,
// then it rolls around. zero is returned for no rollover or a positive
{
public:
months month; //!< The current month.
- int day_in_month; //!< The day number within the month (starting at one).
+ time_number day_in_month; //!< The day number within the month (starting at one).
days day_of_week; //!< The day of the week.
- int day_of_year; //!< Numerical day, where January 1st is equal to zero.
+ time_number day_of_year; //!< Numerical day, where January 1st is equal to zero.
- int packed_size() const { return 4 * structures::PACKED_SIZE_INT32; }
+ int packed_size() const { return 4 * structures::PACKED_SIZE_INT64; }
//! Constructs a representation of the day specified.
- day_in_year(months m = JANUARY, int dim = 1, days dow = SUNDAY,
- int day_o_year = 1) : month(m), day_in_month(dim),
+ day_in_year(months m = JANUARY, time_number dim = 1, days dow = SUNDAY,
+ time_number day_o_year = 1) : month(m), day_in_month(dim),
day_of_week(dow), day_of_year(day_o_year) {}
virtual void pack(basis::byte_array &packed_form) const;
void text_form(basis::astring &to_stuff, int how = SHORT_MONTH) const;
//!< Prints the day according to "how" and stores it in "to_stuff".
- static int normalize(day_in_year &to_fix, bool leap_year = false);
+ static time_number normalize(day_in_year &to_fix, bool leap_year = false);
//!< normalizes the day as needed and returns the adjustment in years.
/*!< note that this only adjusts the day_in_month and month members
currently. the other counters are not changed. */
public virtual basis::hoople_standard
{
public:
- int year; //!< The year, using the gregorian calendar.
+ time_number year; //!< The year, using the gregorian calendar.
time_locus() : clock_time(), day_in_year(), year() {}
DEFINE_CLASS_NAME("time_locus");
//! Constructs a location in time given its components.
- time_locus(const clock_time &ct, const day_in_year &ytd, int year_in)
+ time_locus(const clock_time &ct, const day_in_year &ytd, time_number year_in)
: clock_time(ct), day_in_year(ytd), year(year_in) {}
int packed_size() const { return clock_time::packed_size()
int d = day_in_year::SHORT_MONTH, int y = LONG_YEAR) const;
//! Same as text_form() above, but stores into "to_stuff".
- static int normalize(time_locus &to_fix);
+ static time_number normalize(time_locus &to_fix);
//!< normalizes the time_locus for its clock time and date.
//hmmm: what are rollovers measured in?
};
- int year_now(); //!< what year is it?
+ time_number year_now(); //!< what year is it?
clock_time time_now(); //!< what time is it?
day_in_year date_now(); //!< what day on the calendar is it?
time_locus now(); //!< returns our current locus in the time continuum.
using namespace structures;
using namespace timely;
-//#define DEBUG_TIMER_DRIVER
+#define DEBUG_TIMER_DRIVER
// uncomment for noisy code.
#undef LOG
-#define LOG(tpr) printf( (time_stamp::notarize() + "timer_driver::" + func + tpr).s() )
+#define LOG(tpr) printf("%s", (time_stamp::notarize() + "timer_driver::" + func + tpr).s() )
namespace timely {
if (true) { // unknown OS.
#endif
#ifdef DEBUG_TIMER_DRIVER
- LOG(a_sprintf("unknown signal/message %x caught.", (void *)seen));
+ LOG(a_sprintf("unknown signal/message %d caught.", seen));
#endif
return;
}
echo
echo "comparing $dir"
echo
- compare_dirs "/z/$dir" "fred@${DATA_SOURCE_HOST}:/z/$dir"
+ echo "'<' will be remote, '>' will be local."
+ echo
+ compare_dirs "fred@${DATA_SOURCE_HOST}:/z/$dir" "/z/$dir"
retval=$?
s
echo
echo "Failure to find virtual Unix root folder with cygpath."
exit 1322
fi
-echo "found root as '$found_root'"
+#echo "found root as '$found_root'"
# translate any backslashes to forward thinking slashes.
found_root="$(echo $found_root | tr '\\' '/')"
-echo "processed root is now: '$found_root'"
+#echo "processed root is now: '$found_root'"
# edit the entry in place to correct the default path.
sed -i \
-e "s% *#define FEISTY_MEOW_VIRTUAL_UNIX_ROOT \".*$%#define FEISTY_MEOW_VIRTUAL_UNIX_ROOT \"$found_root\"%" \
"$FEISTY_MEOW_BINARIES/system_helper.h"
exit_on_error "updating system_helper header in $FEISTY_MEOW_BINARIES"
-echo "system helper file now has:"
-cat "$FEISTY_MEOW_BINARIES/system_helper.h"
+#echo "system helper file now has:"
+#cat "$FEISTY_MEOW_BINARIES/system_helper.h"
fi
}