syslog-ng source
unixtime.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002-2018 Balabit
3  * Copyright (c) 1998-2018 Balázs Scheidler
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * As an additional exemption you are allowed to compile & link against the
20  * OpenSSL libraries as published by the OpenSSL project. See the file
21  * COPYING for details.
22  *
23  */
24 
25 #ifndef UNIXTIME_H_INCLUDED
26 #define UNIXTIME_H_INCLUDED
27 
28 #include "timeutils/zoneinfo.h"
29 #include <stdint.h>
30 
31 /*
32  * This class represents a UNIX timestamp (as measured in time_t), the
33  * fractions of a second (in microseconds) and an associated GMT timezone
34  * offset. In concept it is the combination of "struct timeval" and the
35  * number of seconds compared to GMT.
36  *
37  * In concept, this is similar to WallClockTime, with the difference that
38  * this represents the time in seconds since the UNIX epoch.
39  *
40  * In design, it is also similar to WallClockTime, the original intention
41  * was to use an embedded "struct timeval", however that would enlarge the
42  * LogMessage structure with a lot of padding (the struct would go from 16
43  * to 24 bytes and we have 3 of these structs in LogMessage).
44  */
45 typedef struct _UnixTime UnixTime;
46 struct _UnixTime
47 {
48  gint64 ut_sec;
49  guint32 ut_usec;
50 
51  /* zone offset in seconds, add this to UTC to get the time in local. This
52  * is just 32 bits, contrary to all other gmtoff variables, as we are
53  * squeezed in space with this struct. 32 bit is more than enough for
54  * +/-24*3600 */
55  gint32 ut_gmtoff;
56 };
57 
58 #define UNIX_TIME_INIT { -1, 0, -1 }
59 
60 static inline gboolean
61 unix_time_is_set(const UnixTime *ut)
62 {
63  return ut->ut_sec != -1;
64 }
65 
66 static inline gboolean
67 unix_time_is_timezone_set(const UnixTime *self)
68 {
69  return self->ut_gmtoff != -1;
70 }
71 
72 void unix_time_unset(UnixTime *ut);
73 void unix_time_set_now(UnixTime *self);
74 
75 void unix_time_set_timezone(UnixTime *self, gint new_gmtoff);
76 void unix_time_set_timezone_with_tzinfo(UnixTime *self, TimeZoneInfo *tzinfo);
77 void unix_time_fix_timezone(UnixTime *self, gint new_gmtoff);
78 void unix_time_fix_timezone_with_tzinfo(UnixTime *self, TimeZoneInfo *tzinfo);
80 
81 gboolean unix_time_eq(const UnixTime *a, const UnixTime *b);
82 gint64 unix_time_diff_in_seconds(const UnixTime *a, const UnixTime *b);
83 gint64 unix_time_diff_in_msec(const UnixTime *a, const UnixTime *b);
84 
85 struct timeval timeval_from_unix_time(UnixTime *ut);
86 UnixTime unix_time_from_unix_epoch(guint64 unix_epoch);
87 guint64 unix_time_to_unix_epoch(const UnixTime ut);
88 
89 #endif
Definition: unixtime.h:47
guint32 ut_usec
Definition: unixtime.h:49
gint32 ut_gmtoff
Definition: unixtime.h:55
gint64 ut_sec
Definition: unixtime.h:48
struct @94::@97 tzinfo
struct timeval timeval_from_unix_time(UnixTime *ut)
Definition: unixtime.c:344
void unix_time_unset(UnixTime *ut)
Definition: unixtime.c:33
void unix_time_set_timezone_with_tzinfo(UnixTime *self, TimeZoneInfo *tzinfo)
Definition: unixtime.c:189
void unix_time_fix_timezone(UnixTime *self, gint new_gmtoff)
Definition: unixtime.c:166
gint64 unix_time_diff_in_msec(const UnixTime *a, const UnixTime *b)
Definition: unixtime.c:329
gboolean unix_time_fix_timezone_assuming_the_time_matches_real_time(UnixTime *self)
Definition: unixtime.c:196
gint64 unix_time_diff_in_seconds(const UnixTime *a, const UnixTime *b)
Definition: unixtime.c:314
UnixTime unix_time_from_unix_epoch(guint64 unix_epoch)
Definition: unixtime.c:355
void unix_time_set_now(UnixTime *self)
Definition: unixtime.c:40
void unix_time_fix_timezone_with_tzinfo(UnixTime *self, TimeZoneInfo *tzinfo)
Definition: unixtime.c:205
gboolean unix_time_eq(const UnixTime *a, const UnixTime *b)
Definition: unixtime.c:305
void unix_time_set_timezone(UnixTime *self, gint new_gmtoff)
Definition: unixtime.c:180
guint64 unix_time_to_unix_epoch(const UnixTime ut)
Definition: unixtime.c:367