1 /*------------------------------------------------------------------------------
  2 Name:      Timestamp.cpp
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Create unique timestamp
  6 Version:   $Id: Timestamp.cpp 16506 2007-10-10 23:00:09Z ruff $
  7 ------------------------------------------------------------------------------*/
  8 
  9 #include <util/Timestamp.h>
 10 #include <util/Constants.h>
 11 #include <util/lexical_cast.h>
 12 #include <time.h>
 13 
 14 namespace org { namespace xmlBlaster { namespace util {
 15 
 16 using namespace std;
 17 using namespace org::xmlBlaster::util::thread;
 18 
 19 TimestampFactory::TimestampFactory() : getterMutex_()
 20 {
 21    lastTimestamp_  = 0;
 22 }
 23 
 24 TimestampFactory::TimestampFactory(const TimestampFactory &factory)
 25    : getterMutex_()
 26 {
 27    lastTimestamp_  = factory.lastTimestamp_;
 28 }
 29 
 30 TimestampFactory& TimestampFactory::operator =(const TimestampFactory &factory)
 31 {
 32    lastTimestamp_  = factory.lastTimestamp_;
 33    return *this;
 34 }
 35 
 36 
 37 TimestampFactory::~TimestampFactory()
 38 {
 39 }
 40 
 41 TimestampFactory& TimestampFactory::getInstance()
 42 {
 43    static TimestampFactory timestamp;
 44    return timestamp;
 45 }
 46 
 47 
 48 Timestamp TimestampFactory::getTimestamp()
 49 {
 50    Lock lock(getterMutex_);
 51    Timestamp timestamp = Thread::getCurrentTimestamp();
 52    if (timestamp <= lastTimestamp_) {
 53       timestamp = lastTimestamp_;
 54       timestamp++;
 55    }
 56 
 57    lastTimestamp_ = timestamp;
 58    return timestamp;
 59 }
 60 
 61 string TimestampFactory::toXml(Timestamp timestamp, const string& extraOffset, bool literal)
 62 {
 63    string ret;
 64    string offset = "\n ";
 65    offset += extraOffset;
 66    if (literal) {
 67       // implement it here ....
 68       ret += offset + "<timestamp nanos='" + lexical_cast<std::string>(timestamp) + "'>";
 69       ret += offset + " " + getTimeAsString(timestamp);
 70       ret += offset + "</timestamp>";
 71    }
 72    else {
 73       ret += offset + "<timestamp nanos='" + lexical_cast<std::string>(timestamp) + "'/>";
 74    }
 75    return ret;
 76 }
 77 
 78 string TimestampFactory::getTimeAsString(Timestamp timestamp)
 79 {
 80     time_t secs = (time_t)(timestamp / Constants::BILLION);
 81     Timestamp nanos = timestamp % Constants::BILLION;
 82 
 83     struct tm* help = gmtime(&secs);
 84 
 85     string ret;
 86     char *ptr = new char[300];
 87 
 88     try {
 89        /* size_t nmax = */ strftime(ptr, 300, "%Y-%m-%d %H:%M:%S", help);
 90        ret = string(ptr) + "." + lexical_cast<std::string>(nanos);
 91        delete[] ptr;
 92     }
 93     catch(...) {
 94        delete[] ptr;
 95     }
 96     return ret;
 97 }
 98 
 99 }}} // namespace
100 
101 
102 #ifdef _XMLBLASTER_CLASSTEST
103 
104 using namespace std;
105 using namespace org::xmlBlaster::util;
106 
107 int main(int args, char* argv[])
108 {
109    TimestampFactory& factory = TimestampFactory::getInstance();
110 
111    Timestamp timestamp = factory.getTimestamp();
112    cout << "raw timestamp: " << timestamp << endl;
113    string ret = factory.toXml(timestamp, "  ", true);
114    cout << "as xml: " << endl << ret << endl;
115 
116    Thread::sleepSecs(3);
117 
118     Timestamp t2 = factory.getTimestamp();
119     cout << factory.toXml(t2, "   ", true) << endl;
120 
121     return 0;
122 }
123 
124 #endif


syntax highlighted by Code2HTML, v. 0.9.1