From: Christian P. <cp...@us...> - 2004-12-28 20:08:41
|
Update of /cvsroot/pclasses/pclasses2/src/System In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26758/src/System Added Files: SystemClock.posix.cpp Log Message: Added System::SystemClock class --- NEW FILE: SystemClock.posix.cpp --- /*************************************************************************** * Copyright (C) 2004 by Christian Prochnow * * cp...@se... * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU Library General Public License as * * published by the Free Software Foundation; either version 2 of the * * License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU Library General Public * * License along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "pclasses/pclasses-config.h" #include "pclasses/System/CriticalSection.h" #include "pclasses/System/SystemClock.h" #ifdef PCLASSES_HAVE_GETTIMEOFDAY # include <sys/time.h> #endif namespace P { namespace System { #ifndef PCLASSES_HAVE_LOCALTIME_R static CriticalSection localtimeCs; #endif #ifndef PCLASSES_HAVE_GMTIME_R static CriticalSection gmtimeCs; #endif const std::string& get_tz_name(struct tm* tztime) { static bool tz_name_Init = false; static std::string tz_names[3]; /* init timezone names copying it from libc's external variable */ if(!tz_name_Init) { static CriticalSection tz_name_InitCs; CriticalSection::ScopedLock lock(tz_name_InitCs); tz_name_Init = true; tz_names[0] = tzname[0]; tz_names[1] = tzname[1]; tz_names[2] = "Unknown"; } if(tztime->tm_isdst > 0) { /* daylight saving time */ return tz_names[1]; } else if(tztime->tm_isdst == 0) { /* no daylight saving time */ return tz_names[2]; } /* daylight saving time is unknown */ return tz_names[3]; } DateTime SystemClock::now(NowMode mode) { std::string timezone_name; time_t secs, usecs; struct tm currtm, *currtmp; #ifdef PCLASSES_HAVE_CLOCK_GETTIME { struct timespec tp; clock_gettime(CLOCK_REALTIME, &tp); secs = tp.tv_sec; usecs = tp.tv_nsec / 1000; } #elif defined(PCLASSES_HAVE_GETTIMEOFDAY) { struct timeval tv; gettimeofday(&tv, 0); secs = tv.tv_sec; usecs = tv.tv_usec; } #else { secs = time(0); usecs = 0; } #endif switch(mode) { case Local: #ifdef PCLASSES_HAVE_LOCALTIME_R { currtmp = localtime_r(&secs, &currtm); if(!currtmp) return DateTime(); } #else { CriticalSection::ScopedLock l(localtimeCs); currtmp = localtime(&secs); if(!currtmp) return DateTime(); currtm = *currtmp; currtmp = &currtm; } #endif timezone_name = get_tz_name(currtmp); break; case UTC: #ifdef PCLASSES_HAVE_GMTIME_R { currtmp = gmtime_r(&secs, &currtm); if(!currtmp) return DateTime(); } #else { CriticalSection::ScopedLock l(gmtimeCs); currtmp = gmtime(&secs); if(!currtmp) return DateTime(); currtm = *currtmp; currtmp = &currtm; } #endif timezone_name = "UTC"; break; } return DateTime(Date(currtmp->tm_year + 1900, currtmp->tm_mon + 1, currtmp->tm_mday), Time(currtmp->tm_hour, currtmp->tm_min, currtmp->tm_sec, usecs), timezone_name); } std::string SystemClock::timeZone() { time_t timet = time(0); struct tm* currtmp; #ifdef PCLASSES_HAVE_LOCALTIME_R struct tm currtm; currtmp = localtime_r(&timet, &currtm); #else CriticalSection::ScopedLock l(localtimeCs); currtmp = localtime(&timet); #endif return get_tz_name(currtmp); } } // !namespace System } // !namespace P |