Added method for retrieving the current timezone offset from UTC.

Added possibility to return day of week from Time::toDateTime()


git-svn-id: http://voip.null.ro/svn/yate@5468 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
paulc 2013-04-17 15:38:44 +00:00
parent d6fec3d6d8
commit 2bef0f9d8c
4 changed files with 48 additions and 3 deletions

View File

@ -158,6 +158,20 @@ fi
AC_SUBST(HAVE_SOCKADDR_LEN)
AC_MSG_RESULT([$have_sockaddr_len])
HAVE_GMTOFF=""
AC_MSG_CHECKING([for tm.tm_gmtoff presence])
have_gmtoff="no"
AC_TRY_COMPILE([
#include <time.h>
],[
struct tm t;
t.tm_gmtoff = 3600;
],have_gmtoff="yes")
AC_MSG_RESULT([$have_gmtoff])
if [[ "$have_gmtoff" = "yes" ]]; then
HAVE_GMTOFF="-DHAVE_GMTOFF"
fi
AC_SUBST(HAVE_GMTOFF)
HAVE_POLL=no
AC_ARG_ENABLE(poll,AC_HELP_STRING([--enable-poll],[Use poll() on sockets (default: yes)]),want_poll=$enableval,want_poll=yes)

View File

@ -113,7 +113,7 @@ Thread.o: @srcdir@/Thread.cpp $(MKDEPS) $(CINC)
$(COMPILE) @THREAD_KILL@ @HAVE_PRCTL@ -c $<
TelEngine.o: @srcdir@/TelEngine.cpp $(MKDEPS) $(CINC)
$(COMPILE) @ATOMIC_OPS@ -c $<
$(COMPILE) @ATOMIC_OPS@ @HAVE_GMTOFF@ -c $<
Client.o: @srcdir@/Client.cpp $(MKDEPS) $(CLINC)
$(COMPILE) -c $<

View File

@ -632,7 +632,8 @@ unsigned int Time::toEpoch(int year, unsigned int month, unsigned int day,
// Split a given EPOCH time into its date/time components
bool Time::toDateTime(unsigned int epochTimeSec, int& year, unsigned int& month,
unsigned int& day, unsigned int& hour, unsigned int& minute, unsigned int& sec)
unsigned int& day, unsigned int& hour, unsigned int& minute, unsigned int& sec,
unsigned int* wDay)
{
#ifdef _WINDOWS
FILETIME ft;
@ -651,6 +652,8 @@ bool Time::toDateTime(unsigned int epochTimeSec, int& year, unsigned int& month,
hour = st.wHour;
minute = st.wMinute;
sec = st.wSecond;
if (wDay)
*wDay = st.wDayOfWeek;
#else
struct tm t;
time_t time = (time_t)epochTimeSec;
@ -662,12 +665,32 @@ bool Time::toDateTime(unsigned int epochTimeSec, int& year, unsigned int& month,
hour = t.tm_hour;
minute = t.tm_min;
sec = t.tm_sec;
if (wDay)
*wDay = t.tm_wday;
#endif
DDebug(DebugAll,"Time::toDateTime(%u,%d,%u,%u,%u,%u,%u)",
epochTimeSec,year,month,day,hour,minute,sec);
return true;
}
int Time::timeZone()
{
#ifdef _WINDOWS
int diff = 0;
_get_timezone(&diff);
return -diff;
#else
#ifdef HAVE_GMTOFF
struct tm t;
time_t time = (time_t)secNow();
if (localtime_r(&time,&t))
return t.tm_gmtoff;
#endif
return -timezone;
#endif
}
static Random s_random;
static Mutex s_randomMutex(false,"Random");

View File

@ -3194,10 +3194,12 @@ public:
* @param hour The hour component of the time (0 to 23)
* @param minute The minute component of the time (0 to 59)
* @param sec The seconds component of the time (0 to 59)
* @param wDay The day of the week (optional)
* @return True on succes, false if conversion failed
*/
static bool toDateTime(unsigned int epochTimeSec, int& year, unsigned int& month,
unsigned int& day, unsigned int& hour, unsigned int& minute, unsigned int& sec);
unsigned int& day, unsigned int& hour, unsigned int& minute, unsigned int& sec,
unsigned int* wDay = 0);
/**
* Check if an year is a leap one
@ -3207,6 +3209,12 @@ public:
static inline bool isLeap(unsigned int year)
{ return (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)); }
/**
* Retrieve the difference between local time and UTC in seconds east of UTC
* @return Difference between local time and UTC in seconds
*/
static int timeZone();
private:
u_int64_t m_time;
};