version 1.25, 2002/08/17 00:59:36
|
version 1.26, 2002/08/19 18:04:15
|
|
|
// Modified By: Sushma Fernandes, Hewlett-Packard Company | // Modified By: Sushma Fernandes, Hewlett-Packard Company |
// (sushma_fernandes@hp.com) | // (sushma_fernandes@hp.com) |
// Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com) | // Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com) |
|
// Carol Ann Krug Graves, Hewlett-Packard Company |
|
// (carolann_graves@hp.com) |
// | // |
//%///////////////////////////////////////////////////////////////////////////// | //%///////////////////////////////////////////////////////////////////////////// |
| |
|
|
#endif | #endif |
| |
#include <cctype> | #include <cctype> |
#include <cstdlib> |
|
#include <cstdio> |
|
#include <time.h> | #include <time.h> |
|
#include <Pegasus/Common/Destroyer.h> |
#include "CIMDateTime.h" | #include "CIMDateTime.h" |
#include "InternalException.h" | #include "InternalException.h" |
#include "Array.h" |
|
| |
#if defined(PEGASUS_OS_TYPE_WINDOWS) | #if defined(PEGASUS_OS_TYPE_WINDOWS) |
# include <Pegasus/Common/CIMDateTimeWindows.cpp> | # include <Pegasus/Common/CIMDateTimeWindows.cpp> |
|
|
clear(); | clear(); |
} | } |
| |
CIMDateTime::CIMDateTime(const char* str) |
CIMDateTime::CIMDateTime(const String & str) |
{ | { |
_rep = new CIMDateTimeRep(); | _rep = new CIMDateTimeRep(); |
if (!_set(str)) | if (!_set(str)) |
|
|
delete _rep; | delete _rep; |
} | } |
| |
Boolean CIMDateTime::isNull() const |
String CIMDateTime::toString () const |
{ | { |
return strcmp(_rep->data, _NULL_INTERVAL_TYPE_STRING) == 0; |
return String (_rep->data); |
} |
|
|
|
const char* CIMDateTime::getString() const |
|
{ |
|
return _rep->data; |
|
} | } |
| |
void CIMDateTime::clear() | void CIMDateTime::clear() |
|
|
strcpy(_rep->data, _NULL_INTERVAL_TYPE_STRING); | strcpy(_rep->data, _NULL_INTERVAL_TYPE_STRING); |
} | } |
| |
Boolean CIMDateTime::_set(const char* str) |
Boolean CIMDateTime::_set(const String & dateTimeStr) |
{ | { |
clear(); | clear(); |
| |
|
ArrayDestroyer <char> dtStr (dateTimeStr.allocateCString ()); |
|
|
|
const char* str = dtStr.getPointer (); |
|
|
// Be sure the incoming string is the proper length: | // Be sure the incoming string is the proper length: |
| |
if (strlen(str) != CIMDateTimeRep::FORMAT_LENGTH) | if (strlen(str) != CIMDateTimeRep::FORMAT_LENGTH) |
|
|
sprintf(buffer, "%2.2s", str + 8); | sprintf(buffer, "%2.2s", str + 8); |
long hours = atoi(buffer); | long hours = atoi(buffer); |
| |
if (hours > 24) |
if (hours > 23) |
return false; | return false; |
| |
sprintf(buffer, "%2.2s", str + 10); | sprintf(buffer, "%2.2s", str + 10); |
|
|
return true; | return true; |
} | } |
| |
void CIMDateTime::set(const char* str) |
void CIMDateTime::set(const String & str) |
{ | { |
if (!_set(str)) | if (!_set(str)) |
throw BadDateTimeFormat(); | throw BadDateTimeFormat(); |
} | } |
| |
CIMDateTime CIMDateTime::clone() const |
|
{ |
|
return CIMDateTime(*this); |
|
} |
|
|
|
PEGASUS_STD(ostream)& operator<<(PEGASUS_STD(ostream)& os, const CIMDateTime& x) |
|
{ |
|
return os << x.getString(); |
|
} |
|
|
|
Boolean operator==(const CIMDateTime& x, const CIMDateTime& y) | Boolean operator==(const CIMDateTime& x, const CIMDateTime& y) |
{ | { |
return memcmp(x._rep->data, y._rep->data, sizeof(x._rep->data)) == 0; |
return x.equal (y); |
} | } |
| |
void CIMDateTime::formatDateTime(char* dateTimeStr, tm* tm) |
void formatDateTime(char* dateTimeStr, tm* tm) |
{ | { |
Uint32 index = 0, index1 = 0; | Uint32 index = 0, index1 = 0; |
long year = 0; | long year = 0; |
|
|
| |
Boolean CIMDateTime::isInterval() | Boolean CIMDateTime::isInterval() |
{ | { |
const char* str; |
|
const Uint32 SIGN_OFFSET = 21; | const Uint32 SIGN_OFFSET = 21; |
| |
str = getString(); |
Boolean isInterval = strcmp(&_rep->data[SIGN_OFFSET], ":000") == 0 ; |
Boolean isInterval = strcmp(&str[SIGN_OFFSET], ":000") == 0 ; |
|
| |
return isInterval; | return isInterval; |
} | } |
| |
|
Boolean CIMDateTime::equal (const CIMDateTime & x) const |
|
{ |
|
return memcmp (this->_rep->data, x._rep->data, sizeof (this->_rep->data)) |
|
== 0; |
|
} |
|
|
Sint64 CIMDateTime::getDifference(CIMDateTime startTime, CIMDateTime finishTime) | Sint64 CIMDateTime::getDifference(CIMDateTime startTime, CIMDateTime finishTime) |
{ | { |
const char* startDateTimeCString; | const char* startDateTimeCString; |
|
|
// | // |
// Get the dates in CString form | // Get the dates in CString form |
// | // |
startDateTimeCString = startTime.getString(); |
startDateTimeCString = startTime._rep->data; |
finishDateTimeCString = finishTime.getString(); |
finishDateTimeCString = finishTime._rep->data; |
| |
// | // |
// Check if the startTime or finishTime are intervals | // Check if the startTime or finishTime are intervals |
|
|
if (startTime.isInterval() && finishTime.isInterval()) | if (startTime.isInterval() && finishTime.isInterval()) |
{ | { |
char intervalBuffer[9]; | char intervalBuffer[9]; |
Uint32 startIntervalDays; |
// |
Uint32 startIntervalHours; |
// NOTE: although a Uint64 is not required to hold the maximum |
Uint32 startIntervalMinutes; |
// value for these variables, if they are not defined as Uint64s, |
Uint32 startIntervalSeconds; |
// overflow/truncation can occur during the calculation of the |
Uint32 finishIntervalDays; |
// number of microseconds, and the final result may be incorrect |
Uint32 finishIntervalHours; |
// |
Uint32 finishIntervalMinutes; |
Uint64 startIntervalDays; |
Uint32 finishIntervalSeconds; |
Uint64 startIntervalHours; |
Uint64 startIntervalInSeconds; |
Uint64 startIntervalMinutes; |
Uint64 finishIntervalInSeconds; |
Uint64 startIntervalSeconds; |
Sint64 intervalDifferenceInSeconds; |
Uint64 startIntervalMicroseconds; |
|
Uint64 finishIntervalDays; |
|
Uint64 finishIntervalHours; |
|
Uint64 finishIntervalMinutes; |
|
Uint64 finishIntervalSeconds; |
|
Uint64 finishIntervalMicroseconds; |
|
Uint64 startIntervalInMicroseconds; |
|
Uint64 finishIntervalInMicroseconds; |
|
Sint64 intervalDifferenceInMicroseconds; |
| |
// Parse the start time interval and get the days, minutes, hours | // Parse the start time interval and get the days, minutes, hours |
// and seconds | // and seconds |
|
|
sprintf(intervalBuffer, "%2.2s", startDateTimeCString + 12); | sprintf(intervalBuffer, "%2.2s", startDateTimeCString + 12); |
startIntervalSeconds = atoi(intervalBuffer); | startIntervalSeconds = atoi(intervalBuffer); |
| |
|
// Extract the Microseconds |
|
sprintf(intervalBuffer, "%6.6s", startDateTimeCString + 15); |
|
startIntervalMicroseconds = atoi(intervalBuffer); |
|
|
// Parse the finish time interval and get the days, minutes, hours | // Parse the finish time interval and get the days, minutes, hours |
// and seconds | // and seconds |
| |
|
|
sprintf(intervalBuffer, "%2.2s", finishDateTimeCString + 12); | sprintf(intervalBuffer, "%2.2s", finishDateTimeCString + 12); |
finishIntervalSeconds = atoi(intervalBuffer); | finishIntervalSeconds = atoi(intervalBuffer); |
| |
|
// Extract the Microseconds |
|
sprintf(intervalBuffer, "%6.6s", finishDateTimeCString + 15); |
|
finishIntervalMicroseconds = atoi(intervalBuffer); |
|
|
// Convert all values to seconds and compute the start and finish | // Convert all values to seconds and compute the start and finish |
// intervals in seconds. | // intervals in seconds. |
startIntervalInSeconds = (Uint64)((startIntervalDays*86400) + |
startIntervalInMicroseconds = |
(startIntervalHours*3600) + |
(Uint64)((startIntervalDays*86400000000) + |
(startIntervalMinutes*60) + |
(startIntervalHours*3600000000) + |
startIntervalSeconds) ; |
(startIntervalMinutes*60000000) + |
|
(startIntervalSeconds*1000000) + |
finishIntervalInSeconds = (Uint64)((finishIntervalDays*86400) + |
startIntervalMicroseconds); |
(finishIntervalHours*3600) + |
|
(finishIntervalMinutes*60) + |
finishIntervalInMicroseconds = |
finishIntervalSeconds) ; |
(Uint64)((finishIntervalDays*86400000000) + |
|
(finishIntervalHours*3600000000) + |
|
(finishIntervalMinutes*60000000) + |
|
(finishIntervalSeconds*1000000) + |
|
finishIntervalMicroseconds); |
| |
// Get the difference. | // Get the difference. |
intervalDifferenceInSeconds =(Sint64)(finishIntervalInSeconds - |
intervalDifferenceInMicroseconds = |
startIntervalInSeconds); |
(Sint64)(finishIntervalInMicroseconds - |
|
startIntervalInMicroseconds); |
| |
return intervalDifferenceInSeconds; |
return intervalDifferenceInMicroseconds; |
} | } |
else if ( startTime.isInterval() || finishTime.isInterval() ) | else if ( startTime.isInterval() || finishTime.isInterval() ) |
{ | { |
|
|
// | // |
differenceInSeconds = (Sint64) difftime( timeFinishInSeconds, timeStartInSeconds ); | differenceInSeconds = (Sint64) difftime( timeFinishInSeconds, timeStartInSeconds ); |
| |
|
// |
|
// ATTN-CAKG-P1-20020816: struct tm and difftime don't handle microseconds |
|
// |
|
Sint64 differenceInMicroseconds = differenceInSeconds * 1000000; |
|
startDateTimeCString = startTime._rep->data; |
|
finishDateTimeCString = finishTime._rep->data; |
|
char dateBuffer [9]; |
|
sprintf (dateBuffer, "%6.6s", startDateTimeCString + 15); |
|
Uint32 startDateMicroseconds = atoi (dateBuffer); |
|
sprintf (dateBuffer, "%6.6s", finishDateTimeCString + 15); |
|
Uint32 finishDateMicroseconds = atoi (dateBuffer); |
|
(finishDateMicroseconds > startDateMicroseconds) ? |
|
differenceInMicroseconds += |
|
(finishDateMicroseconds - startDateMicroseconds) : |
|
differenceInMicroseconds -= |
|
(startDateMicroseconds - finishDateMicroseconds); |
|
|
delete []dateTimeOnly; | delete []dateTimeOnly; |
return differenceInSeconds; |
return differenceInMicroseconds; |
} | } |
| |
PEGASUS_NAMESPACE_END | PEGASUS_NAMESPACE_END |