2011-11-29 12:11:01 +00:00
|
|
|
/*
|
|
|
|
* virtime.h: Time handling functions
|
|
|
|
*
|
util: new function virTimeLocalOffsetFromUTC
Since there isn't a single libc API to get this value, this patch
supplies one which gets the value by grabbing current time, then
converting that into a struct tm with gmtime_r(), then back to a
time_t using mktime.
The returned value is the difference between UTC and localtime in
seconds. If localtime is ahead of UTC (east) the offset will be a
positive number, and if localtime is behind UTC (west) the offset will
be negative.
This function should be POSIX-compliant, and is threadsafe, but not
async signal safe. If it was ever necessary to know this value in a
child process, we could cache it with a one-time init function when
libvirtd starts, then just supply the cached value, but that
complexity isn't needed for current usage; that would also have the
problem that it might not be accurate after a local daylight savings
boundary.
(If it weren't for DST, we could simply replace this entire function
with "-timezone"; timezone contains the offset of the current timezone
(negated from what we want) but doesn't account for DST. And in spite
of being guaranteed by POSIX, it isn't available on older versions of
mingw.)
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-05-24 14:21:26 +00:00
|
|
|
* Copyright (C) 2006-2011, 2014 Red Hat, Inc.
|
2011-11-29 12:11:01 +00:00
|
|
|
*
|
|
|
|
* 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
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2011-11-29 12:11:01 +00:00
|
|
|
*/
|
|
|
|
|
2019-06-18 16:13:08 +00:00
|
|
|
#pragma once
|
2011-11-29 12:11:01 +00:00
|
|
|
|
2019-06-18 16:13:08 +00:00
|
|
|
#include <time.h>
|
2011-11-29 12:11:01 +00:00
|
|
|
|
2019-06-18 16:13:08 +00:00
|
|
|
#include "internal.h"
|
2011-11-29 12:11:01 +00:00
|
|
|
|
|
|
|
/* The format string we intend to use is:
|
|
|
|
*
|
|
|
|
* Yr Mon Day Hour Min Sec Ms TZ
|
|
|
|
* %4d-%02d-%02d %02d:%02d:%02d.%03d+0000
|
|
|
|
*
|
|
|
|
*/
|
2019-06-18 16:13:08 +00:00
|
|
|
#define VIR_TIME_STRING_BUFLEN \
|
2011-11-29 12:11:01 +00:00
|
|
|
(4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 3 + 5 + 1)
|
|
|
|
/* Yr Mon Day Hour Min Sec Ms TZ NULL */
|
|
|
|
|
2014-07-25 08:13:57 +00:00
|
|
|
void virTimeFieldsThen(unsigned long long when, struct tm *fields)
|
|
|
|
ATTRIBUTE_NONNULL(2);
|
|
|
|
|
2011-11-29 12:11:01 +00:00
|
|
|
/* These APIs are async signal safe and return -1, setting
|
|
|
|
* errno on failure */
|
|
|
|
int virTimeMillisNowRaw(unsigned long long *now)
|
|
|
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
|
|
|
|
int virTimeFieldsNowRaw(struct tm *fields)
|
|
|
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
|
|
|
|
int virTimeStringNowRaw(char *buf)
|
|
|
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
|
|
|
|
int virTimeStringThenRaw(unsigned long long when, char *buf)
|
|
|
|
ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
|
|
|
|
|
|
|
|
/* These APIs are *not* async signal safe and return -1,
|
|
|
|
* raising a libvirt error on failure
|
|
|
|
*/
|
|
|
|
int virTimeMillisNow(unsigned long long *now)
|
|
|
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
|
|
|
|
int virTimeFieldsNow(struct tm *fields)
|
|
|
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
|
|
|
|
char *virTimeStringNow(void);
|
|
|
|
char *virTimeStringThen(unsigned long long when);
|
|
|
|
|
util: new function virTimeLocalOffsetFromUTC
Since there isn't a single libc API to get this value, this patch
supplies one which gets the value by grabbing current time, then
converting that into a struct tm with gmtime_r(), then back to a
time_t using mktime.
The returned value is the difference between UTC and localtime in
seconds. If localtime is ahead of UTC (east) the offset will be a
positive number, and if localtime is behind UTC (west) the offset will
be negative.
This function should be POSIX-compliant, and is threadsafe, but not
async signal safe. If it was ever necessary to know this value in a
child process, we could cache it with a one-time init function when
libvirtd starts, then just supply the cached value, but that
complexity isn't needed for current usage; that would also have the
problem that it might not be accurate after a local daylight savings
boundary.
(If it weren't for DST, we could simply replace this entire function
with "-timezone"; timezone contains the offset of the current timezone
(negated from what we want) but doesn't account for DST. And in spite
of being guaranteed by POSIX, it isn't available on older versions of
mingw.)
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-05-24 14:21:26 +00:00
|
|
|
int virTimeLocalOffsetFromUTC(long *offset)
|
|
|
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
|
2011-11-29 12:11:01 +00:00
|
|
|
|
2016-04-08 11:11:10 +00:00
|
|
|
typedef struct {
|
|
|
|
unsigned long long start_t;
|
|
|
|
unsigned long long next;
|
|
|
|
unsigned long long limit_t;
|
|
|
|
} virTimeBackOffVar;
|
|
|
|
|
|
|
|
int virTimeBackOffStart(virTimeBackOffVar *var,
|
|
|
|
unsigned long long first, unsigned long long timeout);
|
|
|
|
|
|
|
|
bool virTimeBackOffWait(virTimeBackOffVar *var);
|