Skip to content

Commit

Permalink
time: update to use injected properties
Browse files Browse the repository at this point in the history
  • Loading branch information
kb100 committed Aug 24, 2018
1 parent 32228cb commit 9abe517
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 82 deletions.
42 changes: 15 additions & 27 deletions time/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,23 @@ To use with i3blocks, copy the below configuration into your i3blocks configurat

```INI
[time]
instance=%Y-%m-%d %H:%M
command=$SCRIPT_DIR/time [/path/to/tz/file]
command=$SCRIPT_DIR/time
interval=1
#TZ_FILE=~/.tz
STRFTIME_FORMAT=%Y-%m-%d %H:%M
TZONES=$DEFAULT_TZ,Brazil/East,Australia/Brisbane,Asia/Calcutta
TZ_LABELS=,Brazil,AU,Hyderabad
```

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

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

In the script there are two hashes that control the timezones used and the way they are displayed.

This hash defines the timezones that are switched to when clicking (ie. clicking when displaying Europe/London switches to Brazil/East)
```perl
my %tzmap = (
"" => $default_tz,
$default_tz => "Brazil/East",
"Brazil/East" => "Australia/Brisbane",
"Australia/Brisbane" => "Asia/Calcutta",
"Asia/Calcutta" => $default_tz,
);
```

This hash defines how each timezone should be displayed (e.g. Australia/Brisbane displays as "AU")
```perl
my %display_map = (
$default_tz => "Home",
"Brazil/East" => "Brazil",
"Australia/Brisbane" => "AU",
"Asia/Calcutta" => "Hyderabad",
);
```
Also TZ_LABELS is a comma separated list of how to label each timezone in case you prefer
not to see the full timezone as part of the label.
E.g. you may want it to say "12:34 (US)" instead of "12:34 (America/Chicago)".
Labels are allowed to be empty,
in which case the script omits parentheses.
For example, in the config above, the label for the default timezone is omitted.
7 changes: 7 additions & 0 deletions time/i3blocks.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[time]
command=$SCRIPT_DIR/time
interval=1
#TZ_FILE=~/.tz
STRFTIME_FORMAT=%Y-%m-%d %H:%M
TZONES=$DEFAULT_TZ,Brazil/East,Australia/Brisbane,Asia/Calcutta
TZ_LABELS=,Brazil,AU,Hyderabad
128 changes: 73 additions & 55 deletions time/time
Original file line number Diff line number Diff line change
Expand Up @@ -5,78 +5,96 @@ use warnings;
use POSIX qw/strftime/;

my $click = $ENV{BLOCK_BUTTON} || 0;
my $format = $ENV{BLOCK_INSTANCE};
my $tz_file = shift || "$ENV{HOME}/.tz";
my $format = $ENV{BLOCK_INSTANCE} || $ENV{STRFTIME_FORMAT} || "%H:%M";
my $tz_file = shift || $ENV{TZ_FILE} || "$ENV{HOME}/.tz";
$tz_file = glob($tz_file);
my $default_tz = get_default_tz();

my $tzones = $ENV{TZONES} || '$DEFAULT_TZ';
$tzones =~ s/\$DEFAULT_TZ/$default_tz/g;
my @tz_list = split(/,/, $tzones);
my @tz_labels = split(/,/, $ENV{TZ_LABELS} || "");
if (scalar(@tz_list) != scalar(@tz_labels)) {
@tz_labels = @tz_list;
}

my $current_tz;
my $default_tz = get_default_tz();
if ($click == 1) {
$current_tz = get_tz();

# List of timezones. Make sure the list loops back to itself.
# Entries must be present in /usr/share/zoneinfo (Olson DB).
my %tzmap = (
"" => $default_tz,
$default_tz => "Brazil/East",
"Brazil/East" => "Australia/Brisbane",
"Australia/Brisbane" => "Asia/Calcutta",
"Asia/Calcutta" => $default_tz,
);

if (exists $tzmap{$current_tz}) {
set_tz($tzmap{$current_tz});
$current_tz = $tzmap{$current_tz};
}
$current_tz = get_tz();

my %tzmap;
$tzmap{""} = $tz_list[0];
my $prev = $tz_list[0];
foreach my $tz (@tz_list) {
$tzmap{$prev} = $tz;
$prev = $tz;
}
$tzmap{$prev} = $tz_list[0];

if (exists $tzmap{$current_tz}) {
set_tz($tzmap{$current_tz});
$current_tz = $tzmap{$current_tz};
}
}

# How each timezone will be displayed in the bar.
my %display_map = (
$default_tz => "Home",
"Brazil/East" => "Brazil",
"Australia/Brisbane" => "AU",
"Asia/Calcutta" => "Hyderabad",
);

$ENV{TZ} = $current_tz || get_tz();
my $tz_display = $display_map{$ENV{TZ}} || $ENV{TZ};
my %display_map;
for (my $i=0; $i < scalar(@tz_list); $i++) {
$display_map{$tz_list[$i]} = $tz_labels[$i];
}

my $time = $format ? strftime($format, localtime()) : localtime();
print "$time ($tz_display)";
if (!defined $current_tz) {
$current_tz = get_tz();
set_tz($current_tz);
}
$ENV{TZ} = $current_tz;
my $tz_display = "";
if (!exists $display_map{$ENV{TZ}}) {
$ENV{TZ} = $tz_list[0];
set_tz($tz_list[0]);
}
$tz_display = $display_map{$ENV{TZ}};

my $time = strftime($format, localtime());
if ($tz_display eq "") {
print "$time\n";
} else {
print "$time ($tz_display)\n";
}

sub get_tz {
my $current_tz;
my $current_tz;

if (-f "$ENV{HOME}/.tz") {
open my $fh, '<', $tz_file;
$current_tz = <$fh>;
chomp $current_tz;
close $fh;
}
if (-f $tz_file) {
open my $fh, '<', $tz_file || die "Couldn't open file: $tz_file";
$current_tz = <$fh>;
chomp $current_tz;
close $fh;
}

return $current_tz || get_default_tz();
return $current_tz || get_default_tz();
}

sub set_tz {
my $tz = shift;
my $tz = shift;

open my $fh, '>', $tz_file;
print $fh $tz;
close $fh;
open my $fh, '>', $tz_file || die "Couldn't open file: $tz_file";
print $fh $tz;
close $fh;
}

sub get_default_tz {
my $tz = "Europe/London";

if (-f "/etc/timezone") {
open my $fh, '<', "/etc/timezone";
$tz = <$fh>;
chomp $tz;
close $fh;
} elsif (-l "/etc/localtime") {
$tz = readlink "/etc/localtime";
$tz = (split /zoneinfo\//, $tz)[-1];
}

return $tz;
my $tz = "Europe/London";

if (-f "/etc/timezone") {
open my $fh, '<', "/etc/timezone" || die "Couldn't open file: /etc/timezone";
$tz = <$fh>;
chomp $tz;
close $fh;
} elsif (-l "/etc/localtime") {
$tz = readlink "/etc/localtime";
$tz = (split /zoneinfo\//, $tz)[-1];
}

return $tz;
}

0 comments on commit 9abe517

Please sign in to comment.