@@ -5,78 +5,96 @@ use warnings;
5
5
use POSIX qw/ strftime/ ;
6
6
7
7
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
+ }
10
20
11
21
my $current_tz ;
12
- my $default_tz = get_default_tz();
13
22
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
+ }
30
38
}
31
39
32
40
# 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
+ }
42
45
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 }};
45
57
58
+ my $time = strftime($format , localtime ());
59
+ if ($tz_display eq " " ) {
60
+ print " $time \n " ;
61
+ } else {
62
+ print " $time ($tz_display )\n " ;
63
+ }
46
64
47
65
sub get_tz {
48
- my $current_tz ;
66
+ my $current_tz ;
49
67
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
+ }
56
74
57
- return $current_tz || get_default_tz();
75
+ return $current_tz || get_default_tz();
58
76
}
59
77
60
78
sub set_tz {
61
- my $tz = shift ;
79
+ my $tz = shift ;
62
80
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 ;
66
84
}
67
85
68
86
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 ;
82
100
}
0 commit comments