Re: grdc: show timezone when TZ is set

2022-09-23 Thread Florian Obser
deraadt objected to the time zone validation. I don't care about the
feature and I agree with the point that I shouldn't do it because there
is no API for it. I don't even know where the time zone files are.

To make this all more symmetric always print tm_zone, even if TZ is not
set.

OK?

diff --git grdc.6 grdc.6
index 16e1758c6d2..5aa6e84a2d2 100644
--- grdc.6
+++ grdc.6
@@ -34,8 +34,11 @@ key exits the program.
 .Bl -tag -width Ds
 .It Ev TZ
 The time zone to use for displaying the time.
-It is specified as a pathname relative to
-.Pa /usr/share/zoneinfo .
+It is normally specified as a pathname relative to
+.Pa /usr/share/zoneinfo ,
+though see
+.Xr tzset 3
+for more information.
 If this variable is not set, the time zone is determined based on
 .Pa /etc/localtime .
 .El
diff --git grdc.c grdc.c
index 66e5eee79e6..01d29b08d64 100644
--- grdc.c
+++ grdc.c
@@ -12,6 +12,7 @@
  */
 
 #include 
+#include 
 
 #include 
 #include 
@@ -20,6 +21,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 
@@ -78,9 +80,14 @@ main(int argc, char *argv[])
int xbase;
int ybase;
int wintoosmall;
+   int tz_len = 0;
+   int h, m;
+   int prev_tm_gmtoff;
char *tz;
 
tz = getenv("TZ");
+   if (tz != NULL)
+   tz_len = strlen(tz);
 
