added getsetup hooks for all plugins up to recentchanges
[ikiwiki.git] / IkiWiki / Plugin / prettydate.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::prettydate;
3 use IkiWiki 2.00;
4 use warnings;
5 no warnings 'redefine';
6 use strict;
7
8 sub default_timetable {
9         # Blanks duplicate the time before.
10         return [
11                 #translators: These descriptions of times of day are used
12                 #translators: in messages like "last edited <description>".
13                 #translators: %A is the name of the day of the week, while
14                 #translators: %A- is the name of the previous day.
15                 gettext("late %A- night"),                      # 12
16                 "",                                             # 1
17                 gettext("in the wee hours of %A- night"),       # 2
18                 "",                                             # 3
19                 "",                                             # 4
20                 gettext("terribly early %A morning"),           # 5
21                 "",                                             # 6
22                 gettext("early %A morning"),                    # 7
23                 "",                                             # 8
24                 "",                                             # 9
25                 gettext("mid-morning %A"),                      # 10
26                 gettext("late %A morning"),                     # 11
27                 gettext("at lunch time on %A"),                 # 12
28                 "",                                             # 1
29                 gettext("%A afternoon"),                        # 2
30                 "",                                             # 3
31                 "",                                             # 4
32                 gettext("late %A afternoon"),                   # 5
33                 gettext("%A evening"),                          # 6
34                 "",                                             # 7
35                 gettext("late %A evening"),                     # 8
36                 "",                     # 9                     # 9
37                 gettext("%A night"),                            # 10
38                 "",                                             # 11
39         ];
40 }
41
42 sub import { #{{{
43         hook(type => "getsetup", id => "getsetup", call => \&getsetup);
44         hook(type => "checkconfig", id => "prettydate", call => \&checkconfig);
45 } # }}}
46
47 sub getsetup () { #{{{
48         return
49                 prettydateformat => {
50                         type => "string",
51                         default => '%X, %B %o, %Y',
52                         description => "format to use to display date",
53                         safe => 1,
54                         rebuild => 1,
55                 },
56                 timetable => {
57                         type => undef, # don't try to show in interface
58                         default => '%X, %B %o, %Y',
59                         description => "array of time descriptions",
60                         safe => 1,
61                         rebuild => 1,
62                 },
63 } #}}}
64
65 sub checkconfig () { #{{{
66         if (! defined $config{prettydateformat} ||
67             $config{prettydateformat} eq '%c') {
68                 $config{prettydateformat}='%X, %B %o, %Y';
69         }
70
71         if (! ref $config{timetable}) {
72                 $config{timetable}=default_timetable();
73         }
74
75         # Fill in the blanks.
76         for (my $h=0; $h < 24; $h++) {
77                 if (! length $config{timetable}[$h]) {
78                         $config{timetable}[$h] = $config{timetable}[$h - 1];
79                 }
80         }
81 } #}}}
82
83 sub IkiWiki::displaytime ($;$) { #{{{
84         my $time=shift;
85         my $format=shift;
86         if (! defined $format) {
87                 $format=$config{prettydateformat};
88         }
89         
90         eval q{use Date::Format};
91         error($@) if $@;
92
93         my @t=localtime($time);
94         my ($h, $m, $wday)=@t[2, 1, 6];
95         my $t;
96         if ($h == 16 && $m < 30) {
97                 $t = gettext("at teatime on %A");
98         }
99         elsif (($h == 0 && $m < 30) || ($h == 23 && $m > 50)) {
100                 # well, at 40 minutes it's more like the martian timeslip..
101                 $t = gettext("at midnight");
102         }
103         elsif (($h == 12 && $m < 15) || ($h == 11 && $m > 50)) {
104                 $t = gettext("at noon on %A");
105         }
106         # TODO: sunrise and sunset, but to be right I need to do it based on
107         # lat and long, and calculate the appropriate one for the actual
108         # time of year using Astro::Sunrise. Not tonight, it's wee hours
109         # already..
110         else {
111                 $t = $config{timetable}[$h];
112                 if (! length $t) {
113                         $t = "sometime";
114                 }
115         }
116
117         $t=~s{\%A-}{my @yest=@t; $yest[6]--; strftime("%A", \@yest)}eg;
118
119         $format=~s/\%X/$t/g;
120         return strftime($format, \@t);
121 } #}}}
122
123 1