Timestamp.cpp
Go to the documentation of this file.
00001 //
00002 // Timestamp.cpp
00003 //
00004 // $Id: //poco/1.3/Foundation/src/Timestamp.cpp#2 $
00005 //
00006 // Library: Foundation
00007 // Package: DateTime
00008 // Module:  Timestamp
00009 //
00010 // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
00011 // and Contributors.
00012 //
00013 // Permission is hereby granted, free of charge, to any person or organization
00014 // obtaining a copy of the software and accompanying documentation covered by
00015 // this license (the "Software") to use, reproduce, display, distribute,
00016 // execute, and transmit the Software, and to prepare derivative works of the
00017 // Software, and to permit third-parties to whom the Software is furnished to
00018 // do so, all subject to the following:
00019 // 
00020 // The copyright notices in the Software and this entire statement, including
00021 // the above license grant, this restriction and the following disclaimer,
00022 // must be included in all copies of the Software, in whole or in part, and
00023 // all derivative works of the Software, unless such copies or derivative
00024 // works are solely in the form of machine-executable object code generated by
00025 // a source language processor.
00026 // 
00027 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00028 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00029 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
00030 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
00031 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
00032 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00033 // DEALINGS IN THE SOFTWARE.
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; // UNIX epoch (1970-01-01 00:00:00) expressed in Windows NT FILETIME
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; // UNIX epoch (1970-01-01 00:00:00) expressed in Windows NT FILETIME
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; // UNIX epoch (1970-01-01 00:00:00) expressed in Windows NT FILETIME
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 } // namespace Poco


pluginlib
Author(s): Tully Foote and Eitan Marder-Eppstein
autogenerated on Sat Dec 28 2013 17:20:19