LCOV - code coverage report
Current view: top level - sal/osl/unx - time.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 35 72 48.6 %
Date: 2015-06-13 12:38:46 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             : #include <sal/config.h>
      21             : 
      22             : #include "saltime.hxx"
      23             : #include "system.hxx"
      24             : 
      25             : #include <osl/diagnose.h>
      26             : #include <osl/time.h>
      27             : #include <time.h>
      28             : #include <unistd.h>
      29             : 
      30             : #ifdef __MACH__
      31             : #include <mach/clock.h>
      32             : #include <mach/mach.h>
      33             : #endif
      34             : 
      35             : /* FIXME: detection should be done in configure script */
      36             : #if defined(MACOSX) || defined(IOS) || defined(FREEBSD) || defined(NETBSD) || \
      37             :     defined(LINUX) || defined(OPENBSD) || defined(DRAGONFLY)
      38             : #define STRUCT_TM_HAS_GMTOFF 1
      39             : 
      40             : #elif defined(SOLARIS)
      41             : #define HAS_ALTZONE 1
      42             : #endif
      43             : 
      44             : #ifdef __MACH__
      45             : typedef mach_timespec_t osl_time_t;
      46             : #else
      47             : #if defined(_POSIX_TIMERS)
      48             : #define USE_CLOCK_GETTIME
      49             : typedef struct timespec osl_time_t;
      50             : #else
      51             : typedef struct timeval osl_time_t;
      52             : #endif
      53             : #endif
      54             : static osl_time_t startTime;
      55             : 
      56       17152 : sal_Bool SAL_CALL osl_getSystemTime(TimeValue* tv)
      57             : {
      58             : #ifdef __MACH__
      59             :     clock_serv_t cclock;
      60             :     mach_timespec_t mts;
      61             : 
      62             :     host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
      63             :     clock_get_time(cclock, &mts);
      64             :     mach_port_deallocate(mach_task_self(), cclock);
      65             : 
      66             :     tv->Seconds = mts.tv_sec;
      67             :     tv->Nanosec = mts.tv_nsec;
      68             : #else
      69             :     int res;
      70             :     osl_time_t tp;
      71             : #if defined(USE_CLOCK_GETTIME)
      72       17152 :     res = clock_gettime(CLOCK_REALTIME, &tp);
      73             : #else
      74             :     res = gettimeofday(&tp, NULL);
      75             : #endif
      76             : 
      77       17152 :     if (res != 0)
      78             :     {
      79           0 :         return sal_False;
      80             :     }
      81             : 
      82       17152 :     tv->Seconds = tp.tv_sec;
      83             :     #if defined(USE_CLOCK_GETTIME)
      84       17152 :     tv->Nanosec = tp.tv_nsec;
      85             :     #else
      86             :     tv->Nanosec = tp.tv_usec * 1000;
      87             :     #endif
      88             : #endif
      89       17152 :     return sal_True;
      90             : }
      91             : 
      92       59401 : sal_Bool SAL_CALL osl_getDateTimeFromTimeValue( const TimeValue* pTimeVal, oslDateTime* pDateTime )
      93             : {
      94             :     struct tm *pSystemTime;
      95             :     struct tm tmBuf;
      96             :     time_t atime;
      97             : 
      98       59401 :     atime = (time_t)pTimeVal->Seconds;
      99             : 
     100             :     /* Convert time from type time_t to struct tm */
     101       59401 :     pSystemTime = gmtime_r( &atime, &tmBuf );
     102             : 
     103             :     /* Convert struct tm to struct oslDateTime */
     104       59401 :     if ( pSystemTime != NULL )
     105             :     {
     106       59401 :         pDateTime->NanoSeconds  =   pTimeVal->Nanosec;
     107       59401 :         pDateTime->Seconds      =   pSystemTime->tm_sec;
     108       59401 :         pDateTime->Minutes      =   pSystemTime->tm_min;
     109       59401 :         pDateTime->Hours        =   pSystemTime->tm_hour;
     110       59401 :         pDateTime->Day          =   pSystemTime->tm_mday;
     111       59401 :         pDateTime->DayOfWeek    =   pSystemTime->tm_wday;
     112       59401 :         pDateTime->Month        =   pSystemTime->tm_mon + 1;
     113       59401 :         pDateTime->Year         =   pSystemTime->tm_year  + 1900;
     114             : 
     115       59401 :         return sal_True;
     116             :     }
     117             : 
     118           0 :     return sal_False;
     119             : }
     120             : 
     121           0 : sal_Bool SAL_CALL osl_getTimeValueFromDateTime( const oslDateTime* pDateTime, TimeValue* pTimeVal )
     122             : {
     123             :     struct tm   aTime;
     124             :     time_t      nSeconds;
     125             : 
     126             :     /* Convert struct oslDateTime to struct tm */
     127           0 :     aTime.tm_sec  = pDateTime->Seconds;
     128           0 :     aTime.tm_min  = pDateTime->Minutes;
     129           0 :     aTime.tm_hour = pDateTime->Hours;
     130           0 :     aTime.tm_mday = pDateTime->Day;
     131           0 :     aTime.tm_wday = pDateTime->DayOfWeek;
     132             : 
     133           0 :     if ( pDateTime->Month > 0 )
     134           0 :         aTime.tm_mon = pDateTime->Month - 1;
     135             :     else
     136           0 :         return sal_False;
     137             : 
     138           0 :     aTime.tm_year = pDateTime->Year - 1900;
     139             : 
     140           0 :     aTime.tm_isdst = -1;
     141           0 :     aTime.tm_wday  = 0;
     142           0 :     aTime.tm_yday  = 0;
     143             : 
     144             : #if defined(STRUCT_TM_HAS_GMTOFF)
     145           0 :     aTime.tm_gmtoff = 0;
     146             : #endif
     147             : 
     148             :     /* Convert time to calendar value */
     149           0 :     nSeconds = mktime( &aTime );
     150             : 
     151             :     /*
     152             :      * mktime expects the struct tm to be in local timezone, so we have to adjust
     153             :      * the returned value to be timezone neutral.
     154             :      */
     155             : 
     156           0 :     if ( nSeconds != (time_t) -1 )
     157             :     {
     158             :         time_t bias;
     159             : 
     160             :         /* timezone corrections */
     161           0 :         tzset();
     162             : 
     163             : #if defined(STRUCT_TM_HAS_GMTOFF)
     164             :         /* members of struct tm are corrected by mktime */
     165           0 :         bias = 0 - aTime.tm_gmtoff;
     166             : 
     167             : #elif defined(HAS_ALTZONE)
     168             :         /* check if daylight saving time is in effect */
     169             :         bias = aTime.tm_isdst > 0 ? altzone : timezone;
     170             : #else
     171             :         /* exspect daylight saving time to be one hour */
     172             :         bias = aTime.tm_isdst > 0 ? timezone - 3600 : timezone;
     173             : #endif
     174             : 
     175           0 :         pTimeVal->Seconds = nSeconds;
     176           0 :         pTimeVal->Nanosec = pDateTime->NanoSeconds;
     177             : 
     178           0 :         if ( nSeconds > bias )
     179           0 :             pTimeVal->Seconds -= bias;
     180             : 
     181           0 :         return sal_True;
     182             :     }
     183             : 
     184           0 :     return sal_False;
     185             : }
     186             : 
     187       47368 : sal_Bool SAL_CALL osl_getLocalTimeFromSystemTime( const TimeValue* pSystemTimeVal, TimeValue* pLocalTimeVal )
     188             : {
     189             :     struct tm *pLocalTime;
     190             :     struct tm tmBuf;
     191             :     time_t bias;
     192             :     time_t atime;
     193             : 
     194       47368 :     atime = (time_t) pSystemTimeVal->Seconds;
     195       47368 :     pLocalTime = localtime_r( &atime, &tmBuf );
     196             : 
     197             : #if defined(STRUCT_TM_HAS_GMTOFF)
     198             :     /* members of struct tm are corrected by mktime */
     199       47368 :     bias = -pLocalTime->tm_gmtoff;
     200             : 
     201             : #elif defined(HAS_ALTZONE)
     202             :     /* check if daylight saving time is in effect */
     203             :     bias = pLocalTime->tm_isdst > 0 ? altzone : timezone;
     204             : #else
     205             :     /* expect daylight saving time to be one hour */
     206             :     bias = pLocalTime->tm_isdst > 0 ? timezone - 3600 : timezone;
     207             : #endif
     208             : 
     209       47368 :     if ( (sal_Int64) pSystemTimeVal->Seconds > bias )
     210             :     {
     211       47368 :         pLocalTimeVal->Seconds = pSystemTimeVal->Seconds - bias;
     212       47368 :         pLocalTimeVal->Nanosec = pSystemTimeVal->Nanosec;
     213             : 
     214       47368 :         return sal_True;
     215             :     }
     216             : 
     217           0 :     return sal_False;
     218             : }
     219             : 
     220           0 : sal_Bool SAL_CALL osl_getSystemTimeFromLocalTime( const TimeValue* pLocalTimeVal, TimeValue* pSystemTimeVal )
     221             : {
     222             :     struct tm *pLocalTime;
     223             :     struct tm tmBuf;
     224             :     time_t bias;
     225             :     time_t atime;
     226             : 
     227           0 :     atime = (time_t) pLocalTimeVal->Seconds;
     228             : 
     229             :     /* Convert atime, which is a local time, to it's GMT equivalent. Then, get
     230             :      * the timezone offset for the local time for the GMT equivalent time. Note
     231             :      * that we cannot directly use local time to determine the timezone offset
     232             :      * because GMT is the only reliable time that we can determine timezone
     233             :      * offset from.
     234             :      */
     235             : 
     236           0 :     atime = mktime( gmtime_r( &atime, &tmBuf ) );
     237           0 :     pLocalTime = localtime_r( &atime, &tmBuf );
     238             : 
     239             : #if defined(STRUCT_TM_HAS_GMTOFF)
     240             :     /* members of struct tm are corrected by mktime */
     241           0 :     bias = 0 - pLocalTime->tm_gmtoff;
     242             : 
     243             : #elif defined(HAS_ALTZONE)
     244             :     /* check if daylight saving time is in effect */
     245             :     bias = pLocalTime->tm_isdst > 0 ? altzone : timezone;
     246             : #else
     247             :     /* exspect daylight saving time to be one hour */
     248             :     bias = pLocalTime->tm_isdst > 0 ? timezone - 3600 : timezone;
     249             : #endif
     250             : 
     251           0 :     if ( (sal_Int64) pLocalTimeVal->Seconds + bias > 0 )
     252             :     {
     253           0 :         pSystemTimeVal->Seconds = pLocalTimeVal->Seconds + bias;
     254           0 :         pSystemTimeVal->Nanosec = pLocalTimeVal->Nanosec;
     255             : 
     256           0 :         return sal_True;
     257             :     }
     258             : 
     259           0 :     return sal_False;
     260             : }
     261             : 
     262        1847 : void sal_initGlobalTimer()
     263             : {
     264             : #ifdef __MACH__
     265             :   clock_serv_t cclock;
     266             : 
     267             :   host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
     268             :   clock_get_time(cclock, &startTime);
     269             :   mach_port_deallocate(mach_task_self(), cclock);
     270             : #else /* ! (MACOSX || IOS) */
     271             : #if defined(USE_CLOCK_GETTIME)
     272        1847 :   clock_gettime(CLOCK_REALTIME, &startTime);
     273             : #else /* Ndef USE_CLOCK_GETTIME */
     274             :   gettimeofday( &startTime, NULL );
     275             : #endif /* NDef USE_CLOCK_GETTIME */
     276             : #endif /* ! (MACOSX || IOS) */
     277        1847 : }
     278             : 
     279       17202 : sal_uInt32 SAL_CALL osl_getGlobalTimer()
     280             : {
     281             :     sal_uInt32 nSeconds;
     282             : 
     283             : #ifdef __MACH__
     284             :     clock_serv_t cclock;
     285             :     mach_timespec_t currentTime;
     286             : 
     287             :     host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
     288             :     clock_get_time(cclock, &currentTime);
     289             :     mach_port_deallocate(mach_task_self(), cclock);
     290             : 
     291             :     nSeconds = ( currentTime.tv_sec - startTime.tv_sec );
     292             :     nSeconds = ( nSeconds * 1000 ) + (long) (( currentTime.tv_nsec - startTime.tv_nsec) / 1000000 );
     293             : #else
     294             :     osl_time_t currentTime;
     295             : 
     296             : #if defined(USE_CLOCK_GETTIME)
     297       17202 :     clock_gettime(CLOCK_REALTIME, &currentTime);
     298             : #else
     299             :     gettimeofday( &currentTime, NULL );
     300             : #endif
     301             : 
     302       17202 :     nSeconds = (sal_uInt32)( currentTime.tv_sec - startTime.tv_sec );
     303             : #if defined(USE_CLOCK_GETTIME)
     304       17202 :     nSeconds = ( nSeconds * 1000 ) + (long) (( currentTime.tv_nsec - startTime.tv_nsec) / 1000000 );
     305             : #else
     306             :     nSeconds = ( nSeconds * 1000 ) + (long) (( currentTime.tv_usec - startTime.tv_usec) / 1000 );
     307             : #endif
     308             : #endif
     309       17202 :     return nSeconds;
     310             : }
     311             : 
     312             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11