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