]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/prettydate.pm
* Add a prettydate plugin that formats dates in a more readable fashion.
[ikiwiki.git] / IkiWiki / Plugin / prettydate.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::prettydate;
3 use IkiWiki;
4 use warnings;
5 use strict;
6
7 # Blanks duplicate the time before.
8 my $default_timetable=[
9         "late at night on",     # 12
10         "",                     # 1
11         "in the wee hours of",  # 2
12         "",                     # 3
13         "",                     # 4
14         "terribly early in the morning of", # 5
15         "",                     # 6
16         "in early morning on",  # 7
17         "",                     # 8
18         "",                     # 9
19         "in mid-morning of",    # 10
20         "in late morning of",   # 11
21         "at lunch time on",     # 12
22         "",                     # 1
23         "in the afternoon of",  # 2
24         "",                     # 3
25         "",                     # 4
26         "in late afternoon of", # 5
27         "in the evening of",    # 6
28         "",                     # 7
29         "in late evening on",   # 8
30         "",                     # 9
31         "at night on",          # 10
32         "",                     # 11
33 ];
34
35 sub import { #{{{
36         hook(type => "checkconfig", id => "skeleton", call => \&checkconfig);
37 } # }}}
38
39 sub checkconfig () { #{{{
40         if (! defined $config{prettydateformat} ||
41             $config{prettydateformat} eq '%c') {
42                 $config{prettydateformat}='%X %B %o, %Y';
43         }
44
45         if (! ref $config{timetable}) {
46                 $config{timetable}=$default_timetable;
47         }
48
49         # Fill in the blanks.
50         for (my $h=0; $h < 24; $h++) {
51                 if (! length $config{timetable}[$h]) {
52                         $config{timetable}[$h] = $config{timetable}[$h - 1];
53                 }
54         }
55 } #}}}
56
57 sub IkiWiki::displaytime ($) { #{{{
58         my $time=shift;
59
60         my @t=localtime($time);
61         my ($h, $m)=@t[2, 1];
62         if ($h == 16 && $m < 30) {
63                 $time = "at teatime on";
64         }
65         elsif (($h == 0 && $m < 30) || ($h == 23 && $m > 50)) {
66                 # well, at 40 minutes it's more like the martian timeslip..
67                 $time = "at midnight on";
68         }
69         elsif (($h == 12 && $m < 15) || ($h == 11 && $m > 50)) {
70                 $time = "at noon on";
71         }
72         # TODO: sunrise and sunset, but to be right I need to do it based on
73         # lat and long, and calculate the appropriate one for the actual
74         # time of year using Astro::Sunrise. Not tonight, it's wee hours
75         # already..
76         else {
77                 $time = $config{timetable}[$h];
78                 if (! length $time) {
79                         $time = "sometime";
80                 }
81         }
82
83         eval q{use Date::Format};
84         error($@) if $@;
85         my $format=$config{prettydateformat};
86         $format=~s/\%X/$time/g;
87         return strftime($format, \@t);
88 } #}}}
89
90 1