Skip to content

Commit 9abe517

Browse files
committed
time: update to use injected properties
See vivien#132
1 parent 32228cb commit 9abe517

File tree

3 files changed

+95
-82
lines changed

3 files changed

+95
-82
lines changed

Diff for: time/README.md

+15-27
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,23 @@ To use with i3blocks, copy the below configuration into your i3blocks configurat
1414

1515
```INI
1616
[time]
17-
instance=%Y-%m-%d %H:%M
18-
command=$SCRIPT_DIR/time [/path/to/tz/file]
17+
command=$SCRIPT_DIR/time
1918
interval=1
19+
#TZ_FILE=~/.tz
20+
STRFTIME_FORMAT=%Y-%m-%d %H:%M
21+
TZONES=$DEFAULT_TZ,Brazil/East,Australia/Brisbane,Asia/Calcutta
22+
TZ_LABELS=,Brazil,AU,Hyderabad
2023
```
2124

22-
Instance is an optional time format string. See [strftime](https://linux.die.net/man/3/strftime).
23-
If `/path/to/tz/file` is omitted, the script uses `$HOME/.tz` by default.
25+
See [strftime](https://linux.die.net/man/3/strftime) for allowed strftime formats.
2426

25-
# Configuration
27+
Here TZONES is a comma separated list of timeszones, see /usr/share/zoneinfo (Olson DB)
28+
for allowed timezones. One exception is that the string $DEFAULT_TZ
29+
is also allowed and represents whatever your current system timezone is.
2630

27-
In the script there are two hashes that control the timezones used and the way they are displayed.
28-
29-
This hash defines the timezones that are switched to when clicking (ie. clicking when displaying Europe/London switches to Brazil/East)
30-
```perl
31-
my %tzmap = (
32-
"" => $default_tz,
33-
$default_tz => "Brazil/East",
34-
"Brazil/East" => "Australia/Brisbane",
35-
"Australia/Brisbane" => "Asia/Calcutta",
36-
"Asia/Calcutta" => $default_tz,
37-
);
38-
```
39-
40-
This hash defines how each timezone should be displayed (e.g. Australia/Brisbane displays as "AU")
41-
```perl
42-
my %display_map = (
43-
$default_tz => "Home",
44-
"Brazil/East" => "Brazil",
45-
"Australia/Brisbane" => "AU",
46-
"Asia/Calcutta" => "Hyderabad",
47-
);
48-
```
31+
Also TZ_LABELS is a comma separated list of how to label each timezone in case you prefer
32+
not to see the full timezone as part of the label.
33+
E.g. you may want it to say "12:34 (US)" instead of "12:34 (America/Chicago)".
34+
Labels are allowed to be empty,
35+
in which case the script omits parentheses.
36+
For example, in the config above, the label for the default timezone is omitted.

Diff for: time/i3blocks.conf

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[time]
2+
command=$SCRIPT_DIR/time
3+
interval=1
4+
#TZ_FILE=~/.tz
5+
STRFTIME_FORMAT=%Y-%m-%d %H:%M
6+
TZONES=$DEFAULT_TZ,Brazil/East,Australia/Brisbane,Asia/Calcutta
7+
TZ_LABELS=,Brazil,AU,Hyderabad

Diff for: time/time

+73-55
Original file line numberDiff line numberDiff line change
@@ -5,78 +5,96 @@ use warnings;
55
use POSIX qw/strftime/;
66

77
my $click = $ENV{BLOCK_BUTTON} || 0;
8-
my $format = $ENV{BLOCK_INSTANCE};
9-
my $tz_file = shift || "$ENV{HOME}/.tz";
8+
my $format = $ENV{BLOCK_INSTANCE} || $ENV{STRFTIME_FORMAT} || "%H:%M";
9+
my $tz_file = shift || $ENV{TZ_FILE} || "$ENV{HOME}/.tz";
10+
$tz_file = glob($tz_file);
11+
my $default_tz = get_default_tz();
12+
13+
my $tzones = $ENV{TZONES} || '$DEFAULT_TZ';
14+
$tzones =~ s/\$DEFAULT_TZ/$default_tz/g;
15+
my @tz_list = split(/,/, $tzones);
16+
my @tz_labels = split(/,/, $ENV{TZ_LABELS} || "");
17+
if (scalar(@tz_list) != scalar(@tz_labels)) {
18+
@tz_labels = @tz_list;
19+
}
1020

1121
my $current_tz;
12-
my $default_tz = get_default_tz();
1322
if ($click == 1) {
14-
$current_tz = get_tz();
15-
16-
# List of timezones. Make sure the list loops back to itself.
17-
# Entries must be present in /usr/share/zoneinfo (Olson DB).
18-
my %tzmap = (
19-
"" => $default_tz,
20-
$default_tz => "Brazil/East",
21-
"Brazil/East" => "Australia/Brisbane",
22-
"Australia/Brisbane" => "Asia/Calcutta",
23-
"Asia/Calcutta" => $default_tz,
24-
);
25-
26-
if (exists $tzmap{$current_tz}) {
27-
set_tz($tzmap{$current_tz});
28-
$current_tz = $tzmap{$current_tz};
29-
}
23+
$current_tz = get_tz();
24+
25+
my %tzmap;
26+
$tzmap{""} = $tz_list[0];
27+
my $prev = $tz_list[0];
28+
foreach my $tz (@tz_list) {
29+
$tzmap{$prev} = $tz;
30+
$prev = $tz;
31+
}
32+
$tzmap{$prev} = $tz_list[0];
33+
34+
if (exists $tzmap{$current_tz}) {
35+
set_tz($tzmap{$current_tz});
36+
$current_tz = $tzmap{$current_tz};
37+
}
3038
}
3139

3240
# How each timezone will be displayed in the bar.
33-
my %display_map = (
34-
$default_tz => "Home",
35-
"Brazil/East" => "Brazil",
36-
"Australia/Brisbane" => "AU",
37-
"Asia/Calcutta" => "Hyderabad",
38-
);
39-
40-
$ENV{TZ} = $current_tz || get_tz();
41-
my $tz_display = $display_map{$ENV{TZ}} || $ENV{TZ};
41+
my %display_map;
42+
for (my $i=0; $i < scalar(@tz_list); $i++) {
43+
$display_map{$tz_list[$i]} = $tz_labels[$i];
44+
}
4245

43-
my $time = $format ? strftime($format, localtime()) : localtime();
44-
print "$time ($tz_display)";
46+
if (!defined $current_tz) {
47+
$current_tz = get_tz();
48+
set_tz($current_tz);
49+
}
50+
$ENV{TZ} = $current_tz;
51+
my $tz_display = "";
52+
if (!exists $display_map{$ENV{TZ}}) {
53+
$ENV{TZ} = $tz_list[0];
54+
set_tz($tz_list[0]);
55+
}
56+
$tz_display = $display_map{$ENV{TZ}};
4557

58+
my $time = strftime($format, localtime());
59+
if ($tz_display eq "") {
60+
print "$time\n";
61+
} else {
62+
print "$time ($tz_display)\n";
63+
}
4664

4765
sub get_tz {
48-
my $current_tz;
66+
my $current_tz;
4967

50-
if (-f "$ENV{HOME}/.tz") {
51-
open my $fh, '<', $tz_file;
52-
$current_tz = <$fh>;
53-
chomp $current_tz;
54-
close $fh;
55-
}
68+
if (-f $tz_file) {
69+
open my $fh, '<', $tz_file || die "Couldn't open file: $tz_file";
70+
$current_tz = <$fh>;
71+
chomp $current_tz;
72+
close $fh;
73+
}
5674

57-
return $current_tz || get_default_tz();
75+
return $current_tz || get_default_tz();
5876
}
5977

6078
sub set_tz {
61-
my $tz = shift;
79+
my $tz = shift;
6280

63-
open my $fh, '>', $tz_file;
64-
print $fh $tz;
65-
close $fh;
81+
open my $fh, '>', $tz_file || die "Couldn't open file: $tz_file";
82+
print $fh $tz;
83+
close $fh;
6684
}
6785

6886
sub get_default_tz {
69-
my $tz = "Europe/London";
70-
71-
if (-f "/etc/timezone") {
72-
open my $fh, '<', "/etc/timezone";
73-
$tz = <$fh>;
74-
chomp $tz;
75-
close $fh;
76-
} elsif (-l "/etc/localtime") {
77-
$tz = readlink "/etc/localtime";
78-
$tz = (split /zoneinfo\//, $tz)[-1];
79-
}
80-
81-
return $tz;
87+
my $tz = "Europe/London";
88+
89+
if (-f "/etc/timezone") {
90+
open my $fh, '<', "/etc/timezone" || die "Couldn't open file: /etc/timezone";
91+
$tz = <$fh>;
92+
chomp $tz;
93+
close $fh;
94+
} elsif (-l "/etc/localtime") {
95+
$tz = readlink "/etc/localtime";
96+
$tz = (split /zoneinfo\//, $tz)[-1];
97+
}
98+
99+
return $tz;
82100
}

0 commit comments

Comments
 (0)