//============================================================================= // drt/sys/stime.cpp // DRT primitive date and time class functions. // // History // 0.01, 1999-03-09, David R Tribble. // First cut. // // Copyright ©1999, by David R. Tribble, all rights reserved. // See "drt/sys/copyr.txt" for more information. //----------------------------------------------------------------------------- // Identification static const char id[] = "@(#)drt/sys/stime.cpp 0.01"; // Special includes #include "sdefs.hpp" #include "stime.hpp" // System includes #include #define drt_std_assert_h 1 #include #define drt_std_limits_h 1 #if TEST #include #define drt_std_stdio_h 1 #endif // Local includes #include "sdebug.hpp" // Local wrappers drt_namespace_begin //----------------------------------------------------------------------------- // Shared class constants //----------------------------------------------------------------------------- #if DrtTime_VS/100 != 1 #error DrtTime_VS has changed #endif /*static*/ const int DrtTime::VS = DrtTime_VS; // Class version number /*static*/ const int DrtTime::ZYEAR = 2001; // Zero year of the epoch /*static*/ const long int DrtTime::TICKS_PER_SEC = 10000000; // == 10E+6 // Number of ticks in a second /*static*/ const long int DrtTime::TICKS_PER_DAY = DrtTime::TICKS_PER_SEC * 3600 * 24; // Number of ticks in a day /*static*/ const drt_int64_t DrtTime::TICKS_PER_YEAR = (drt_int64_t) DrtTime::TICKS_PER_SEC/10000 * 3562425 * 24 * 3600; // Number of ticks in a year /*static*/ const drt_int64_t DrtTime::MIN = (-9998 - DrtTime::ZYEAR) * DrtTime::TICKS_PER_YEAR; // BC9999-01-01 00:00:00.000 Z /*static*/ const drt_int64_t DrtTime::MAX = (+10000 - DrtTime::ZYEAR) * DrtTime::TICKS_PER_YEAR - 1; // AD9999-12-31 23:59:59.999 Z /*static*/ const drt_int64_t DrtTime::UNKNOWN = LLONG_MIN; // Unknown date /*static*/ const drt_int64_t DrtTime::NEVER = LLONG_MAX; // Never date /*static*/ const drt_int64_t DrtTime::BC4713 = (-4712 - DrtTime::ZYEAR) * DrtTime::TICKS_PER_YEAR; // BC4713-01-01 12:00:00.000 Z /*static*/ const drt_int64_t DrtTime::BC0001 = (+0 - DrtTime::ZYEAR) * DrtTime::TICKS_PER_YEAR; // BC0001-01-01 00:00:00.000 Z /*static*/ const drt_int64_t DrtTime::AD0001 = (+1 - DrtTime::ZYEAR) * DrtTime::TICKS_PER_YEAR; // AD0001-01-01 00:00:00.000 Z /*static*/ const drt_int64_t DrtTime::AD1601 = (+1601 - DrtTime::ZYEAR) * DrtTime::TICKS_PER_YEAR; // AD1601-01-01 00:00:00.000 Z /*static*/ const drt_int64_t DrtTime::AD1858 = 0; //??? // AD1858-11-?? 00:00:00.000 Z /*static*/ const drt_int64_t DrtTime::AD1900 = (+1900 - DrtTime::ZYEAR) * DrtTime::TICKS_PER_YEAR; // AD1900-01-01 00:00:00.000 Z /*static*/ const drt_int64_t DrtTime::AD1970 = (+1970 - DrtTime::ZYEAR) * DrtTime::TICKS_PER_YEAR; // AD1970-01-01 00:00:00.000 Z /*static*/ const drt_int64_t DrtTime::AD1972 = (+1972 - DrtTime::ZYEAR) * DrtTime::TICKS_PER_YEAR; // AD1972-01-01 00:00:00.000 Z /*static*/ const drt_int64_t DrtTime::AD1980 = (+1980 - DrtTime::ZYEAR) * DrtTime::TICKS_PER_YEAR; // AD1980-01-01 00:00:00.000 Z /*static*/ const drt_int64_t DrtTime::AD2001 = (+2001 - DrtTime::ZYEAR) * DrtTime::TICKS_PER_YEAR; // AD2001-01-01 00:00:00.000 Z //----------------------------------------------------------------------------- // Shared class variables //----------------------------------------------------------------------------- #if DrtTime_VS/100 != 1 #error DrtTime_VS has changed #endif /*static*/ DrtTraceGroup DrtTime::s_grp("DrtTime", __FILE__, __DATE__, __TIME__); // Class debugging group //----------------------------------------------------------------------------- // Class member functions //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // DrtTime::~DrtTime() // Destructor. //----------------------------------------------------------------------------- /*void*/ DrtTime::~DrtTime() { #if DrtTime_VS != 100 #error DrtTime_VS has changed #endif DrtTrace dbg(s_grp, "~DrtTime", this); // Validate this object validate(); // De-initialize members m_ticks = UNKNOWN; } //----------------------------------------------------------------------------- // DrtTime::DrtTime() // Default constructor. //----------------------------------------------------------------------------- /*DrtTime*/ DrtTime::DrtTime(): m_ticks(UNKNOWN) { #if DrtTime_VS != 100 #error DrtTime_VS has changed #endif DrtTrace dbg(s_grp, "DrtTime", this); // Initialize other members // (None) } //----------------------------------------------------------------------------- // DrtTime::DrtTime() // Copy constructor. //----------------------------------------------------------------------------- /*DrtTime*/ DrtTime::DrtTime(const DrtTime &r): m_ticks(r.m_ticks) { #if DrtTime_VS != 100 #error DrtTime_VS has changed #endif DrtTrace dbg(s_grp, "DrtTime", this); // Initialize other members // (None) } //----------------------------------------------------------------------------- // DrtTime::operator =() // Assignment operator. //----------------------------------------------------------------------------- const DrtTime & DrtTime::operator =(const DrtTime &r) { #if DrtTime_VS != 100 #error DrtTime_VS has changed #endif #if DEBUG >= 2 DrtTrace dbg(s_grp, "operator=", this); #endif // Assign members m_ticks = r.m_ticks; return (*this); } //----------------------------------------------------------------------------- // DrtTime::validate() // Validate this object. // // Notes // Fails an assert() if this object is invalid. //----------------------------------------------------------------------------- void DrtTime::validate() const { #if DrtTime_VS/100 != 1 #error DrtTime_VS has changed #endif //DrtTrace dbg(s_grp, "validate", this); // Validate this object assert(("DrtTime", this != null)); // Check for null ///assert(DrtTime::m_magic == MAGIC); // Check magic number ///assert(DrtTime::m_vs/100 == VS/100); // Check version number } //----------------------------------------------------------------------------- // DrtTime::dump() // Dump the contents of this object to the debugging trace output. //----------------------------------------------------------------------------- void DrtTime::dump() const { #if DrtTime_VS != 100 #error DrtTime_VS has changed #endif //DrtTrace dbg(s_grp, "dump", this); // Validate this object validate(); // Lock the debug output context DrtTrace::enterContext(); // Dump the contents of this (base) object DrtTrace::print(".m_ticks ...... %+lld\n", (drt_int64_t) m_ticks); // Done DrtTrace::leaveContext(); } //----------------------------------------------------------------------------- // DrtTime::isValid() // Determines if this date value is valid, i.e., contains a value within // the normal range of dates within the epoch. // // Returns // True if this date is invalid, otherwise false. //----------------------------------------------------------------------------- bool DrtTime::isValid() const { #if DrtTime_VS/100 != 1 #error DrtTime_VS has changed #endif #if DEBUG >= 2 DrtTrace dbg(s_grp, "isValid", this); #endif // Check this object validate(); // Check for special cases if (m_ticks >= MIN and m_ticks <= MAX) return (true); if (m_ticks == UNKNOWN) // Is 'unknown' return (true); if (m_ticks == NEVER) // Is 'never' return (true); return (false); } //----------------------------------------------------------------------------- // DrtTime::isUnknown() // Determines if this date value is "unknown" (i.e., not set). // // Returns // True if this date is unknown, otherwise false. //----------------------------------------------------------------------------- bool DrtTime::isUnknown() const { #if DrtTime_VS/100 != 1 #error DrtTime_VS has changed #endif #if DEBUG >= 2 DrtTrace dbg(s_grp, "isUnknown", this); #endif // Check this object validate(); // Check the date fields return (m_ticks == UNKNOWN); } //----------------------------------------------------------------------------- // DrtTime::isNever() // Determines if this date value is "never" (i.e., a time that will never // transpire). // // Returns // True if this date is never, otherwise false. //----------------------------------------------------------------------------- bool DrtTime::isNever() const { #if DrtTime_VS/100 != 1 #error DrtTime_VS has changed #endif #if DEBUG >= 2 DrtTrace dbg(s_grp, "isNever", this); #endif // Check this object validate(); // Check the date fields return (m_ticks == NEVER); } //----------------------------------------------------------------------------- // DrtTime::ticks() // Retrieve the ticks count of this date value. // // Returns // Number of subsecond ticks since the zero (center) date of the epoch. //----------------------------------------------------------------------------- drt_int64_t DrtTime::ticks() const { #if DrtTime_VS/100 != 1 #error DrtTime_VS has changed #endif #if DEBUG >= 2 DrtTrace dbg(s_grp, "ticks", this); #endif // Check this object validate(); // Return the ticks count return (m_ticks); } drt_namespace_end //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- #if TEST //----------------------------------------------------------------------------- // ::buf_print() // Print the contents of string 'buf' to stdout. //----------------------------------------------------------------------------- static void buf_print(const char *buf) { #if is_incomplete___ ...REDO buf[0].print(); #else ...REDO // Temporary hack ///drt_std::printf("::buf_print() is incomplete\n"); for (int i = 0; buf[i] != '\0'; i++) putc(buf[i], stdout); #endif } //----------------------------------------------------------------------------- // ::main() // Test driver. //----------------------------------------------------------------------------- int main(int argc, char **argv) { printf("Create an DrtTime object...\n"); drt_namespace::DrtTime date; printf("Print its value...\n"); char buf[30]; ...REDO date.toString(buf, sizeof(buf)); buf_print(buf); printf("\n"); ...REDO printf("Get the current date and time...\n"); date.setToNow(); date.toString(buf, sizeof(buf)); buf_print(buf); printf("\n"); return (0); } #endif // TEST // End stime.cpp