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

Generated by: LCOV version 1.10