]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/prettydate.pm
update
[git.ikiwiki.info.git] / IkiWiki / Plugin / prettydate.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::prettydate;
3 use IkiWiki 3.00;
4 use warnings;
5 no warnings 'redefine';
6 use strict;
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
37                 gettext("%A night"),                            # 10
38                 "",                                             # 11
39         ];
40 }
42 sub import {
43         hook(type => "getsetup", id => "prettydate", call => \&getsetup);
44         hook(type => "checkconfig", id => "prettydate", call => \&checkconfig);
45 }
47 sub getsetup () {
48         return
49                 plugin => {
50                         safe => 1,
51                         rebuild => 1,
52                 },
53                 prettydateformat => {
54                         type => "string",
55                         example => '%X, %B %o, %Y',
56                         description => "format to use to display date",
57                         advanced => 1,
58                         safe => 1,
59                         rebuild => 1,
60                 },
61                 timetable => {
62                         type => "internal",
63                         description => "array of time descriptions",
64                         safe => 1,
65                         rebuild => 1,
66                 },
67 }
69 sub checkconfig () {
70         if (! defined $config{prettydateformat} ||
71             $config{prettydateformat} eq '%c') {
72                 $config{prettydateformat}='%X, %B %o, %Y';
73         }
75         if (! ref $config{timetable}) {
76                 $config{timetable}=default_timetable();
77         }
79         # Fill in the blanks.
80         for (my $h=0; $h < 24; $h++) {
81                 if (! length $config{timetable}[$h]) {
82                         $config{timetable}[$h] = $config{timetable}[$h - 1];
83                 }
84         }
85 }
87 sub IkiWiki::formattime ($;$) {
88         my $time=shift;
89         my $format=shift;
90         if (! defined $format) {
91                 $format=$config{prettydateformat};
92         }
93         
94         eval q{use Date::Format};
95         error($@) if $@;
97         my @t=localtime($time);
98         my ($h, $m, $wday)=@t[2, 1, 6];
99         my $t;
100         if ($h == 16 && $m < 30) {
101                 $t = gettext("at teatime on %A");
102         }
103         elsif (($h == 0 && $m < 30) || ($h == 23 && $m > 50)) {
104                 # well, at 40 minutes it's more like the martian timeslip..
105                 $t = gettext("at midnight");
106         }
107         elsif (($h == 12 && $m < 15) || ($h == 11 && $m > 50)) {
108                 $t = gettext("at noon on %A");
109         }
110         # TODO: sunrise and sunset, but to be right I need to do it based on
111         # lat and long, and calculate the appropriate one for the actual
112         # time of year using Astro::Sunrise. Not tonight, it's wee hours
113         # already..
114         else {
115                 $t = $config{timetable}[$h];
116                 if (! length $t) {
117                         $t = "sometime";
118                 }
119         }
121         $t=~s{\%A-}{my @yest=@t; $yest[6]--; strftime("%A", \@yest)}eg;
123         $format=~s/\%X/$t/g;
124         return strftime($format, \@t);