scrol = wintoosmall = 0;
while ((i = getopt(argc, argv, "sh")) != -1) {
@@ -135,6 +142,7 @@ main(int argc, char *argv[])
 
curs_set(0);
sigwinched = 1; /* force initial sizing */
+   prev_tm_gmtoff = 24 * 3600; /* force initial header printing */
 
clock_gettime(CLOCK_REALTIME, );
if (n) {
@@ -152,9 +160,11 @@ main(int argc, char *argv[])
set(tm->tm_hour / 10, 24);
set(10, 7);
set(10, 17);
-   if (sigwinched) {
+   /* force repaint if window size changed or DST changed */
+   if (sigwinched || prev_tm_gmtoff != tm->tm_gmtoff) {
sigwinched = 0;
wintoosmall = 0;
+   prev_tm_gmtoff = tm->tm_gmtoff;
getwinsize(, );
if (i >= XLENGTH + 2)
xbase = (i - XLENGTH) / 2;
@@ -184,11 +194,19 @@ main(int argc, char *argv[])
move(ybase, xbase + XLENGTH);
vline(ACS_VLINE, YDEPTH);
 
-   if (tz != NULL) {
-   move(ybase - 1, xbase);
-   printw("[ %s %+d ]", tz,
-   tm->tm_gmtoff / 60 / 60 );
-   }
+   move(ybase - 1, xbase);
+
+   h = tm->tm_gmtoff / 3600;
+   m = abs((int)tm->tm_gmtoff % 3600 / 60);
+
+   if (tz_len > 0 && tz_len <= XLENGTH -
+   strlen("[  () + ]") -
+   strlen(tm->tm_zone))
+   printw("[ %s (%s) %+2.2d%02d ]", tz,
+   tm->tm_zone, h, m);
+   else
+   printw("[ %s %+2.2d%02d ]",
+   tm->tm_zone, h, m);
 
attrset(COLOR_PAIR(2));
}

-- 
I'm not entirely sure you are real.



Re: grdc: show timezone when TZ is set

2022-09-23 Thread Florian Obser
So, with the tzset(3) restriction in place I'd like to fix grdc, because
what we currently have is wrong:

There are time zones that have minute offsets, display those
correctly. Pointed out by pjanzen@.
To display the offset, use ISO 8601, as suggested by David Goerger.

Take a guess if tzset(3) will accept the time zone specified in TZ as
a relative path and only display it if that is true and it's not too
long. All time zones currently in /usr/share/zoneinfo fit.

Lastly check if tm->tm_gmtoff changed which probably means that we
moved in or out of daylight savings time.

OK?

p.s. I don't know what to do about wintoosmall at this time, the diff is
big enough as it is. And quite frankly I don't care about that.

diff --git grdc.6 grdc.6
index 16e1758c6d2..5aa6e84a2d2 100644
--- grdc.6
+++ grdc.6
@@ -34,8 +34,11 @@ key exits the program.
 .Bl -tag -width Ds
 .It Ev TZ
 The time zone to use for displaying the time.
-It is specified as a pathname relative to
-.Pa /usr/share/zoneinfo .
+It is normally specified as a pathname relative to
+.Pa /usr/share/zoneinfo ,
+though see
+.Xr tzset 3
+for more information.
 If this variable is not set, the time zone is determined based on
 .Pa /etc/localtime .
 .El
diff --git grdc.c grdc.c
index 66e5eee79e6..4bb7fa1b1af 100644
--- grdc.c
+++ grdc.c
@@ -12,6 +12,7 @@
  */
 
 #include 
+#include 
 
 #include 
 #include 
@@ -20,6 +21,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 
@@ -45,6 +47,7 @@ void getwinsize(int *, int *);
 void set(int, int);
 void standt(int);
 void __dead usage(void);
+int check_tz(const char *);
 
 void
 sigalrm(int signo)
@@ -64,6 +67,36 @@ sigresize(int signo)
sigwinched = signo;
 }
 
+/* Take a guess if tzset(3) will accept TZ as a relative path */
+int
+check_tz(const char *tz)
+{
+   struct stat  sb;
+   char fullname[PATH_MAX];
+   int  i;
+
+   if (tz == NULL)
+   return 0;
+
+   if (tz[0] == ':')
+   tz++;
+
+   if (tz[0] == '/' || strstr(tz, "../") != NULL)
+   return 0;
+
+   i = snprintf(fullname, sizeof(fullname), "/usr/share/zoneinfo/%s", tz);
+   if (i < 0 || i >= sizeof(fullname))
+   return 0;
+
+   if (stat(fullname, ) == -1)
+   return 0;
+
+   if (!S_ISREG(sb.st_mode))
+   return 0;
+
+   return 1;
+}
+
 int
 main(int argc, char *argv[])
 {
@@ -78,9 +111,19 @@ main(int argc, char *argv[])
int xbase;
int ybase;
int wintoosmall;
+   int tz_valid;
+   int tz_len = 0;
+   int prev_tm_gmtoff;
char *tz;
 
tz = getenv("TZ");
+   tz_valid = check_tz(tz);
+
+   if (tz_valid) {
+   if (tz[0] == ':')
+   tz++;
+   tz_len = strlen(tz);
+   }
 
scrol = wintoosmall = 0;
while ((i = getopt(argc, argv, "sh")) != -1) {
@@ -135,6 +178,7 @@ main(int argc, char *argv[])
 
curs_set(0);
sigwinched = 1; /* force initial sizing */
+   prev_tm_gmtoff = 24 * 3600; /* force initial header printing */
 
clock_gettime(CLOCK_REALTIME, );
if (n) {
@@ -152,9 +196,11 @@ main(int argc, char *argv[])
set(tm->tm_hour / 10, 24);
set(10, 7);
set(10, 17);
-   if (sigwinched) {
+   /* force repaint if window size changed or DST changed */
+   if (sigwinched || prev_tm_gmtoff != tm->tm_gmtoff) {
sigwinched = 0;
wintoosmall = 0;
+   prev_tm_gmtoff = tm->tm_gmtoff;
getwinsize(, );
if (i >= XLENGTH + 2)
xbase = (i - XLENGTH) / 2;
@@ -185,9 +231,20 @@ main(int argc, char *argv[])
vline(ACS_VLINE, YDEPTH);
 
if (tz != NULL) {
+   int h, m;
+   h = tm->tm_gmtoff / 3600;
+   m = abs((int)tm->tm_gmtoff % 3600 / 60);
+   if (tz_valid && tz_len > XLENGTH -
+   strlen("[  () + ]") -
+   strlen(tm->tm_zone))
+   tz_valid = 0;
move(ybase - 1, xbase);
-   printw("[ %s %+d ]", tz,
-   tm->tm_gmtoff / 60 / 60 );
+   if (tz_valid)
+   printw("[ %s (%s) %+2.2d%02d ]",
+   tz, tm->tm_zone, h, m);
+   else
+   printw("[ %s %+2.2d%02d ]",
+

Re: grdc: show timezone when TZ is set

2022-09-18 Thread Steffen Nurpmeso
David Goerger wrote in
 :
 |Sunday, 20220918 13:38+, Florian Obser wrote:
 |>I'm happy with that, let's do this then
 |>- fix the offset calculation
 |>- output tm->tm_zone in addition to TZ to be able to spot typos.
 |
 |I like the overall diff (thanks!), but one minor formatting nit is that
 |I believe it's more common under ISO 8601[1] to print UTC timezone
 |offset as "+/-%H%M", e.g. "-0400", not as "-4h00". At least, that's
 |what I'm familiar with in SMTP headers.
 |
 |Updated diff below, where only change from your latest diff is in the
 |second printw(3) statement. Tested against both TZ='America/New_York'
 |(-0400) and TZ='Asia/Kolkata' (+0530).
 |
 |Again, thanks for polishing this old "game"!

Only to mention that nothing prevents timezones from using second
offsets also.  This is a political thing, and the TZ DB even is
about to start supporting millisecond resolution (i think).
Anyhow, since you mentioned the SMTP standard, that was surely
an all american miss to go for +-HHMM instead of +-HHMMSS.
The CLOCK_TAI clock is off by only seconds.  (And someone wants to
use its own TZ file, surely atomic-whatever-genitals thus!)

--steffen
|
|Der Kragenbaer,The moon bear,
|der holt sich munter   he cheerfully and one by one
|einen nach dem anderen runter  wa.ks himself off
|(By Robert Gernhardt)



Re: grdc: show timezone when TZ is set

2022-09-18 Thread David Goerger

Sunday, 20220918 13:38+, Florian Obser wrote:

I'm happy with that, let's do this then
- fix the offset calculation
- output tm->tm_zone in addition to TZ to be able to spot typos.


I like the overall diff (thanks!), but one minor formatting nit is that
I believe it's more common under ISO 8601[1] to print UTC timezone
offset as "+/-%H%M", e.g. "-0400", not as "-4h00". At least, that's
what I'm familiar with in SMTP headers.

Updated diff below, where only change from your latest diff is in the
second printw(3) statement. Tested against both TZ='America/New_York'
(-0400) and TZ='Asia/Kolkata' (+0530).

Again, thanks for polishing this old "game"!

[1] https://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC


$ got diff
diff /cvs/src
commit - 5d6f1c4a07abcf6c1413059c603776d5dc6805aa
path + /cvs/src
blob - 66e5eee79e6449916e83660a8c7c62667d04c5ab
file + games/grdc/grdc.c
--- games/grdc/grdc.c
+++ games/grdc/grdc.c
@@ -185,9 +185,12 @@ main(int argc, char *argv[])
vline(ACS_VLINE, YDEPTH);

if (tz != NULL) {
+   int h, m;
+   h = tm->tm_gmtoff / 3600;
+   m = abs((int)tm->tm_gmtoff % 3600 / 60);
move(ybase - 1, xbase);
-   printw("[ %s %+d ]", tz,
-   tm->tm_gmtoff / 60 / 60 );
+   printw("[ %s (%s) %+2.2d%02d]", tz,
+   tm->tm_zone, h, m);
}

attrset(COLOR_PAIR(2));





Re: grdc: show timezone when TZ is set

2022-09-18 Thread Theo de Raadt
Paul Janzen  wrote:

> My issues with the time system are (a) it has to "just work", so it flees
> to UTC the instant something goes wrong, but never makes available any
> status as to whether the asked-for time zone is the one returned, except
> for (b) my program aborts if I'm pledged and I go do something silly with
> the TZ.

There is absolutely nothing wrong with this behaviour.

It is commonly known as 'garbage in, garbage in".



Re: grdc: show timezone when TZ is set

2022-09-18 Thread Paul Janzen
> I disagree with the full-featured support of tzset() for all TZ paths.
> 
> and, of course your private Monticello file is corrupt TZ file, which 
> exercises
> a secret bug in the libc/time parser of the file, and you rely upon it to take
> over control of this program, and other programs.
> 
> 
> I despise this "feature".  It basically says that tzset() requires full
> filesystem access during the whole program lifetime.

I agree.  Anyone who's not actually developing time zone files should be
plenty happy using what exists, or at worst lobbying a sysadmin to add a TZ
file to /usr/share/zoneinfo.  I think this feature exists only for
historical developer convenience.  Otherwise it's just exposure surface.

My issues with the time system are (a) it has to "just work", so it flees
to UTC the instant something goes wrong, but never makes available any
status as to whether the asked-for time zone is the one returned, except
for (b) my program aborts if I'm pledged and I go do something silly with
the TZ.

If the answer to (b) is "don't do silly things", I can live with that,
although in that case I'd think the best answer would be to "just work" and
default to UTC like everything else, rather than aborting -- just like our
setugid() programs do (grdc is now, interestingly, a program that aborts on
localtime() if TZ is outside zoneinfo... unless you make grdc setgid first,
then it works).  Sadly I can't promise I'll stop doing silly things.  But I
have no issue breaking with tradition, revising the documentation, and
insisting that valid TZs are only relative paths inside zoneinfo.

For (a), the issue is that I don't think grdc should display $TZ if TZ is
an invalid timezone or at least not the timezone being used.  Sadly, grdc
has no easy way of telling.  If only there were some API to tell.

Further with grdc, I'd really like it if the string being displayed were
not necessarily all of $TZ, but limited to up to the last  characters
(the end of the time zone string being more interesting than the start),
for n on the order of 40.



Paul Janzen.



Re: grdc: show timezone when TZ is set

2022-09-18 Thread Florian Obser
I'm happy with that, let's do this then
- fix the offset calculation
- output tm->tm_zone in addition to TZ to be able to spot typos.

OK?

diff --git grdc.c grdc.c
index 66e5eee79e6..05b1ff1ea87 100644
--- grdc.c
+++ grdc.c
@@ -185,9 +185,12 @@ main(int argc, char *argv[])
vline(ACS_VLINE, YDEPTH);
 
if (tz != NULL) {
+   int h, m;
+   h = tm->tm_gmtoff / 3600;
+   m = abs((int)tm->tm_gmtoff % 3600 / 60);
move(ybase - 1, xbase);
-   printw("[ %s %+d ]", tz,
-   tm->tm_gmtoff / 60 / 60 );
+   printw("[ %s (%s) %+dh%02d ]", tz,
+   tm->tm_zone, h, m);
}
 
attrset(COLOR_PAIR(2));



-- 
I'm not entirely sure you are real.



Re: grdc: show timezone when TZ is set

2022-09-18 Thread Theo de Raadt
Paul Janzen  wrote:

> Yeah, I know with pledge() you can't do testing like
> TZ=:/home/pjanzen/dev/usr/share/zoneinfo/testing/2022/America/Kentucky/Monticello
> any more, even though that's a perfectly cromulent and functional TZ on my
> system otherwise. So for the time being, probably any TZ that exists and
> doesn't abort grdc is 38 bytes or shorter.  Which works.


I disagree with the full-featured support of tzset() for all TZ paths.

and, of course your private Monticello file is corrupt TZ file, which exercises
a secret bug in the libc/time parser of the file, and you rely upon it to take
over control of this program, and other programs.


I despise this "feature".  It basically says that tzset() requires full
filesystem access during the whole program lifetime.

Actually it is worse than that.  Most programs do not call tzset().  I would
argue this program should not either.

Because there are 20+ libc API in src/lib/libc/time that will implicitly
do tzset(), whenever they need to.

And there are 10+ other libc API on that will call those libc/time API

And then there probably are thousands of API in other libraries which will
call those, late in a program's lifetime.


Backtracking, I think the idea that programs cannot be filesystem-contained
because of support for a stupid environment variable is dumb, and I think
it should fall back to the localtime or GMT for that program.

Otherwise if we wanted to be perfect, we should add tzset() as the first
function call in main() in ALL programs, which would be far more
repugnant than slowly taking the arbitrary path ability away from TZ, program
by program (in the same way we did for setuid/setgid binaries 20+ years ago).

tzload(const char *name, struct state *sp, int doextend)
...
if (name != NULL && issetugid() != 0) {
if ((name[0] == ':' && (strchr(name, '/') || strstr(name, 
".."))) ||
name[0] == '/' || strchr(name, '.'))
name = NULL;
}


I think you will notice noone notices or cares.


I can mock it further by suggesting we add a O_TZ flag to open() which
tzload() can use to bypass the restrictions, because pointing at a private
personal TZ file is a Unix Right /sarc



Re: grdc: show timezone when TZ is set

2022-09-18 Thread Florian Obser
On 2022-09-18 01:55 -04, Paul Janzen  wrote:
> The recent change to grdc(6), to display additional information if TZ is
> set, has a few issues.
>
> 1.  Time zone offset incorrectly reported in Newfoundland.
>
> Some time zones have offsets of 30 or 45 minutes.  The displayed time
> offset is currently truncated to the closest hour.  (Australia/Eucla
> is a fun time zone too.)

Very good point. Let's shine this turd a bit more!

>
> 2.  The new, additional information disappears if the window is sized too
> small (wintoosmall).  There's basically room for it though...  except

I don't care too much about the wintoosmall case, so I left your version
in. I guess we could do better with the TZ validation (see below).

>
> 3.  The TZ information is a string of unknown length, and so it doesn't
> necessarily display correctly.
>
> Yeah, I know with pledge() you can't do testing like
> TZ=:/home/pjanzen/dev/usr/share/zoneinfo/testing/2022/America/Kentucky/Monticello
> any more, even though that's a perfectly cromulent and functional TZ on my
> system otherwise. So for the time being, probably any TZ that exists and
> doesn't abort grdc is 38 bytes or shorter.  Which works.

grdc got aborted even before this last change. That's not optimal. So we
have to hoist the (implicit) call to tzset before pledge(2). I went
further and did:
pledge("stdio rpath tty")
tzset()
pledge("stdio tty")

>
> But, if your timezone exists, it already has its name in short form
> included in tm->tm_zone.  That's what I think should be
> printed, even if "EDT" is way less cool than "America/Pangnirtung".  I
> mean, if the point is just to be able to label clocks with nice places,
> instead of the time zone it's showing, maybe it could be a different
> option.

For me, that would completely miss the point. I have no idea what EDT
means.

>
> Counterpoint:  some of the timezones have short names that are just the UTC
> offset, which is really boring and duplicated given that we print out the
> offset already.  Hey, maybe we could just print the offset...
>
> 4.  There's no indication if you type an invalid TZ--you get UTC and a
> misleading label onscreen.
>
> Timezone handling defaults to UTC if anything breaks along the chain, as
> the tzset(3) man page makes clear.  That means if you misspell Antarctica
> while setting your TZ=Antartica/McMurdo, you'll end up half a day off from
> all your pals at McMurdo as your screen happily tells you that you're
> seeing McMurdo +0 time.
>
> There's no simple way to tell if your $TZ is valid or not (if you get back
> UTC, maybe that's really what it should be!).  At least if we display
> tm->tm_zone rather than $TZ, we're not misleading.
>

Right, so I had a stab at validating TZ:
If TZ is a relative path and exists as a file in /usr/share/zoneinfo
display it and also print tm->tm_zone:

  ┌[ Antarctica/McMurdo (NZST) +12h00 ]──┐

  ┌[ Australia/Eucla (+0845) +8h45 ]─┐

There is a TOCTU issue (doesn't matter I think) and an issue where what
we find in /usr/share/zoneinfo is not syntactically correct, i.e.:
$ TZ=zone.tab grdc
  ┌[ zone.tab (GMT) +0h00 ]──┐

I don't think that matters either. Both are cases of: don't do that.

>
>
> I was going to be upset that the man page for grdc(6) is way pickier on TZ
> than tzset(3), but it's quite accurate given the pledge() call.  Speaking
> of which, I don't expect any one else plays with timezone files, and surely
> one doesn't want one's general utilities to be pwned by possible bugs in
> the time-handling code exploited with custom files created outside
> /usr/share/zoneinfo.  But it's still a touch irritating-should-be-fixable
> that, because the pledge() has to be after initscr(), grdc has the
> possibility of leaving the terminal in the wrong state when it aborts on
> test TZ files.
>

This works now, too:

$ TZ=~/Newfoundland grdc
  ┌[ NDT -2h30 ]─┐

>
>
> Paul Janzen.
>
> Index: grdc.c
> ===
> RCS file: /cvs/src/games/grdc/grdc.c,v
> retrieving revision 1.35
> +void
> +print_tz(int y, int x, int sml)
> +{
> + int i, j;
> +
> + move(y, x);
> + i = tm->tm_gmtoff / 60 / 60;
> + j = tm->tm_gmtoff / 60 - i * 60;

Isn't this just a weird spelling for mod (%)?

> + if (i < 0)
> + j = -j;
> + if (!sml)
> + printw("[ %s %+dh%02d ]", tm->tm_zone, i, j);
> + else
> + printw("[%+dh%02d]", i, j);
>  }
>  
>  void
>

diff --git grdc.c grdc.c
index 66e5eee79e6..5d2ea19a532 100644
--- grdc.c
+++ grdc.c
@@ -12,6 +12,7 @@
  */
 
 #include 
+#include 
 
 #include 
 #include 
@@ -26,9 +27,6 @@
 #define XLENGTH 58
 #define YDEPTH  7
 
-struct timespec now;
-struct tm *tm;
-
 short disp[11] = {
075557, 01, 071747, 071717, 055711,
074717, 074757, 07, 

Re: grdc: show timezone when TZ is set

2022-09-18 Thread Paul Janzen
The recent change to grdc(6), to display additional information if TZ is
set, has a few issues.

1.  Time zone offset incorrectly reported in Newfoundland.

Some time zones have offsets of 30 or 45 minutes.  The displayed time
offset is currently truncated to the closest hour.  (Australia/Eucla
is a fun time zone too.)

2.  The new, additional information disappears if the window is sized too
small (wintoosmall).  There's basically room for it though...  except

3.  The TZ information is a string of unknown length, and so it doesn't
necessarily display correctly.

Yeah, I know with pledge() you can't do testing like
TZ=:/home/pjanzen/dev/usr/share/zoneinfo/testing/2022/America/Kentucky/Monticello
any more, even though that's a perfectly cromulent and functional TZ on my
system otherwise. So for the time being, probably any TZ that exists and
doesn't abort grdc is 38 bytes or shorter.  Which works.

But, if your timezone exists, it already has its name in short form
included in tm->tm_zone.  That's what I think should be
printed, even if "EDT" is way less cool than "America/Pangnirtung".  I
mean, if the point is just to be able to label clocks with nice places,
instead of the time zone it's showing, maybe it could be a different option.

Counterpoint:  some of the timezones have short names that are just the UTC
offset, which is really boring and duplicated given that we print out the
offset already.  Hey, maybe we could just print the offset...

4.  There's no indication if you type an invalid TZ--you get UTC and a
misleading label onscreen.

Timezone handling defaults to UTC if anything breaks along the chain, as
the tzset(3) man page makes clear.  That means if you misspell Antarctica
while setting your TZ=Antartica/McMurdo, you'll end up half a day off from
all your pals at McMurdo as your screen happily tells you that you're
seeing McMurdo +0 time.

There's no simple way to tell if your $TZ is valid or not (if you get back
UTC, maybe that's really what it should be!).  At least if we display
tm->tm_zone rather than $TZ, we're not misleading.



I was going to be upset that the man page for grdc(6) is way pickier on TZ
than tzset(3), but it's quite accurate given the pledge() call.  Speaking
of which, I don't expect any one else plays with timezone files, and surely
one doesn't want one's general utilities to be pwned by possible bugs in
the time-handling code exploited with custom files created outside
/usr/share/zoneinfo.  But it's still a touch irritating-should-be-fixable
that, because the pledge() has to be after initscr(), grdc has the
possibility of leaving the terminal in the wrong state when it aborts on
test TZ files.



Paul Janzen.


Index: grdc.c
===
RCS file: /cvs/src/games/grdc/grdc.c,v
retrieving revision 1.35
diff -u -p -r1.35 grdc.c
--- grdc.c  17 Sep 2022 10:32:05 -  1.35
+++ grdc.c  18 Sep 2022 05:30:04 -
@@ -41,6 +41,7 @@ volatile sig_atomic_t sigwinched = 0;
 
 int hascolor = 0;
 
+void print_tz(int, int, int);
 void getwinsize(int *, int *);
 void set(int, int);
 void standt(int);
@@ -184,11 +185,8 @@ main(int argc, char *argv[])
move(ybase, xbase + XLENGTH);
vline(ACS_VLINE, YDEPTH);
 
-   if (tz != NULL) {
-   move(ybase - 1, xbase);
-   printw("[ %s %+d ]", tz,
-   tm->tm_gmtoff / 60 / 60 );
-   }
+   if (tz)
+   print_tz(ybase - 1, xbase, wintoosmall);
 
attrset(COLOR_PAIR(2));
}
@@ -199,6 +197,8 @@ main(int argc, char *argv[])
move(0, 0);
printw("%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
tm->tm_sec);
+   if (tz)
+   print_tz(1, 0, wintoosmall);
} else for (k = 0; k < 6; k++) {
if (scrol) {
for(i = 0; i < 5; i++)
@@ -282,6 +282,22 @@ set(int t, int n)
}
if (mask & m)
mask |= m;
+}
+
+void
+print_tz(int y, int x, int sml)
+{
+   int i, j;
+
+   move(y, x);
+   i = tm->tm_gmtoff / 60 / 60;
+   j = tm->tm_gmtoff / 60 - i * 60;
+   if (i < 0)
+   j = -j;
+   if (!sml)
+   printw("[ %s %+dh%02d ]", tm->tm_zone, i, j);
+   else
+   printw("[%+dh%02d]", i, j);
 }
 
 void



Re: grdc: show timezone when TZ is set

2022-09-17 Thread Klemens Nanni
On Sat, Sep 17, 2022 at 12:13:16PM +0200, Florian Obser wrote:
> Good catch, I have adapted the text from date(1) for this, I think it
> flows nicer and dropped the example.
> 
> Update diff with a local variable for getenv(3) and pulled the call up,
> it's not going to change during runtime.
> 
> OK?

OK kn



Re: grdc: show timezone when TZ is set

2022-09-17 Thread Florian Obser
On 2022-09-17 08:42 UTC, Klemens Nanni  wrote:
> On Sat, Sep 17, 2022 at 09:40:27AM +0200, Florian Obser wrote:
>> On 2021-10-24 03:06 +02, James Russell Stickney  wrote:
>> > I recently found myself wanting to moniter local time from a number of 
>> > locations around the world.
>> > Setting the TZ environment variable on grdc did a wonderfull job at this.
>> > At which point, I wanted to know which clock was showing what time.
>> > This lead to the following patch.
>> >
>> > If curious as to how this looked, it was sort of like the image below.
>> > https://nl1.outband.net/image/grdc_desktop_example.png
>> >
>> 
>> I recently found a use for this as well and remembered this diff. One
>> long line re-wrapped.
>> 
>> OK?
>
> Reads fine, but I don't use this "game".
>
>> 
>> (Or commit it with OK florian)
>> 
>> diff --git grdc.6 grdc.6
>> index 16c1fb5cd3d..69febddc9a2 100644
>> --- grdc.6
>> +++ grdc.6
>> @@ -30,6 +30,13 @@ skips seconds.
>>  Pressing the
>>  .Sq q
>>  key exits the program.
>> +.Sh ENVIRONMENT
>> +.Bl -tag -width "daemon_timeout"
>> +.It Ev TZ
>> +Time shown will be from zone as found under /usr/share/zonefile.
>
> This should be
> .Pa /usr/share/zonefile .

Good catch, I have adapted the text from date(1) for this, I think it
flows nicer and dropped the example.

Update diff with a local variable for getenv(3) and pulled the call up,
it's not going to change during runtime.

OK?

diff --git grdc.6 grdc.6
index 16c1fb5cd3d..a7258488e5a 100644
--- grdc.6
+++ grdc.6
@@ -30,6 +30,15 @@ skips seconds.
 Pressing the
 .Sq q
 key exits the program.
+.Sh ENVIRONMENT
+.Bl -tag -width Ds
+.It Ev TZ
+The time zone to use for displaying the time.
+It is specified as a pathname relative to
+.Pa /usr/share/zoneinfo .
+If this variable is not set, the time zone is determined based on
+.Pa /etc/localtime .
+.El
 .Sh AUTHORS
 .An -nosplit
 .An Amos Shapir ,
diff --git grdc.c grdc.c
index 287fb14e95f..94f80a865c0 100644
--- grdc.c
+++ grdc.c
@@ -78,6 +78,9 @@ main(int argc, char *argv[])
int xbase;
int ybase;
int wintoosmall;
+   char *tz;
+
+   tz = getenv("TZ");
 
scrol = wintoosmall = 0;
while ((i = getopt(argc, argv, "sh")) != -1) {
@@ -139,6 +142,16 @@ main(int argc, char *argv[])
alarm(n);
}
do {
+   mask = 0;
+   tm = localtime(_sec);
+   set(tm->tm_sec % 10, 0);
+   set(tm->tm_sec / 10, 4);
+   set(tm->tm_min % 10, 10);
+   set(tm->tm_min / 10, 14);
+   set(tm->tm_hour % 10, 20);
+   set(tm->tm_hour / 10, 24);
+   set(10, 7);
+   set(10, 17);
if (sigwinched) {
sigwinched = 0;
wintoosmall = 0;
@@ -171,21 +184,17 @@ main(int argc, char *argv[])
move(ybase, xbase + XLENGTH);
vline(ACS_VLINE, YDEPTH);
 
+   if (tz != NULL) {
+   move(ybase - 1, xbase);
+   printw("[ %s %+d ]", tz,
+   tm->tm_gmtoff / 60 / 60 );
+   }
+
attrset(COLOR_PAIR(2));
}
for (k = 0; k < 6; k++)
old[k] = 0;
}
-   mask = 0;
-   tm = localtime(_sec);
-   set(tm->tm_sec % 10, 0);
-   set(tm->tm_sec / 10, 4);
-   set(tm->tm_min % 10, 10);
-   set(tm->tm_min / 10, 14);
-   set(tm->tm_hour % 10, 20);
-   set(tm->tm_hour / 10, 24);
-   set(10, 7);
-   set(10, 17);
if (wintoosmall) {
move(0, 0);
printw("%02d:%02d:%02d", tm->tm_hour, tm->tm_min,





-- 
I'm not entirely sure you are real.



Re: grdc: show timezone when TZ is set

2022-09-17 Thread Klemens Nanni
On Sat, Sep 17, 2022 at 09:40:27AM +0200, Florian Obser wrote:
> On 2021-10-24 03:06 +02, James Russell Stickney  wrote:
> > I recently found myself wanting to moniter local time from a number of 
> > locations around the world.
> > Setting the TZ environment variable on grdc did a wonderfull job at this.
> > At which point, I wanted to know which clock was showing what time.
> > This lead to the following patch.
> >
> > If curious as to how this looked, it was sort of like the image below.
> > https://nl1.outband.net/image/grdc_desktop_example.png
> >
> 
> I recently found a use for this as well and remembered this diff. One
> long line re-wrapped.
> 
> OK?

Reads fine, but I don't use this "game".

> 
> (Or commit it with OK florian)
> 
> diff --git grdc.6 grdc.6
> index 16c1fb5cd3d..69febddc9a2 100644
> --- grdc.6
> +++ grdc.6
> @@ -30,6 +30,13 @@ skips seconds.
>  Pressing the
>  .Sq q
>  key exits the program.
> +.Sh ENVIRONMENT
> +.Bl -tag -width "daemon_timeout"
> +.It Ev TZ
> +Time shown will be from zone as found under /usr/share/zonefile.

This should be
.Pa /usr/share/zonefile .

> +For Example:
> +TZ=Asia/Tokyo grdc
> +.El
>  .Sh AUTHORS
>  .An -nosplit
>  .An Amos Shapir ,
> diff --git grdc.c grdc.c
> index 287fb14e95f..f8521a5029d 100644
> --- grdc.c
> +++ grdc.c
> @@ -139,6 +139,16 @@ main(int argc, char *argv[])
>   alarm(n);
>   }
>   do {
> + mask = 0;
> + tm = localtime(_sec);
> + set(tm->tm_sec % 10, 0);
> + set(tm->tm_sec / 10, 4);
> + set(tm->tm_min % 10, 10);
> + set(tm->tm_min / 10, 14);
> + set(tm->tm_hour % 10, 20);
> + set(tm->tm_hour / 10, 24);
> + set(10, 7);
> + set(10, 17);
>   if (sigwinched) {
>   sigwinched = 0;
>   wintoosmall = 0;
> @@ -171,21 +181,17 @@ main(int argc, char *argv[])
>   move(ybase, xbase + XLENGTH);
>   vline(ACS_VLINE, YDEPTH);
>  
> + if (getenv("TZ") ) {

You could assign the return value here...

> + move(ybase - 1, xbase);
> + printw("[ %s %+d ]", getenv("TZ"),

... and reuse it here.

> + tm->tm_gmtoff / 60 / 60 );
> + }
> +
>   attrset(COLOR_PAIR(2));
>   }
>   for (k = 0; k < 6; k++)
>   old[k] = 0;
>   }
> - mask = 0;
> - tm = localtime(_sec);
> - set(tm->tm_sec % 10, 0);
> - set(tm->tm_sec / 10, 4);
> - set(tm->tm_min % 10, 10);
> - set(tm->tm_min / 10, 14);
> - set(tm->tm_hour % 10, 20);
> - set(tm->tm_hour / 10, 24);
> - set(10, 7);
> - set(10, 17);
>   if (wintoosmall) {
>   move(0, 0);
>   printw("%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
> 
> 
> -- 
> I'm not entirely sure you are real.
> 



Re: grdc: show timezone when TZ is set

2022-09-17 Thread Florian Obser
On 2021-10-24 03:06 +02, James Russell Stickney  wrote:
> I recently found myself wanting to moniter local time from a number of 
> locations around the world.
> Setting the TZ environment variable on grdc did a wonderfull job at this.
> At which point, I wanted to know which clock was showing what time.
> This lead to the following patch.
>
> If curious as to how this looked, it was sort of like the image below.
> https://nl1.outband.net/image/grdc_desktop_example.png
>

I recently found a use for this as well and remembered this diff. One
long line re-wrapped.

OK?

(Or commit it with OK florian)

diff --git grdc.6 grdc.6
index 16c1fb5cd3d..69febddc9a2 100644
--- grdc.6
+++ grdc.6
@@ -30,6 +30,13 @@ skips seconds.
 Pressing the
 .Sq q
 key exits the program.
+.Sh ENVIRONMENT
+.Bl -tag -width "daemon_timeout"
+.It Ev TZ
+Time shown will be from zone as found under /usr/share/zonefile.
+For Example:
+TZ=Asia/Tokyo grdc
+.El
 .Sh AUTHORS
 .An -nosplit
 .An Amos Shapir ,
diff --git grdc.c grdc.c
index 287fb14e95f..f8521a5029d 100644
--- grdc.c
+++ grdc.c
@@ -139,6 +139,16 @@ main(int argc, char *argv[])
alarm(n);
}
do {
+   mask = 0;
+   tm = localtime(_sec);
+   set(tm->tm_sec % 10, 0);
+   set(tm->tm_sec / 10, 4);
+   set(tm->tm_min % 10, 10);
+   set(tm->tm_min / 10, 14);
+   set(tm->tm_hour % 10, 20);
+   set(tm->tm_hour / 10, 24);
+   set(10, 7);
+   set(10, 17);
if (sigwinched) {
sigwinched = 0;
wintoosmall = 0;
@@ -171,21 +181,17 @@ main(int argc, char *argv[])
move(ybase, xbase + XLENGTH);
vline(ACS_VLINE, YDEPTH);
 
+   if (getenv("TZ") ) {
+   move(ybase - 1, xbase);
+   printw("[ %s %+d ]", getenv("TZ"),
+   tm->tm_gmtoff / 60 / 60 );
+   }
+
attrset(COLOR_PAIR(2));
}
for (k = 0; k < 6; k++)
old[k] = 0;
}
-   mask = 0;
-   tm = localtime(_sec);
-   set(tm->tm_sec % 10, 0);
-   set(tm->tm_sec / 10, 4);
-   set(tm->tm_min % 10, 10);
-   set(tm->tm_min / 10, 14);
-   set(tm->tm_hour % 10, 20);
-   set(tm->tm_hour / 10, 24);
-   set(10, 7);
-   set(10, 17);
if (wintoosmall) {
move(0, 0);
printw("%02d:%02d:%02d", tm->tm_hour, tm->tm_min,


-- 
I'm not entirely sure you are real.