File: unixtime.h

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (89 lines) | stat: -rw-r--r-- 3,216 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (c) 2002-2018 Balabit
 * Copyright (c) 1998-2018 Balázs Scheidler
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */

#ifndef UNIXTIME_H_INCLUDED
#define UNIXTIME_H_INCLUDED

#include "timeutils/zoneinfo.h"
#include <stdint.h>

/*
 * This class represents a UNIX timestamp (as measured in time_t), the
 * fractions of a second (in microseconds) and an associated GMT timezone
 * offset.  In concept it is the combination of "struct timeval" and the
 * number of seconds compared to GMT.
 *
 * In concept, this is similar to WallClockTime, with the difference that
 * this represents the time in seconds since the UNIX epoch.
 *
 * In design, it is also similar to WallClockTime, the original intention
 * was to use an embedded "struct timeval", however that would enlarge the
 * LogMessage structure with a lot of padding (the struct would go from 16
 * to 24 bytes and we have 3 of these structs in LogMessage).
 */
typedef struct _UnixTime UnixTime;
struct _UnixTime
{
  gint64 ut_sec;
  guint32 ut_usec;

  /* zone offset in seconds, add this to UTC to get the time in local.  This
   * is just 32 bits, contrary to all other gmtoff variables, as we are
   * squeezed in space with this struct.  32 bit is more than enough for
   * +/-24*3600 */
  gint32 ut_gmtoff;
};

#define UNIX_TIME_INIT { -1, 0, -1 }

static inline gboolean
unix_time_is_set(const UnixTime *ut)
{
  return ut->ut_sec != -1;
}

static inline gboolean
unix_time_is_timezone_set(const UnixTime *self)
{
  return self->ut_gmtoff != -1;
}

void unix_time_unset(UnixTime *ut);
void unix_time_set_now(UnixTime *self);

void unix_time_set_timezone(UnixTime *self, gint new_gmtoff);
void unix_time_set_timezone_with_tzinfo(UnixTime *self, TimeZoneInfo *tzinfo);
void unix_time_fix_timezone(UnixTime *self, gint new_gmtoff);
void unix_time_fix_timezone_with_tzinfo(UnixTime *self, TimeZoneInfo *tzinfo);
gboolean unix_time_fix_timezone_assuming_the_time_matches_real_time(UnixTime *self);

gboolean unix_time_eq(const UnixTime *a, const UnixTime *b);
gint64 unix_time_diff_in_seconds(const UnixTime *a, const UnixTime *b);
gint64 unix_time_diff_in_msec(const UnixTime *a, const UnixTime *b);

struct timeval timeval_from_unix_time(UnixTime *ut);
UnixTime unix_time_from_unix_epoch(guint64 unix_epoch);
guint64 unix_time_to_unix_epoch(const UnixTime ut);

#endif