1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir 29*cdf0e10cSrcweir #include "system.h" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include <osl/diagnose.h> 32*cdf0e10cSrcweir #include <osl/time.h> 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir /* FIXME: detection should be done in configure script */ 35*cdf0e10cSrcweir #if defined(MACOSX) || defined(FREEBSD) || defined(NETBSD) || defined(LINUX) 36*cdf0e10cSrcweir #define STRUCT_TM_HAS_GMTOFF 1 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir #elif defined(SOLARIS) 39*cdf0e10cSrcweir #define HAS_ALTZONE 1 40*cdf0e10cSrcweir #endif 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir /*-------------------------------------------------- 43*cdf0e10cSrcweir * osl_getSystemTime 44*cdf0e10cSrcweir *-------------------------------------------------*/ 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir sal_Bool SAL_CALL osl_getSystemTime(TimeValue* TimeValue) 47*cdf0e10cSrcweir { 48*cdf0e10cSrcweir struct timeval tp; 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir /* FIXME: use higher resolution */ 51*cdf0e10cSrcweir gettimeofday(&tp, NULL); 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir TimeValue->Seconds = tp.tv_sec; 54*cdf0e10cSrcweir TimeValue->Nanosec = tp.tv_usec * 1000; 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir return (sal_True); 57*cdf0e10cSrcweir } 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir 60*cdf0e10cSrcweir /*-------------------------------------------------- 61*cdf0e10cSrcweir * osl_getDateTimeFromTimeValue 62*cdf0e10cSrcweir *-------------------------------------------------*/ 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir sal_Bool SAL_CALL osl_getDateTimeFromTimeValue( TimeValue* pTimeVal, oslDateTime* pDateTime ) 65*cdf0e10cSrcweir { 66*cdf0e10cSrcweir struct tm *pSystemTime; 67*cdf0e10cSrcweir struct tm tmBuf; 68*cdf0e10cSrcweir time_t atime; 69*cdf0e10cSrcweir 70*cdf0e10cSrcweir atime = (time_t)pTimeVal->Seconds; 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir /* Convert time from type time_t to struct tm */ 73*cdf0e10cSrcweir pSystemTime = gmtime_r( &atime, &tmBuf ); 74*cdf0e10cSrcweir 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir /* Convert struct tm to struct oslDateTime */ 77*cdf0e10cSrcweir if ( pSystemTime != NULL ) 78*cdf0e10cSrcweir { 79*cdf0e10cSrcweir pDateTime->NanoSeconds = pTimeVal->Nanosec; 80*cdf0e10cSrcweir pDateTime->Seconds = pSystemTime->tm_sec; 81*cdf0e10cSrcweir pDateTime->Minutes = pSystemTime->tm_min; 82*cdf0e10cSrcweir pDateTime->Hours = pSystemTime->tm_hour; 83*cdf0e10cSrcweir pDateTime->Day = pSystemTime->tm_mday; 84*cdf0e10cSrcweir pDateTime->DayOfWeek = pSystemTime->tm_wday; 85*cdf0e10cSrcweir pDateTime->Month = pSystemTime->tm_mon + 1; 86*cdf0e10cSrcweir pDateTime->Year = pSystemTime->tm_year + 1900; 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir return sal_True; 89*cdf0e10cSrcweir } 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir return sal_False; 92*cdf0e10cSrcweir } 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir /*-------------------------------------------------- 95*cdf0e10cSrcweir * osl_getTimeValueFromDateTime 96*cdf0e10cSrcweir *--------------------------------------------------*/ 97*cdf0e10cSrcweir 98*cdf0e10cSrcweir sal_Bool SAL_CALL osl_getTimeValueFromDateTime( oslDateTime* pDateTime, TimeValue* pTimeVal ) 99*cdf0e10cSrcweir { 100*cdf0e10cSrcweir struct tm aTime; 101*cdf0e10cSrcweir time_t nSeconds; 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir /* Convert struct oslDateTime to struct tm */ 104*cdf0e10cSrcweir aTime.tm_sec = pDateTime->Seconds; 105*cdf0e10cSrcweir aTime.tm_min = pDateTime->Minutes; 106*cdf0e10cSrcweir aTime.tm_hour = pDateTime->Hours; 107*cdf0e10cSrcweir aTime.tm_mday = pDateTime->Day; 108*cdf0e10cSrcweir aTime.tm_wday = pDateTime->DayOfWeek; 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir if ( pDateTime->Month > 0 ) 111*cdf0e10cSrcweir aTime.tm_mon = pDateTime->Month - 1; 112*cdf0e10cSrcweir else 113*cdf0e10cSrcweir return sal_False; 114*cdf0e10cSrcweir 115*cdf0e10cSrcweir if ( pDateTime->Year >= 1900 ) 116*cdf0e10cSrcweir aTime.tm_year = pDateTime->Year - 1900; 117*cdf0e10cSrcweir else 118*cdf0e10cSrcweir return sal_False; 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir aTime.tm_isdst = -1; 121*cdf0e10cSrcweir aTime.tm_wday = 0; 122*cdf0e10cSrcweir aTime.tm_yday = 0; 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir /* Convert time to calendar value */ 125*cdf0e10cSrcweir nSeconds = mktime( &aTime ); 126*cdf0e10cSrcweir 127*cdf0e10cSrcweir /* 128*cdf0e10cSrcweir * mktime expects the struct tm to be in local timezone, so we have to adjust 129*cdf0e10cSrcweir * the returned value to be timezone neutral. 130*cdf0e10cSrcweir */ 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir if ( nSeconds != (time_t) -1 ) 133*cdf0e10cSrcweir { 134*cdf0e10cSrcweir time_t bias; 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir /* timezone corrections */ 137*cdf0e10cSrcweir tzset(); 138*cdf0e10cSrcweir 139*cdf0e10cSrcweir #if defined(STRUCT_TM_HAS_GMTOFF) 140*cdf0e10cSrcweir /* members of struct tm are corrected by mktime */ 141*cdf0e10cSrcweir bias = 0 - aTime.tm_gmtoff; 142*cdf0e10cSrcweir 143*cdf0e10cSrcweir #elif defined(HAS_ALTZONE) 144*cdf0e10cSrcweir /* check if daylight saving time is in effect */ 145*cdf0e10cSrcweir bias = aTime.tm_isdst > 0 ? altzone : timezone; 146*cdf0e10cSrcweir #else 147*cdf0e10cSrcweir /* exspect daylight saving time to be one hour */ 148*cdf0e10cSrcweir bias = aTime.tm_isdst > 0 ? timezone - 3600 : timezone; 149*cdf0e10cSrcweir #endif 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir pTimeVal->Seconds = nSeconds; 152*cdf0e10cSrcweir pTimeVal->Nanosec = pDateTime->NanoSeconds; 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir if ( nSeconds > bias ) 155*cdf0e10cSrcweir pTimeVal->Seconds -= bias; 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir return sal_True; 158*cdf0e10cSrcweir } 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir return sal_False; 161*cdf0e10cSrcweir } 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir 164*cdf0e10cSrcweir /*-------------------------------------------------- 165*cdf0e10cSrcweir * osl_getLocalTimeFromSystemTime 166*cdf0e10cSrcweir *--------------------------------------------------*/ 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir sal_Bool SAL_CALL osl_getLocalTimeFromSystemTime( TimeValue* pSystemTimeVal, TimeValue* pLocalTimeVal ) 169*cdf0e10cSrcweir { 170*cdf0e10cSrcweir struct tm *pLocalTime; 171*cdf0e10cSrcweir struct tm tmBuf; 172*cdf0e10cSrcweir time_t bias; 173*cdf0e10cSrcweir time_t atime; 174*cdf0e10cSrcweir 175*cdf0e10cSrcweir atime = (time_t) pSystemTimeVal->Seconds; 176*cdf0e10cSrcweir pLocalTime = localtime_r( &atime, &tmBuf ); 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir #if defined(STRUCT_TM_HAS_GMTOFF) 179*cdf0e10cSrcweir /* members of struct tm are corrected by mktime */ 180*cdf0e10cSrcweir bias = 0 - pLocalTime->tm_gmtoff; 181*cdf0e10cSrcweir 182*cdf0e10cSrcweir #elif defined(HAS_ALTZONE) 183*cdf0e10cSrcweir /* check if daylight saving time is in effect */ 184*cdf0e10cSrcweir bias = pLocalTime->tm_isdst > 0 ? altzone : timezone; 185*cdf0e10cSrcweir #else 186*cdf0e10cSrcweir /* exspect daylight saving time to be one hour */ 187*cdf0e10cSrcweir bias = pLocalTime->tm_isdst > 0 ? timezone - 3600 : timezone; 188*cdf0e10cSrcweir #endif 189*cdf0e10cSrcweir 190*cdf0e10cSrcweir if ( (sal_Int64) pSystemTimeVal->Seconds > bias ) 191*cdf0e10cSrcweir { 192*cdf0e10cSrcweir pLocalTimeVal->Seconds = pSystemTimeVal->Seconds - bias; 193*cdf0e10cSrcweir pLocalTimeVal->Nanosec = pSystemTimeVal->Nanosec; 194*cdf0e10cSrcweir 195*cdf0e10cSrcweir return sal_True; 196*cdf0e10cSrcweir } 197*cdf0e10cSrcweir 198*cdf0e10cSrcweir return sal_False; 199*cdf0e10cSrcweir } 200*cdf0e10cSrcweir 201*cdf0e10cSrcweir /*-------------------------------------------------- 202*cdf0e10cSrcweir * osl_getSystemTimeFromLocalTime 203*cdf0e10cSrcweir *--------------------------------------------------*/ 204*cdf0e10cSrcweir 205*cdf0e10cSrcweir sal_Bool SAL_CALL osl_getSystemTimeFromLocalTime( TimeValue* pLocalTimeVal, TimeValue* pSystemTimeVal ) 206*cdf0e10cSrcweir { 207*cdf0e10cSrcweir struct tm *pLocalTime; 208*cdf0e10cSrcweir struct tm tmBuf; 209*cdf0e10cSrcweir time_t bias; 210*cdf0e10cSrcweir time_t atime; 211*cdf0e10cSrcweir 212*cdf0e10cSrcweir atime = (time_t) pLocalTimeVal->Seconds; 213*cdf0e10cSrcweir 214*cdf0e10cSrcweir /* Convert atime, which is a local time, to it's GMT equivalent. Then, get 215*cdf0e10cSrcweir * the timezone offset for the local time for the GMT equivalent time. Note 216*cdf0e10cSrcweir * that we cannot directly use local time to determine the timezone offset 217*cdf0e10cSrcweir * because GMT is the only reliable time that we can determine timezone 218*cdf0e10cSrcweir * offset from. 219*cdf0e10cSrcweir */ 220*cdf0e10cSrcweir 221*cdf0e10cSrcweir atime = mktime( gmtime_r( &atime, &tmBuf ) ); 222*cdf0e10cSrcweir pLocalTime = localtime_r( &atime, &tmBuf ); 223*cdf0e10cSrcweir 224*cdf0e10cSrcweir #if defined(STRUCT_TM_HAS_GMTOFF) 225*cdf0e10cSrcweir /* members of struct tm are corrected by mktime */ 226*cdf0e10cSrcweir bias = 0 - pLocalTime->tm_gmtoff; 227*cdf0e10cSrcweir 228*cdf0e10cSrcweir #elif defined(HAS_ALTZONE) 229*cdf0e10cSrcweir /* check if daylight saving time is in effect */ 230*cdf0e10cSrcweir bias = pLocalTime->tm_isdst > 0 ? altzone : timezone; 231*cdf0e10cSrcweir #else 232*cdf0e10cSrcweir /* exspect daylight saving time to be one hour */ 233*cdf0e10cSrcweir bias = pLocalTime->tm_isdst > 0 ? timezone - 3600 : timezone; 234*cdf0e10cSrcweir #endif 235*cdf0e10cSrcweir 236*cdf0e10cSrcweir if ( (sal_Int64) pLocalTimeVal->Seconds + bias > 0 ) 237*cdf0e10cSrcweir { 238*cdf0e10cSrcweir pSystemTimeVal->Seconds = pLocalTimeVal->Seconds + bias; 239*cdf0e10cSrcweir pSystemTimeVal->Nanosec = pLocalTimeVal->Nanosec; 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir return sal_True; 242*cdf0e10cSrcweir } 243*cdf0e10cSrcweir 244*cdf0e10cSrcweir return sal_False; 245*cdf0e10cSrcweir } 246*cdf0e10cSrcweir 247*cdf0e10cSrcweir 248*cdf0e10cSrcweir 249*cdf0e10cSrcweir static struct timeval startTime; 250*cdf0e10cSrcweir static sal_Bool bGlobalTimer = sal_False; 251*cdf0e10cSrcweir 252*cdf0e10cSrcweir sal_uInt32 SAL_CALL osl_getGlobalTimer() 253*cdf0e10cSrcweir { 254*cdf0e10cSrcweir struct timeval currentTime; 255*cdf0e10cSrcweir sal_uInt32 nSeconds; 256*cdf0e10cSrcweir 257*cdf0e10cSrcweir // FIXME: not thread safe !! 258*cdf0e10cSrcweir if ( bGlobalTimer == sal_False ) 259*cdf0e10cSrcweir { 260*cdf0e10cSrcweir gettimeofday( &startTime, NULL ); 261*cdf0e10cSrcweir bGlobalTimer=sal_True; 262*cdf0e10cSrcweir } 263*cdf0e10cSrcweir 264*cdf0e10cSrcweir gettimeofday( ¤tTime, NULL ); 265*cdf0e10cSrcweir 266*cdf0e10cSrcweir nSeconds = (sal_uInt32)( currentTime.tv_sec - startTime.tv_sec ); 267*cdf0e10cSrcweir 268*cdf0e10cSrcweir return ( nSeconds * 1000 ) + (long) (( currentTime.tv_usec - startTime.tv_usec) / 1000 ); 269*cdf0e10cSrcweir } 270