From nobody Fri May 19 19:09:54 2023 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4QNGcf5cYnz4CGWZ; Fri, 19 May 2023 19:09:54 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4QNGcf371yz3FHf; Fri, 19 May 2023 19:09:54 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1684523394; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=EKVOdpNS7e7yICi4Im9jkmMpdQT/+aNo3BTHPe/ZW0k=; b=XX1LWWj1BbCmzzM7VSxf0bhjrbhQcl0K7vLS0pnpTa+4POMNBTrc8evZgyHTWt6FelHpLf Fz6f4EI5fMOw3OBtzbqxWMyQZQ+HPM3s+gHS/uhwm5IDADzINipboZZcc0mrytc6J4UiPE EiXXGEtN5mTJDFh5WPnNIXi79cFYeLSqvCYs46xNw61b30/KlWVHWrQC9T+aL5LhCMCB/E LkAVOUD7UfbZJ/pzY+2uFUbDRPNDzf+ndvR3prqWg5VQGAqRL01dYpsSQ33+vFE1RsbR4V NZct3lYfqIZr75aiTDrRkQ7+RdHtzlJeJyQKp3sszAdOa3Ur7N+HeoKOjXwiNg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1684523394; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=EKVOdpNS7e7yICi4Im9jkmMpdQT/+aNo3BTHPe/ZW0k=; b=ELS31CS5TagjqtIRGUrTRFh7ew/5ZyB0HDoMVRd74bBtYfFlswDH3rxkcvfCfZmUlNlB5r 94I/1VaEZM+b0qAbAaM87AHhkQfMsYH1YnpB5e0nRs7uDbj5PT43YhsVX4WJ6F/2AXffW+ U79ComBkj+WputVlDn+7yadj+X5IBGOMIF1g93OY+qmNkFggdCRd1IJM05mjl0+rEoJ6Bi xMuNGS+wmr9apnJSJd1M6nCt/hWUbK6s3LtgiVPlrtBIkItg/nQYQfHImEJHJnnffBR8c7 5A+102vXaNrO/X3K+QDY4iBqkh0Kh0GC8vVN5zG6hxG5U/J3JXOIsz+grbh15g== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1684523394; a=rsa-sha256; cv=none; b=CfSBbx1GbQZ24Xg6ofeW+urJHTbcyfYyN8XqBkgRCDG6WpBEzVWTlBZ9367h/CO/vonSt5 exJ13GcF/kFlS53eqM4oO8BrOeApVdzsMvKFSDPJz0HRAPARuBW5fbZISuThQvOKnEPuRl fGiphKafLT2ux2TQdbPxA7mSpnjHPT+BgWwG1j5JNyAWwOABApxZtsgtnbDaGvON+aEQhH 8CZuN3cYoCfN4ffQBNcVGQVI3lWIASbtPcogoCV+CucPe0/39iG3fGgtqftn8KOc5vTk2i dvNZrrYz70Ud5Utrfv5sckavmRI0KQLxpn7BpNhPk8rPCQ/XYH753HgTOsg2Cw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4QNGcf2ChHzfQp; Fri, 19 May 2023 19:09:54 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 34JJ9sW9086754; Fri, 19 May 2023 19:09:54 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 34JJ9sVd086753; Fri, 19 May 2023 19:09:54 GMT (envelope-from git) Date: Fri, 19 May 2023 19:09:54 GMT Message-Id: <202305191909.34JJ9sVd086753@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Baptiste Daroussin Subject: git: 31edf56b1571 - main - date: add -z output_zone option List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: bapt X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 31edf56b157169b2f83d288ebfae7d4b15ce6dde Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by bapt: URL: https://cgit.FreeBSD.org/src/commit/?id=31edf56b157169b2f83d288ebfae7d4b15ce6dde commit 31edf56b157169b2f83d288ebfae7d4b15ce6dde Author: Baptiste Daroussin AuthorDate: 2023-05-19 09:08:11 +0000 Commit: Baptiste Daroussin CommitDate: 2023-05-19 19:09:40 +0000 date: add -z output_zone option Inpired by OpenBSD date(1), this option allows to do timezone conversion via the date(1) command. For example, to determine when the BSDCan livestream begins for me: $ env -i TZ=EST5EDT date -z Europe/Paris -j 0900 MFC After: 1 week Reviewed by: kib, bcr (manpage) Differential Revision: https://reviews.freebsd.org/D40159 --- bin/date/date.1 | 22 ++++++++++++++++++++-- bin/date/date.c | 12 +++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/bin/date/date.1 b/bin/date/date.1 index 0c92a8c71f90..b8a1ae9718c1 100644 --- a/bin/date/date.1 +++ b/bin/date/date.1 @@ -32,7 +32,7 @@ .\" @(#)date.1 8.3 (Berkeley) 4/28/95 .\" $FreeBSD$ .\" -.Dd April 13, 2023 +.Dd May 19, 2023 .Dt DATE 1 .Os .Sh NAME @@ -42,6 +42,7 @@ .\" Display time. .Nm .Op Fl nRu +.Op Fl z Ar output_zone .Op Fl I Ns Op Ar FMT .Op Fl r Ar filename .Op Fl r Ar seconds @@ -56,6 +57,7 @@ .\" Set time with the default input format. .Nm .Op Fl jnRu +.Op Fl z Ar output_zone .Op Fl I Ns Op Ar FMT .Oo .Sm off @@ -77,6 +79,7 @@ .\" Set time with the user-provided input format. .Nm .Op Fl jnRu +.Op Fl z Ar output_zone .Op Fl I Ns Op Ar FMT .Oo .Sm off @@ -202,6 +205,14 @@ displays the time in the time zone described by or the .Ev TZ environment variable. +.It Fl z Ar output_zone +Just before printing the time, change to the specified timezone; +see the description of +.Ev TZ +below. +This can be used with +.Fl j +to easily convert time specifications from one zone to another. .It Xo .Fl v .Sm off @@ -514,13 +525,20 @@ will display .Pp .Dl "2018-08-04T13:42:19-07:00" .Pp -Finally the command: +The command: .Pp .Dl "env LC_ALL=C date -j -f ""%a %b %d %T %Z %Y"" ""`env LC_ALL=C date`"" ""+%s""" .Pp can be used to parse the output from .Nm and express it in Epoch time. +.Pp +Finally the command +.Pp +.Dl "TZ=America/Los_Angeles date -z Europe/Paris -j 0900" +.Pp +will print the time in the "Europe/Paris" timezone when it is 9:00 in The +America/Los_Angeles timezone. .Sh DIAGNOSTICS It is invalid to combine the .Fl I diff --git a/bin/date/date.c b/bin/date/date.c index 4dc5df0dccfc..c1fb496ce7ee 100644 --- a/bin/date/date.c +++ b/bin/date/date.c @@ -95,7 +95,7 @@ main(int argc, char *argv[]) bool Iflag, jflag, Rflag; const char *format; char buf[1024]; - char *fmt; + char *fmt, *outzone = NULL; char *tmp; struct vary *v; const struct vary *badv; @@ -108,7 +108,7 @@ main(int argc, char *argv[]) (void) setlocale(LC_TIME, ""); rflag = 0; Iflag = jflag = Rflag = 0; - while ((ch = getopt(argc, argv, "f:I::jnRr:uv:")) != -1) + while ((ch = getopt(argc, argv, "f:I::jnRr:uv:z:")) != -1) switch((char)ch) { case 'f': fmt = optarg; @@ -152,6 +152,9 @@ main(int argc, char *argv[]) case 'u': /* do everything in UTC */ (void)setenv("TZ", "UTC0", 1); break; + case 'z': + outzone = optarg; + break; case 'v': v = vary_append(v, optarg); break; @@ -189,6 +192,8 @@ main(int argc, char *argv[]) format = *argv + 1; } + if (outzone != NULL && setenv("TZ", outzone, 1) != 0) + err(1, "setenv(TZ)"); lt = localtime(&tval); if (lt == NULL) errx(1, "invalid time"); @@ -211,6 +216,7 @@ main(int argc, char *argv[]) */ setlocale(LC_TIME, "C"); + (void)strftime(buf, sizeof(buf), format, lt); printdate(buf); } @@ -385,7 +391,7 @@ usage(void) (void)fprintf(stderr, "%s\n%s\n%s\n", "usage: date [-jnRu] [-I[date|hours|minutes|seconds]] [-f input_fmt]", " " - "[-r filename|seconds] [-v[+|-]val[y|m|w|d|H|M|S]]", + "[ -z output_zone ] [-r filename|seconds] [-v[+|-]val[y|m|w|d|H|M|S]]", " " "[[[[[[cc]yy]mm]dd]HH]MM[.SS] | new_date] [+output_fmt]" );