summaryrefslogtreecommitdiff
path: root/mbglib/common/timeutil.c
blob: 37b29e0ae9cab32f577b0434c46396d6fe5df92a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172

/**************************************************************************
 *
 *  $Id: timeutil.c 1.1.1.5 2017/04/10 12:37:46 martin TEST $
 *
 *  Copyright (c) Meinberg Funkuhren, Bad Pyrmont, Germany
 *
 *  Description:
 *    Meinberg Library module for safe time conversion routines.
 *
 * -----------------------------------------------------------------------
 *  $Log: timeutil.c $
 *  Revision 1.1.1.5  2017/04/10 12:37:46  martin
 *  Fixed some compiler warnings.
 *  Revision 1.1.1.4  2017/02/06 11:45:57Z  martin
 *  Fixed build under Windows, and implemented mbg_clock_settime(),
 *  but system time is actually not yet set.
 *  Revision 1.1.1.3  2016/11/21 15:22:37Z  martin
 *  Revision 1.1.1.2  2016/08/11 15:09:46Z  martin
 *  *** empty log message ***
 *  Revision 1.1.1.1  2016/08/11 07:54:40  martin
 *  Fixed build with old Visual Studio.
 *  Revision 1.1  2016/07/15 14:14:19Z  martin
 *  Initial revision
 *
 **************************************************************************/

#define _TIMEUTIL
 #include <timeutil.h>
#undef _TIMEUTIL

#include <str_util.h>
#include <mbgerror.h>

#if defined( MBG_TGT_WIN32 )
  #include <stdio.h>
#endif



/*HDR*/
size_t snprint_gmtime_error( char *s, size_t max_len, int mbg_errno, time_t t, const char *calling_fnc )
{
  size_t n = snprintf_safe( s, max_len, "gmtime() call failed" );

  if ( calling_fnc )
    n += snprintf_safe( &s[n], max_len - n, " in %s", calling_fnc );

  #if defined( _MSC_VER ) && ( _MSC_VER < 1500 )
    //### TODO E.g. in VC6 time_t is only 32 bit anyway.
    n += snprintf_safe( &s[n], max_len - n, " for time_t %lu: %s",
                        (unsigned long) t, mbg_strerror( mbg_errno ) );
  #else
    n += snprintf_safe( &s[n], max_len - n, " for time_t %llu: %s",
                        (unsigned long long) t, mbg_strerror( mbg_errno ) );
  #endif

  return n;

}  // snprint_gmtime_error



#if defined( MBG_TGT_WIN32 )

typedef int clockid_t;
#define clockid_t clockid_t

#define CLOCK_REALTIME  ( (clockid_t) 0 )

/*HDR*/
int mbg_clock_gettime( clockid_t clock_id, struct timespec *tp )
{
  if ( clock_id == CLOCK_REALTIME )
  {
    #if defined( TIME_UTC )  // C11 / VS2015+
      int rc = timespec_get( tp, TIME_UTC );  // TODO Check this code
      return ( rc == 0 ) ? -1 : 0   // rc == 0 means error
    #else
      #define EPOCH_HNS     116444736000000000i64
      #define NSEC_PER_SEC          1000000000i64
      FILETIME ft;
      unsigned __int64 tmp;
      gstaft_fnc( &ft );
      tmp = ( (__int64) ft.dwHighDateTime << 32 ) | ft.dwLowDateTime;
      tmp -= EPOCH_HNS;   // convert to Unix epoch
      tmp *= 100;         // convert to nanoseconds
      tp->tv_sec = ( tmp / NSEC_PER_SEC );
      tp->tv_nsec = ( tmp % NSEC_PER_SEC );
      return 0;
    #endif
  }
  else
    return -1;    // TODO this is e.g. in case of CLOCK_MONOTONIC, we could use QPC then.

}  // mbg_clock_gettime



/*HDR*/
int mbg_clock_settime( clockid_t clock_id, const struct timespec *tp )
{
  if ( clock_id == CLOCK_REALTIME )
  {
#if 0  // ### TODO FIXME This needs to be implemented.
    #if defined( TIME_UTC )  // C11 / VS2015+
      int rc = timespec_get( res, TIME_UTC );  // TODO Check this code
      return ( rc == 0 ) ? -1 : 0   // rc == 0 means error
    #else
      #define EPOCH_HNS     116444736000000000i64
      #define NSEC_PER_SEC          1000000000i64
      FILETIME ft;
      unsigned __int64 tmp;
      gstaft_fnc( &ft );
      tmp = ( (__int64) ft.dwHighDateTime << 32 ) | ft.dwLowDateTime;
      tmp -= EPOCH_HNS;   // convert to Unix epoch
      tmp *= 100;         // convert to nanoseconds
      res->tv_sec = ( tmp / NSEC_PER_SEC );
      res->tv_nsec = ( tmp % NSEC_PER_SEC );
      return 0;
    #endif
#endif

    return 0;  // FIXME this is actually not true
  }
  else
    return -1;    // TODO this is e.g. in case of CLOCK_MONOTONIC, we could use QPC then.

}  // mbg_clock_settime



bool force_legacy_gstaft; 


/*HDR*/
void check_precise_time_api( void )
{
  const char *info ="";
  GSTAFT_FNC tmp_fnc;
  HINSTANCE h = LoadLibrary( "kernel32.dll" );

  if ( h == NULL )
  {
    info = "Precise system time may not be supported; failed to get handle for kernel32.dll.";
    goto out;
  }

  tmp_fnc = (GSTAFT_FNC) GetProcAddress( h, "GetSystemTimePreciseAsFileTime" );

  if ( tmp_fnc == NULL )
  {
    info = "Precise system time NOT supported";
    goto out;
  }

  if ( force_legacy_gstaft )
  {
    info = "Precise system time is supported, but legacy function used by request";
    goto out;
  }

  gstaft_fnc = tmp_fnc;
  info = "Precise system time is supported and used";

out:
  printf( "%s\n", info );

}  // check_precise_time_api

#endif