Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "Poco/Timestamp.h"
00038 #include "Poco/Exception.h"
00039 #include <algorithm>
00040 #if defined(POCO_OS_FAMILY_UNIX)
00041 #include <time.h>
00042 #include <unistd.h>
00043 #include <sys/time.h>
00044 #include <sys/times.h>
00045 #elif defined(POCO_OS_FAMILY_WINDOWS)
00046 #include "Poco/UnWindows.h"
00047 #endif
00048
00049
00050 namespace Poco {
00051
00052
00053 Timestamp::Timestamp()
00054 {
00055 update();
00056 }
00057
00058
00059 Timestamp::Timestamp(TimeVal tv)
00060 {
00061 _ts = tv;
00062 }
00063
00064
00065 Timestamp::Timestamp(const Timestamp& other)
00066 {
00067 _ts = other._ts;
00068 }
00069
00070
00071 Timestamp::~Timestamp()
00072 {
00073 }
00074
00075
00076 Timestamp& Timestamp::operator = (const Timestamp& other)
00077 {
00078 _ts = other._ts;
00079 return *this;
00080 }
00081
00082
00083 Timestamp& Timestamp::operator = (TimeVal tv)
00084 {
00085 _ts = tv;
00086 return *this;
00087 }
00088
00089
00090 void Timestamp::swap(Timestamp& timestamp)
00091 {
00092 std::swap(_ts, timestamp._ts);
00093 }
00094
00095
00096 Timestamp Timestamp::fromEpochTime(std::time_t t)
00097 {
00098 return Timestamp(TimeVal(t)*resolution());
00099 }
00100
00101
00102 Timestamp Timestamp::fromUtcTime(UtcTimeVal val)
00103 {
00104 val -= (TimeDiff(0x01b21dd2) << 32) + 0x13814000;
00105 val /= 10;
00106 return Timestamp(val);
00107 }
00108
00109
00110 void Timestamp::update()
00111 {
00112 #if defined(POCO_OS_FAMILY_WINDOWS)
00113
00114 FILETIME ft;
00115 GetSystemTimeAsFileTime(&ft);
00116 ULARGE_INTEGER epoch;
00117 epoch.LowPart = 0xD53E8000;
00118 epoch.HighPart = 0x019DB1DE;
00119
00120 ULARGE_INTEGER ts;
00121 ts.LowPart = ft.dwLowDateTime;
00122 ts.HighPart = ft.dwHighDateTime;
00123 ts.QuadPart -= epoch.QuadPart;
00124 _ts = ts.QuadPart/10;
00125
00126 #else
00127
00128 struct timeval tv;
00129 if (gettimeofday(&tv, NULL))
00130 throw SystemException("cannot get time of day");
00131 _ts = TimeVal(tv.tv_sec)*resolution() + tv.tv_usec;
00132
00133 #endif
00134 }
00135
00136
00137 #if defined(_WIN32)
00138
00139
00140 Timestamp Timestamp::fromFileTimeNP(UInt32 fileTimeLow, UInt32 fileTimeHigh)
00141 {
00142 ULARGE_INTEGER epoch;
00143 epoch.LowPart = 0xD53E8000;
00144 epoch.HighPart = 0x019DB1DE;
00145
00146 ULARGE_INTEGER ts;
00147 ts.LowPart = fileTimeLow;
00148 ts.HighPart = fileTimeHigh;
00149 ts.QuadPart -= epoch.QuadPart;
00150
00151 return Timestamp(ts.QuadPart/10);
00152 }
00153
00154
00155 void Timestamp::toFileTimeNP(UInt32& fileTimeLow, UInt32& fileTimeHigh) const
00156 {
00157 ULARGE_INTEGER epoch;
00158 epoch.LowPart = 0xD53E8000;
00159 epoch.HighPart = 0x019DB1DE;
00160
00161 ULARGE_INTEGER ts;
00162 ts.QuadPart = _ts*10;
00163 ts.QuadPart += epoch.QuadPart;
00164 fileTimeLow = ts.LowPart;
00165 fileTimeHigh = ts.HighPart;
00166 }
00167
00168
00169 #endif
00170
00171
00172
00173 }