Re: [PATCH 1/3] util: new function virTimeLocalOffsetFromUTC

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, May 21, 2014 at 04:16:29PM +0300, Laine Stump wrote:
> Since there isn't a single libc API to get this value, this patch
> supplies one which gets the value by grabbing current UTC, then
> converting that into a struct tm with localtime_r(), then back to a
> time_t using mktime; it again does the same operation, but using
> gmtime_r() instead (for UTC). It then subtracts utc time from the
> localtime, and finally adjusts if dst is set in the localtime timeinfo
> (because for some reason mktime doesn't take that into account).
> 
> 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.
> ---
>  src/libvirt_private.syms |  1 +
>  src/util/virtime.c       | 41 ++++++++++++++++++++++++++++++++++++++++-
>  src/util/virtime.h       |  5 +++--
>  3 files changed, 44 insertions(+), 3 deletions(-)
> 
> diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
> index c3332c9..cb635cd 100644
> --- a/src/libvirt_private.syms
> +++ b/src/libvirt_private.syms
> @@ -1984,6 +1984,7 @@ virTimeFieldsNow;
>  virTimeFieldsNowRaw;
>  virTimeFieldsThen;
>  virTimeFieldsThenRaw;
> +virTimeLocalOffsetFromUTC;
>  virTimeMillisNow;
>  virTimeMillisNowRaw;
>  virTimeStringNow;
> diff --git a/src/util/virtime.c b/src/util/virtime.c
> index caa4e24..c487eba 100644
> --- a/src/util/virtime.c
> +++ b/src/util/virtime.c
> @@ -1,7 +1,7 @@
>  /*
>   * virtime.c: Time handling functions
>   *
> - * Copyright (C) 2006-2012 Red Hat, Inc.
> + * Copyright (C) 2006-2014 Red Hat, Inc.
>   *
>   * This library is free software; you can redistribute it and/or
>   * modify it under the terms of the GNU Lesser General Public
> @@ -344,3 +344,42 @@ char *virTimeStringThen(unsigned long long when)
>  
>      return ret;
>  }
> +
> +/**
> + * virTimeLocalOffsetFromUTC:
> + *
> + * This function is threadsafe, but is *not* async signal safe (due to
> + * localtime_r()).
> + *
> + * @offset: pointer to time_t that will difference between localtime
> + *          and UTC in seconds.
> + *
> + * Returns 0 on success, -1 on error with error reported
> + */
> +int
> +virTimeLocalOffsetFromUTC(time_t *offset)
> +{
> +    struct tm gmtimeinfo, localtimeinfo;
> +    time_t current, utc, local;
> +
> +    if ((current = time(NULL)) < 0) {
> +        virReportSystemError(errno, "%s",
> +                             _("failed to get current system time"));
> +        return -1;
> +    }
> +
> +    localtime_r(&current, &localtimeinfo);
> +    gmtime_r(&current, &gmtimeinfo);
> +
> +    if ((local = mktime(&localtimeinfo)) < 0 ||
> +        (utc = mktime(&gmtimeinfo)) < 0) {
> +        virReportSystemError(errno, "%s",
> +                             _("mktime failed"));
> +        return -1;
> +    }
> +
> +    *offset = local - utc;
> +    if (localtimeinfo.tm_isdst)
> +        *offset += 3600;
> +    return 0;
> +}

I think we ought to be able to unit test this code to make sure it is
doing what we want. ie setenv("TZ", "EDT") in the start of the test
suite to force a predictable timezone, then check the delta is
correct.


Regards,
Daniel
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]