00001 /*------------------------------------------------------------------------------ 00002 Name: Timestamp.cpp 00003 Project: xmlBlaster.org 00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file 00005 Comment: Create unique timestamp 00006 Version: $Id: Timestamp.cpp 16506 2007-10-10 23:00:09Z ruff $ 00007 ------------------------------------------------------------------------------*/ 00008 00009 #include <util/Timestamp.h> 00010 #include <util/Constants.h> 00011 #include <util/lexical_cast.h> 00012 #include <time.h> 00013 00014 namespace org { namespace xmlBlaster { namespace util { 00015 00016 using namespace std; 00017 using namespace org::xmlBlaster::util::thread; 00018 00019 TimestampFactory::TimestampFactory() : getterMutex_() 00020 { 00021 lastTimestamp_ = 0; 00022 } 00023 00024 TimestampFactory::TimestampFactory(const TimestampFactory &factory) 00025 : getterMutex_() 00026 { 00027 lastTimestamp_ = factory.lastTimestamp_; 00028 } 00029 00030 TimestampFactory& TimestampFactory::operator =(const TimestampFactory &factory) 00031 { 00032 lastTimestamp_ = factory.lastTimestamp_; 00033 return *this; 00034 } 00035 00036 00037 TimestampFactory::~TimestampFactory() 00038 { 00039 } 00040 00041 TimestampFactory& TimestampFactory::getInstance() 00042 { 00043 static TimestampFactory timestamp; 00044 return timestamp; 00045 } 00046 00047 00048 Timestamp TimestampFactory::getTimestamp() 00049 { 00050 Lock lock(getterMutex_); 00051 Timestamp timestamp = Thread::getCurrentTimestamp(); 00052 if (timestamp <= lastTimestamp_) { 00053 timestamp = lastTimestamp_; 00054 timestamp++; 00055 } 00056 00057 lastTimestamp_ = timestamp; 00058 return timestamp; 00059 } 00060 00061 string TimestampFactory::toXml(Timestamp timestamp, const string& extraOffset, bool literal) 00062 { 00063 string ret; 00064 string offset = "\n "; 00065 offset += extraOffset; 00066 if (literal) { 00067 // implement it here .... 00068 ret += offset + "<timestamp nanos='" + lexical_cast<std::string>(timestamp) + "'>"; 00069 ret += offset + " " + getTimeAsString(timestamp); 00070 ret += offset + "</timestamp>"; 00071 } 00072 else { 00073 ret += offset + "<timestamp nanos='" + lexical_cast<std::string>(timestamp) + "'/>"; 00074 } 00075 return ret; 00076 } 00077 00078 string TimestampFactory::getTimeAsString(Timestamp timestamp) 00079 { 00080 time_t secs = (time_t)(timestamp / Constants::BILLION); 00081 Timestamp nanos = timestamp % Constants::BILLION; 00082 00083 struct tm* help = gmtime(&secs); 00084 00085 string ret; 00086 char *ptr = new char[300]; 00087 00088 try { 00089 /* size_t nmax = */ strftime(ptr, 300, "%Y-%m-%d %H:%M:%S", help); 00090 ret = string(ptr) + "." + lexical_cast<std::string>(nanos); 00091 delete[] ptr; 00092 } 00093 catch(...) { 00094 delete[] ptr; 00095 } 00096 return ret; 00097 } 00098 00099 }}} // namespace 00100 00101 00102 #ifdef _XMLBLASTER_CLASSTEST 00103 00104 using namespace std; 00105 using namespace org::xmlBlaster::util; 00106 00107 int main(int args, char* argv[]) 00108 { 00109 TimestampFactory& factory = TimestampFactory::getInstance(); 00110 00111 Timestamp timestamp = factory.getTimestamp(); 00112 cout << "raw timestamp: " << timestamp << endl; 00113 string ret = factory.toXml(timestamp, " ", true); 00114 cout << "as xml: " << endl << ret << endl; 00115 00116 Thread::sleepSecs(3); 00117 00118 Timestamp t2 = factory.getTimestamp(); 00119 cout << factory.toXml(t2, " ", true) << endl; 00120 00121 return 0; 00122 } 00123 00124 #endif