Menu

[r1320]: / advanced / lib / libutil / time.c  Maximize  Restore  History

Download this file

174 lines (129 with data), 4.5 kB

  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
173
#include "xmlrpc_config.h"
#include <assert.h>
#include <time.h>
#if !MSVCRT
#include <sys/time.h>
#endif
#if MSVCRT
#include <windows.h>
#endif
#include "xmlrpc-c/string_int.h"
#include "xmlrpc-c/time_int.h"
/* A note about struct timeval and Windows: There is a 'struct
timeval' type in Windows, but it is just an argument to select(),
which is just part of the sockets interface. It's defined
identically to the POSIX type of the same name, but not meant for
general timekeeping as the POSIX type is.
*/
#if HAVE_GETTIMEOFDAY
static void
gettimeofdayPosix(xmlrpc_timespec * const todP) {
struct timeval tv;
gettimeofday(&tv, NULL);
todP->tv_sec = tv.tv_sec;
todP->tv_nsec = tv.tv_usec * 1000;
}
#endif
#if MSVCRT
static void
gettimeofdayWindows(xmlrpc_timespec * const todP) {
__int64 const epochOffset = 116444736000000000i64;
/* Number of 100-nanosecond units between the beginning of the
Windows epoch (Jan. 1, 1601) and the Unix epoch (Jan. 1, 1970).
*/
FILETIME ft;
LARGE_INTEGER li;
__int64 t;
GetSystemTimeAsFileTime(&ft);
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;
t = (li.QuadPart - epochOffset) * 100; /* nanoseconds */
todP->tv_sec = (long)(t / 1E9);
todP->tv_nsec = (long)(t - (__int64)todP->tv_sec * 1E9);
}
#endif
void
xmlrpc_gettimeofday(xmlrpc_timespec * const todP) {
assert(todP);
#if HAVE_GETTIMEOFDAY
gettimeofdayPosix(todP);
#else
#if MSVCRT
gettimeofdayWindows(todP);
#else
#error "We don't know how to get the time of day on this system"
#endif
#endif /* HAVE_GETTIMEOFDAY */
}
static bool
isLeapYear(unsigned int const yearOfAd) {
return
(yearOfAd % 4) == 0 &&
((yearOfAd % 100) != 0 || (yearOfAd % 400) == 0);
}
void
xmlrpc_timegm(const struct tm * const tmP,
time_t * const timeValueP,
const char ** const errorP) {
/*----------------------------------------------------------------------------
This does what GNU libc's timegm() does.
-----------------------------------------------------------------------------*/
if (tmP->tm_year < 70 ||
tmP->tm_mon > 11 ||
tmP->tm_mon < 0 ||
tmP->tm_mday > 31 ||
tmP->tm_min > 60 ||
tmP->tm_sec > 60 ||
tmP->tm_hour > 24) {
xmlrpc_asprintf(errorP, "Invalid time specification; a member "
"of struct tm is out of range");
} else {
static unsigned int const monthDaysNonLeap[12] =
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
unsigned int totalDays;
unsigned int year;
unsigned int month;
totalDays = 0; /* initial value */
for (year = 70; year < (unsigned int)tmP->tm_year; ++year)
totalDays += isLeapYear(1900 + year) ? 366 : 365;
for (month = 0; month < (unsigned int)tmP->tm_mon; ++month)
totalDays += monthDaysNonLeap[month];
if (tmP->tm_mon > 1 && isLeapYear(1900 + tmP->tm_year))
totalDays += 1;
totalDays += tmP->tm_mday - 1;
*errorP = NULL;
*timeValueP = ((totalDays * 24 +
tmP->tm_hour) * 60 +
tmP->tm_min) * 60 +
tmP->tm_sec;
}
}
void
xmlrpc_localtime(time_t const datetime,
struct tm * const tmP) {
/*----------------------------------------------------------------------------
Convert datetime from standard to broken-down format in the local
time zone.
For Windows, this is not thread-safe. If you run a version of Abyss
with multiple threads, you can get arbitrary results here.
-----------------------------------------------------------------------------*/
#if HAVE_LOCALTIME_R
localtime_r(&datetime, tmP);
#else
*tmP = *localtime(&datetime);
#endif
}
void
xmlrpc_gmtime(time_t const datetime,
struct tm * const resultP) {
/*----------------------------------------------------------------------------
Convert datetime from standard to broken-down UTC format.
For Windows, this is not thread-safe. If you run a version of Abyss
with multiple threads, you can get arbitrary results here.
-----------------------------------------------------------------------------*/
#if HAVE_GMTIME_R
gmtime_r(&datetime, resultP);
#else
*resultP = *gmtime(&datetime);
#endif
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.