version 1.83, 2009/06/10 16:23:40
|
version 1.84, 2009/12/15 11:39:33
|
|
|
#include <fstream> | #include <fstream> |
#include "CIMDateTime.h" | #include "CIMDateTime.h" |
#include "CIMDateTimeRep.h" | #include "CIMDateTimeRep.h" |
|
#include "CIMDateTimeInline.h" |
#include "Exception.h" | #include "Exception.h" |
#include "System.h" | #include "System.h" |
#include "AutoPtr.h" | #include "AutoPtr.h" |
|
|
// | // |
//============================================================================== | //============================================================================== |
| |
// Julian day of "1 BCE January 1". |
|
static const Uint32 JULIAN_ONE_BCE = 1721060; |
|
|
|
// Number of microseconds in one second. |
|
static const Uint64 SECOND = 1000000; |
|
|
|
// Number of microseconds in one minute. |
|
static const Uint64 MINUTE = 60 * SECOND; |
|
|
|
// Number of microseconds in one hour. |
|
static const Uint64 HOUR = 60 * MINUTE; |
|
|
|
// Number of microseconds in one day. |
|
static const Uint64 DAY = 24 * HOUR; |
|
|
|
// Number of microseconds in ten thousand years. | // Number of microseconds in ten thousand years. |
static const Uint64 TEN_THOUSAND_YEARS = | static const Uint64 TEN_THOUSAND_YEARS = |
PEGASUS_UINT64_LITERAL(315569520000000000); | PEGASUS_UINT64_LITERAL(315569520000000000); |
|
|
return _daysPerMonth[month - 1]; | return _daysPerMonth[month - 1]; |
} | } |
| |
/** Convert month, day, and year to a Julian day (in the Gregorian calendar). |
|
Return julian day. |
|
*/ |
|
static inline Uint32 _toJulianDay(Uint32 year, Uint32 month, Uint32 day) |
|
{ |
|
// Formula adapted from "FREQUENTLY ASKED QUESTIONS ABOUT CALENDARS" |
|
// (see http://www.tondering.dk/claus/calendar.html). |
|
|
|
int a = (14 - month)/12; |
|
int y = year+4800-a; |
|
int m = month + 12*a - 3; |
|
return day + (153*m+2)/5 + y*365 + y/4 - y/100 + y/400 - 32045; |
|
} |
|
|
|
/** Convert a Julian day number (in the Gregorian calendar) to year, month, |
|
and day. |
|
*/ |
|
static inline void _fromJulianDay( |
|
Uint32 jd, Uint32& year, Uint32& month, Uint32& day) |
|
{ |
|
// Formula adapted from "FREQUENTLY ASKED QUESTIONS ABOUT CALENDARS" |
|
// (see http://www.tondering.dk/claus/calendar.html). |
|
|
|
int a = jd + 32044; |
|
int b = (4*a+3)/146097; |
|
int c = a - (b*146097)/4; |
|
int d = (4*c+3)/1461; |
|
int e = c - (1461*d)/4; |
|
int m = (5*e+2)/153; |
|
day = e - (153*m+2)/5 + 1; |
|
month = m + 3 - 12*(m/10); |
|
year = b*100 + d - 4800 + m/10; |
|
} |
|
| |
/** Optimized version of _strToUint32() for n=2 case. | /** Optimized version of _strToUint32() for n=2 case. |
*/ | */ |
|
|
return tmp.usec; | return tmp.usec; |
} | } |
| |
/** Converts a CIMDateTimeRep representation to its canonical string |
|
representation as defined in the "CIM infrastructure Specification". |
|
Note that this implementation preserves any wildcard characters used |
|
to initially create the CIMDateTime object. |
|
*/ |
|
static void _toCStr(const CIMDateTimeRep* rep, char buffer[26]) |
|
{ |
|
if (rep->sign == ':') |
|
{ |
|
// Extract components: |
|
|
|
Uint64 usec = rep->usec; |
|
Uint32 microseconds = Uint32(usec % SECOND); |
|
Uint32 seconds = Uint32((usec / SECOND) % 60); |
|
Uint32 minutes = Uint32((usec / MINUTE) % 60); |
|
Uint32 hours = Uint32((usec / HOUR) % 24); |
|
Uint32 days = Uint32((usec / DAY)); |
|
|
|
// Format the string. |
|
|
|
sprintf( |
|
buffer, |
|
"%08u%02u%02u%02u.%06u:000", |
|
Uint32(days), |
|
Uint32(hours), |
|
Uint32(minutes), |
|
Uint32(seconds), |
|
Uint32(microseconds)); |
|
} |
|
else |
|
{ |
|
// Extract components: |
|
|
|
Uint64 usec = rep->usec; |
|
Uint32 microseconds = Uint32(usec % SECOND); |
|
Uint32 seconds = Uint32((usec / SECOND) % 60); |
|
Uint32 minutes = Uint32((usec / MINUTE) % 60); |
|
Uint32 hours = Uint32((usec / HOUR) % 24); |
|
Uint32 days = Uint32((usec / DAY)); |
|
Uint32 jd = Uint32(days + JULIAN_ONE_BCE); |
|
|
|
// Convert back from julian to year/month/day: |
|
|
|
Uint32 year; |
|
Uint32 month; |
|
Uint32 day; |
|
_fromJulianDay(jd, year, month, day); |
|
|
|
// Format the string. |
|
|
|
sprintf( |
|
buffer, |
|
"%04u%02u%02u%02u%02u%02u.%06u%c%03u", |
|
Uint32(year), |
|
Uint32(month), |
|
Uint32(day), |
|
Uint32(hours), |
|
Uint32(minutes), |
|
Uint32(seconds), |
|
Uint32(microseconds), |
|
rep->sign, |
|
rep->utcOffset); |
|
} |
|
|
|
// Fill buffer with '*' chars (if any). |
|
{ |
|
char* first = buffer + 20; |
|
char* last = buffer + 20 - rep->numWildcards; |
|
|
|
if (rep->numWildcards > 6) |
|
last--; |
|
|
|
for (; first != last; first--) |
|
{ |
|
if (*first != '.') |
|
*first = '*'; |
|
} |
|
} |
|
} |
|
|
|
/** This table is used to convert integers between 0 and 99 (inclusive) to | /** This table is used to convert integers between 0 and 99 (inclusive) to |
a char16 array, with zero padding. | a char16 array, with zero padding. |
*/ | */ |
|
|
| |
char s1[26]; | char s1[26]; |
char s2[26]; | char s2[26]; |
_toCStr(&x1, s1); |
_DateTimetoCStr(x1, s1); |
_toCStr(&y1, s2); |
_DateTimetoCStr(y1, s2); |
return _matchTimeStampStrings(s1, s2); | return _matchTimeStampStrings(s1, s2); |
} | } |
else | else |
{ | { |
char s1[26]; | char s1[26]; |
char s2[26]; | char s2[26]; |
_toCStr(x, s1); |
_DateTimetoCStr(*x, s1); |
_toCStr(y, s2); |
_DateTimetoCStr(*y, s2); |
return _matchTimeStampStrings(s1, s2); | return _matchTimeStampStrings(s1, s2); |
} | } |
} | } |
|
|
{ | { |
} | } |
| |
|
CIMDateTime::CIMDateTime(const CIMDateTimeRep* x) |
|
{ |
|
_rep = new CIMDateTimeRep; |
|
memcpy(_rep, x, sizeof(CIMDateTimeRep)); |
|
} |
|
|
|
|
CIMDateTime::~CIMDateTime() | CIMDateTime::~CIMDateTime() |
{ | { |
delete _rep; | delete _rep; |