LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/sal/osl/unx - time.c (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 36 73 49.3 %
Date: 2013-07-09 Functions: 5 7 71.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : 
      21             : #include "system.h"
      22             : 
      23             : #include <osl/diagnose.h>
      24             : #include <osl/time.h>
      25             : #include <time.h>
      26             : #include <assert.h>
      27             : #include <unistd.h>
      28             : 
      29             : #ifdef __MACH__
      30             : #include <mach/clock.h>
      31             : #include <mach/mach.h>
      32             : #endif
      33             : 
      34             : /* FIXME: detection should be done in configure script */
      35             : #if defined(MACOSX) || defined(IOS) || defined(FREEBSD) || defined(NETBSD) || \
      36             :     defined(LINUX) || defined(OPENBSD) || defined(DRAGONFLY)
      37             : #define STRUCT_TM_HAS_GMTOFF 1
      38             : 
      39             : #elif defined(SOLARIS)
      40             : #define HAS_ALTZONE 1
      41             : #endif
      42             : 
      43             : #ifdef __MACH__
      44             : typedef mach_timespec_t osl_time_t;
      45             : #else
      46             : #if defined(_POSIX_TIMERS)
      47             : #define USE_CLOCK_GETTIME
      48             : typedef struct timespec osl_time_t;
      49             : #else
      50             : typedef struct timeval osl_time_t;
      51             : #endif
      52             : #endif
      53             : static osl_time_t startTime;
      54             : 
      55             : 
      56             : /*--------------------------------------------------
      57             :  * osl_getSystemTime
      58             :  *-------------------------------------------------*/
      59             : 
      60        3206 : sal_Bool SAL_CALL osl_getSystemTime(TimeValue* tv)
      61             : {
      62             : #ifdef __MACH__
      63             :     clock_serv_t cclock;
      64             :     mach_timespec_t mts;
      65             : 
      66             :     host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
      67             :     clock_get_time(cclock, &mts);
      68             :     mach_port_deallocate(mach_task_self(), cclock);
      69             : 
      70             :     tv->Seconds = mts.tv_sec;
      71             :     tv->Nanosec = mts.tv_nsec;
      72             : #else
      73             :     int res;
      74             :     osl_time_t tp;
      75             : #if defined(USE_CLOCK_GETTIME)
      76        3206 :     res = clock_gettime(CLOCK_REALTIME, &tp);
      77             : #else
      78             :     res = gettimeofday(&tp, NULL);
      79             : #endif
      80             : 
      81        3206 :     if (res != 0)
      82             :     {
      83           0 :         return sal_False;
      84             :     }
      85             : 
      86        3206 :     tv->Seconds = tp.tv_sec;
      87             :     #if defined(USE_CLOCK_GETTIME)
      88        3206 :     tv->Nanosec = tp.tv_nsec;
      89             :     #else
      90             :     tv->Nanosec = tp.tv_usec * 1000;
      91             :     #endif
      92             : #endif
      93        3206 :     return sal_True;
      94             : }
      95             : 
      96             : 
      97             : /*--------------------------------------------------
      98             :  * osl_getDateTimeFromTimeValue
      99             :  *-------------------------------------------------*/
     100             : 
     101       23288 : sal_Bool SAL_CALL osl_getDateTimeFromTimeValue( const TimeValue* pTimeVal, oslDateTime* pDateTime )
     102             : {
     103             :     struct tm *pSystemTime;
     104             :     struct tm tmBuf;
     105             :     time_t atime;
     106             : 
     107       23288 :     atime = (time_t)pTimeVal->Seconds;
     108             : 
     109             :     /* Convert time from type time_t to struct tm */
     110       23288 :     pSystemTime = gmtime_r( &atime, &tmBuf );
     111             : 
     112             : 
     113             :     /* Convert struct tm to struct oslDateTime */
     114       23288 :     if ( pSystemTime != NULL )
     115             :     {
     116       23288 :         pDateTime->NanoSeconds  =   pTimeVal->Nanosec;
     117       23288 :         pDateTime->Seconds      =   pSystemTime->tm_sec;
     118       23288 :         pDateTime->Minutes      =   pSystemTime->tm_min;
     119       23288 :         pDateTime->Hours        =   pSystemTime->tm_hour;
     120       23288 :         pDateTime->Day          =   pSystemTime->tm_mday;
     121       23288 :         pDateTime->DayOfWeek    =   pSystemTime->tm_wday;
     122       23288 :         pDateTime->Month        =   pSystemTime->tm_mon + 1;
     123       23288 :         pDateTime->Year         =   pSystemTime->tm_year  + 1900;
     124             : 
     125       23288 :         return sal_True;
     126             :     }
     127             : 
     128           0 :     return sal_False;
     129             : }
     130             : 
     131             : /*--------------------------------------------------
     132             :  * osl_getTimeValueFromDateTime
     133             :  *--------------------------------------------------*/
     134             : 
     135           0 : sal_Bool SAL_CALL osl_getTimeValueFromDateTime( const oslDateTime* pDateTime, TimeValue* pTimeVal )
     136             : {
     137             :     struct tm   aTime;
     138             :     time_t      nSeconds;
     139             : 
     140             :     /* Convert struct oslDateTime to struct tm */
     141           0 :     aTime.tm_sec  = pDateTime->Seconds;
     142           0 :     aTime.tm_min  = pDateTime->Minutes;
     143           0 :     aTime.tm_hour = pDateTime->Hours;
     144           0 :     aTime.tm_mday = pDateTime->Day;
     145           0 :     aTime.tm_wday = pDateTime->DayOfWeek;
     146             : 
     147           0 :     if ( pDateTime->Month > 0 )
     148           0 :         aTime.tm_mon = pDateTime->Month - 1;
     149             :     else
     150           0 :         return sal_False;
     151             : 
     152           0 :     aTime.tm_year = pDateTime->Year - 1900;
     153             : 
     154           0 :     aTime.tm_isdst = -1;
     155           0 :     aTime.tm_wday  = 0;
     156           0 :     aTime.tm_yday  = 0;
     157             : 
     158             :     /* Convert time to calendar value */
     159           0 :     nSeconds = mktime( &aTime );
     160             : 
     161             :     /*
     162             :      * mktime expects the struct tm to be in local timezone, so we have to adjust
     163             :      * the returned value to be timezone neutral.
     164             :      */
     165             : 
     166           0 :     if ( nSeconds != (time_t) -1 )
     167             :     {
     168             :         time_t bias;
     169             : 
     170             :         /* timezone corrections */
     171           0 :         tzset();
     172             : 
     173             : #if defined(STRUCT_TM_HAS_GMTOFF)
     174             :         /* members of struct tm are corrected by mktime */
     175           0 :         bias = 0 - aTime.tm_gmtoff;
     176             : 
     177             : #elif defined(HAS_ALTZONE)
     178             :         /* check if daylight saving time is in effect */
     179             :         bias = aTime.tm_isdst > 0 ? altzone : timezone;
     180             : #else
     181             :         /* exspect daylight saving time to be one hour */
     182             :         bias = aTime.tm_isdst > 0 ? timezone - 3600 : timezone;
     183             : #endif
     184             : 
     185           0 :         pTimeVal->Seconds = nSeconds;
     186           0 :         pTimeVal->Nanosec = pDateTime->NanoSeconds;
     187             : 
     188           0 :         if ( nSeconds > bias )
     189           0 :             pTimeVal->Seconds -= bias;
     190             : 
     191           0 :         return sal_True;
     192             :     }
     193             : 
     194           0 :     return sal_False;
     195             : }
     196             : 
     197             : 
     198             : /*--------------------------------------------------
     199             :  * osl_getLocalTimeFromSystemTime
     200             :  *--------------------------------------------------*/
     201             : 
     202       21831 : sal_Bool SAL_CALL osl_getLocalTimeFromSystemTime( const TimeValue* pSystemTimeVal, TimeValue* pLocalTimeVal )
     203             : {
     204             :     struct tm *pLocalTime;
     205             :     struct tm tmBuf;
     206             :     time_t bias;
     207             :     time_t atime;
     208             : 
     209       21831 :     atime = (time_t) pSystemTimeVal->Seconds;
     210       21831 :     pLocalTime = localtime_r( &atime, &tmBuf );
     211             : 
     212             : #if defined(STRUCT_TM_HAS_GMTOFF)
     213             :     /* members of struct tm are corrected by mktime */
     214       21831 :     bias = -pLocalTime->tm_gmtoff;
     215             : 
     216             : #elif defined(HAS_ALTZONE)
     217             :     /* check if daylight saving time is in effect */
     218             :     bias = pLocalTime->tm_isdst > 0 ? altzone : timezone;
     219             : #else
     220             :     /* expect daylight saving time to be one hour */
     221             :     bias = pLocalTime->tm_isdst > 0 ? timezone - 3600 : timezone;
     222             : #endif
     223             : 
     224       21831 :     if ( (sal_Int64) pSystemTimeVal->Seconds > bias )
     225             :     {
     226       21831 :         pLocalTimeVal->Seconds = pSystemTimeVal->Seconds - bias;
     227       21831 :         pLocalTimeVal->Nanosec = pSystemTimeVal->Nanosec;
     228             : 
     229       21831 :         return sal_True;
     230             :     }
     231             : 
     232           0 :     return sal_False;
     233             : }
     234             : 
     235             : /*--------------------------------------------------
     236             :  * osl_getSystemTimeFromLocalTime
     237             :  *--------------------------------------------------*/
     238             : 
     239           0 : sal_Bool SAL_CALL osl_getSystemTimeFromLocalTime( const TimeValue* pLocalTimeVal, TimeValue* pSystemTimeVal )
     240             : {
     241             :     struct tm *pLocalTime;
     242             :     struct tm tmBuf;
     243             :     time_t bias;
     244             :     time_t atime;
     245             : 
     246           0 :     atime = (time_t) pLocalTimeVal->Seconds;
     247             : 
     248             :     /* Convert atime, which is a local time, to it's GMT equivalent. Then, get
     249             :      * the timezone offset for the local time for the GMT equivalent time. Note
     250             :      * that we cannot directly use local time to determine the timezone offset
     251             :      * because GMT is the only reliable time that we can determine timezone
     252             :      * offset from.
     253             :      */
     254             : 
     255           0 :     atime = mktime( gmtime_r( &atime, &tmBuf ) );
     256           0 :     pLocalTime = localtime_r( &atime, &tmBuf );
     257             : 
     258             : #if defined(STRUCT_TM_HAS_GMTOFF)
     259             :     /* members of struct tm are corrected by mktime */
     260           0 :     bias = 0 - pLocalTime->tm_gmtoff;
     261             : 
     262             : #elif defined(HAS_ALTZONE)
     263             :     /* check if daylight saving time is in effect */
     264             :     bias = pLocalTime->tm_isdst > 0 ? altzone : timezone;
     265             : #else
     266             :     /* exspect daylight saving time to be one hour */
     267             :     bias = pLocalTime->tm_isdst > 0 ? timezone - 3600 : timezone;
     268             : #endif
     269             : 
     270           0 :     if ( (sal_Int64) pLocalTimeVal->Seconds + bias > 0 )
     271             :     {
     272           0 :         pSystemTimeVal->Seconds = pLocalTimeVal->Seconds + bias;
     273           0 :         pSystemTimeVal->Nanosec = pLocalTimeVal->Nanosec;
     274             : 
     275           0 :         return sal_True;
     276             :     }
     277             : 
     278           0 :     return sal_False;
     279             : }
     280             : 
     281        1735 : void sal_initGlobalTimer()
     282             : {
     283             : #ifdef __MACH__
     284             :   clock_serv_t cclock;
     285             : 
     286             :   host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
     287             :   clock_get_time(cclock, &startTime);
     288             :   mach_port_deallocate(mach_task_self(), cclock);
     289             : #else /* ! (MACOSX || IOS) */
     290             :   int res;
     291             : #if defined(USE_CLOCK_GETTIME)
     292        1735 :   res = clock_gettime(CLOCK_REALTIME, &startTime);
     293             : #else /* Ndef USE_CLOCK_GETTIME */
     294             :   res = gettimeofday( &startTime, NULL );
     295             : #endif /* NDef USE_CLOCK_GETTIME */
     296             :   assert(res == 0);
     297             :   (void) res;
     298             : #endif /* ! (MACOSX || IOS) */
     299        1735 : }
     300             : 
     301        6520 : sal_uInt32 SAL_CALL osl_getGlobalTimer()
     302             : {
     303             :     sal_uInt32 nSeconds;
     304             : 
     305             : #ifdef __MACH__
     306             :     clock_serv_t cclock;
     307             :     mach_timespec_t currentTime;
     308             : 
     309             :     host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
     310             :     clock_get_time(cclock, &currentTime);
     311             :     mach_port_deallocate(mach_task_self(), cclock);
     312             : 
     313             :     nSeconds = ( currentTime.tv_sec - startTime.tv_sec );
     314             :     nSeconds = ( nSeconds * 1000 ) + (long) (( currentTime.tv_nsec - startTime.tv_nsec) / 1000000 );
     315             : #else
     316             :     osl_time_t currentTime;
     317             :     int res;
     318             : 
     319             : #if defined(USE_CLOCK_GETTIME)
     320        6520 :     res = clock_gettime(CLOCK_REALTIME, &currentTime);
     321             : #else
     322             :     res = gettimeofday( &currentTime, NULL );
     323             : #endif
     324             :     assert(res == 0);
     325             : 
     326        6520 :     if (res != 0)
     327           0 :         return 0;
     328             : 
     329        6520 :     nSeconds = (sal_uInt32)( currentTime.tv_sec - startTime.tv_sec );
     330             : #if defined(USE_CLOCK_GETTIME)
     331        6520 :     nSeconds = ( nSeconds * 1000 ) + (long) (( currentTime.tv_nsec - startTime.tv_nsec) / 1000000 );
     332             : #else
     333             :     nSeconds = ( nSeconds * 1000 ) + (long) (( currentTime.tv_usec - startTime.tv_usec) / 1000 );
     334             : #endif
     335             : #endif
     336        6520 :     return nSeconds;
     337             : }
     338             : 
     339             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10