From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qk1-x72d.google.com (mail-qk1-x72d.google.com [IPv6:2607:f8b0:4864:20::72d]) by sourceware.org (Postfix) with ESMTPS id D5DAA3846075 for ; Tue, 2 Feb 2021 18:23:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org D5DAA3846075 Received: by mail-qk1-x72d.google.com with SMTP id t63so20778958qkc.1 for ; Tue, 02 Feb 2021 10:23:29 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:references:from:autocrypt:subject:message-id :date:user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=zJ0a1rxlCKK+mkZRhn+4UfAZ8DKw14BRYNjGT27JoRk=; b=r/mc/3RZL43j+3SBW641ztoAVk4Ww67tIZP7XXK5H0SwJ1HxeMMV4gJ2aWFEJ9X7gP Gtz0l1WGlhkuQ7mwaH+Wj5pI47m0yy2cb7pmvqnNC5S2/AvdNPCh/BzKKxIksDSVSMeE ZBsgx1nW3ROWyJTKme4UHZale/sw55fHiuw6LS87ubriwuXofzx0KleIxRy++DjW0xR3 pfLuHhjEDDbCPlmjaGwcr++iN51C+Qf1kANyjsAFYuSy5bVhKUENM+7v3aLf1vt1bvsG vfAmJiJOJtIm9mTU4KQz6C/FlEbUPpt3mmswZYbY3QMRaePcoBNbhnDZi7Su1M6+xJxm HEnw== X-Gm-Message-State: AOAM53237SoqtVlmaBKOZD4IxWRPx15+OxdZGsyaxBIXVh2gyEApria4 TbmUw0qGJQt6oHaiaH82rXElP8xpCTtfiQ== X-Google-Smtp-Source: ABdhPJxC9eNnibnV72+tXxT0ul6VcYH6JMxrFJEkQLJY+dUs0cQ8YEDBBl96dq+bHV+l1PCPKn99ZA== X-Received: by 2002:a37:9c05:: with SMTP id f5mr22474240qke.189.1612290209138; Tue, 02 Feb 2021 10:23:29 -0800 (PST) Received: from [192.168.1.4] ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id f14sm12659185qkl.76.2021.02.02.10.23.27 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 02 Feb 2021 10:23:28 -0800 (PST) To: noloader@gmail.com, "libc-help@sourceware.org" References: From: Adhemerval Zanella Autocrypt: addr=adhemerval.zanella@linaro.org; prefer-encrypt=mutual; keydata= mQINBFcVGkoBEADiQU2x/cBBmAVf5C2d1xgz6zCnlCefbqaflUBw4hB/bEME40QsrVzWZ5Nq 8kxkEczZzAOKkkvv4pRVLlLn/zDtFXhlcvQRJ3yFMGqzBjofucOrmdYkOGo0uCaoJKPT186L NWp53SACXguFJpnw4ODI64ziInzXQs/rUJqrFoVIlrPDmNv/LUv1OVPKz20ETjgfpg8MNwG6 iMizMefCl+RbtXbIEZ3TE/IaDT/jcOirjv96lBKrc/pAL0h/O71Kwbbp43fimW80GhjiaN2y WGByepnkAVP7FyNarhdDpJhoDmUk9yfwNuIuESaCQtfd3vgKKuo6grcKZ8bHy7IXX1XJj2X/ BgRVhVgMHAnDPFIkXtP+SiarkUaLjGzCz7XkUn4XAGDskBNfbizFqYUQCaL2FdbW3DeZqNIa nSzKAZK7Dm9+0VVSRZXP89w71Y7JUV56xL/PlOE+YKKFdEw+gQjQi0e+DZILAtFjJLoCrkEX w4LluMhYX/X8XP6/C3xW0yOZhvHYyn72sV4yJ1uyc/qz3OY32CRy+bwPzAMAkhdwcORA3JPb kPTlimhQqVgvca8m+MQ/JFZ6D+K7QPyvEv7bQ7M+IzFmTkOCwCJ3xqOD6GjX3aphk8Sr0dq3 4Awlf5xFDAG8dn8Uuutb7naGBd/fEv6t8dfkNyzj6yvc4jpVxwARAQABtElBZGhlbWVydmFs IFphbmVsbGEgTmV0dG8gKExpbmFybyBWUE4gS2V5KSA8YWRoZW1lcnZhbC56YW5lbGxhQGxp bmFyby5vcmc+iQI3BBMBCAAhBQJXFRpKAhsDBQsJCAcDBRUKCQgLBRYCAwEAA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cBPvpoy4OoG Subject: Re: Converting time from timezone string to another timezone time Message-ID: <4e309a02-0398-4679-f74b-4d45a665ae6a@linaro.org> Date: Tue, 2 Feb 2021 15:23:25 -0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GB_TO_NAME_FREEMAIL, KAM_SHORT, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-help@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-help mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 02 Feb 2021 18:23:32 -0000 On 29/01/2021 05:11, Jeffrey Walton via Libc-help wrote: > Hi Everyone, > > I'm having a heck of a time converting a time from a timezone string > to another timezone time. The best I seem to be able to do is adjust > for DST using strptime, mktime and localtime. For example: > > * given: '15 Jan 2021 01:24:55 -0800 (PST)' > * expected: '15 Jan 2021 04:24:55 -0500 (EST)' (or similar, as long > as it includes 04:24:55) > * my best result: '15 Jan 00:24:55 2021' (seems to be a DST adj of PST time) > > I've been through the manual at > https://www.gnu.org/software/libc/manual/html_node/Calendar-Time.html > but I can't seem to find the sequence of calls to perform the actions. > I've also been through the libc FAQ at > https://sourceware.org/glibc/wiki/FAQ (there's one topic on > timezones). > > So I have two questions, but I only need one answered (whichever is > easier to answer): > > 1. Where is the discussion/faq on how to convert from timezone string > to another timezone time? > > 2. What is wrong with this code (error checking omitted)? > > const char pst_timestring[] = '15 Jan 2021 01:24:55 -0800 (PST)'; > struct tm gmail_tm[1]; > strptime(pst_timestring, "%d %b %Y %T %z", gmail_tm); > > // Already set, but just in case. It is -0500 (EST) > char my_timezone[] = "TZ=America/New_York"; > putenv(my_timezone); > > time_t utc_time; > utc_time = mktime(gmail_tm); > > struct tm local_tm[1]; > local_tm = localtime(&utc_time); > > // This prints a time that was adjusted by 1 hour, not 3 hours > (expected from -0800 to -0500) > printf(..., asctime(local_tm)); > // Output is similar to '15 Jan 00:24:55 2021' > > Somewhere there is a non-obvious problem with the code stitched > together from the man pages. > I think what you might like is something like: --- $ cat test.c #include #include #include static void set_tz (const char *tz) { char buf[128]; snprintf (buf, sizeof (buf), "TZ=%s", tz); putenv (buf); tzset (); } static struct tm convert_time (const char *origtm, const char *dsttm, const char *timestr) { struct tm tm, ttm; set_tz (origtm); strptime (timestr, "%d %b %Y %T", &tm); time_t t = mktime (&tm); set_tz (dsttm); localtime_r (&t, &ttm); return ttm; } int main (int argc, char *argv[]) { const char timestr[] = "15 Jan 2021 01:24:55"; const char origtm[] = "Europe/Berlin"; const char dsttm[] = "America/New_York"; struct tm tm = convert_time (origtm, dsttm, timestr); char buffer[256]; strftime (buffer, sizeof (buffer), "%d %b %Y %T", &tm); printf ("%s: %s\n%s: %s\n", origtm, timestr, dsttm, buffer); return 0; } $ gcc -D_GNU_SOURCE test.c && ./a.out Europe/Berlin: 15 Jan 2021 01:24:55 America/New_York: 14 Jan 2021 23:24:55 --- AFAIK there is not easy way to accomplish it without setting the current TZ, create a struct tm, getting a canonical UNIX time_t from it, reset TZ, and convert. Also keep in mind this has some corner cases regarding thread safety, and I am not sure if plays well on every input and timezones. The coreutils date command uses a different strategy, where it uses the gnulib parse-datetime module [1]. It does something as: char const *tzstring = getenv ("TZ"); timezone_t tz = tzalloc (tzstring); [...] parse_datetime2 (&when, datestr, NULL, parse_datetime_flags, tz, tzstring); show_date (format, when, tz); And parse_datetime2 does all the magic internally. It also seems to be thread-safe and do not require messing with TZ environment variable. [1] https://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob;f=lib/parse-datetime.y;h=b8a832fcd8f70a0df7b6104e1bacc4e0495bf01f;hb=HEAD