2007-02-15 08:53:04 +01:00
|
|
|
#!/usr/bin/perl
|
|
|
|
package IkiWiki::Plugin::prettydate;
|
2008-12-23 22:34:19 +01:00
|
|
|
use IkiWiki 3.00;
|
2007-02-15 08:53:04 +01:00
|
|
|
use warnings;
|
2007-02-15 08:57:55 +01:00
|
|
|
no warnings 'redefine';
|
2007-02-15 08:53:04 +01:00
|
|
|
use strict;
|
|
|
|
|
2007-02-15 21:06:14 +01:00
|
|
|
sub default_timetable {
|
|
|
|
# Blanks duplicate the time before.
|
|
|
|
return [
|
|
|
|
#translators: These descriptions of times of day are used
|
|
|
|
#translators: in messages like "last edited <description>".
|
|
|
|
#translators: %A is the name of the day of the week, while
|
|
|
|
#translators: %A- is the name of the previous day.
|
|
|
|
gettext("late %A- night"), # 12
|
|
|
|
"", # 1
|
|
|
|
gettext("in the wee hours of %A- night"), # 2
|
|
|
|
"", # 3
|
|
|
|
"", # 4
|
|
|
|
gettext("terribly early %A morning"), # 5
|
|
|
|
"", # 6
|
|
|
|
gettext("early %A morning"), # 7
|
|
|
|
"", # 8
|
|
|
|
"", # 9
|
2007-05-06 04:04:03 +02:00
|
|
|
gettext("mid-morning %A"), # 10
|
2007-02-15 21:54:52 +01:00
|
|
|
gettext("late %A morning"), # 11
|
2007-02-15 21:06:14 +01:00
|
|
|
gettext("at lunch time on %A"), # 12
|
|
|
|
"", # 1
|
|
|
|
gettext("%A afternoon"), # 2
|
|
|
|
"", # 3
|
|
|
|
"", # 4
|
|
|
|
gettext("late %A afternoon"), # 5
|
|
|
|
gettext("%A evening"), # 6
|
|
|
|
"", # 7
|
|
|
|
gettext("late %A evening"), # 8
|
2009-07-01 19:45:28 +02:00
|
|
|
"", # 9
|
2007-02-15 21:06:14 +01:00
|
|
|
gettext("%A night"), # 10
|
|
|
|
"", # 11
|
|
|
|
];
|
|
|
|
}
|
2007-02-15 08:53:04 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub import {
|
2008-07-26 06:38:13 +02:00
|
|
|
hook(type => "getsetup", id => "prettydate", call => \&getsetup);
|
2008-01-07 21:28:53 +01:00
|
|
|
hook(type => "checkconfig", id => "prettydate", call => \&checkconfig);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-02-15 08:53:04 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getsetup () {
|
2008-07-26 00:05:55 +02:00
|
|
|
return
|
2008-08-03 23:20:21 +02:00
|
|
|
plugin => {
|
|
|
|
safe => 1,
|
|
|
|
rebuild => 1,
|
|
|
|
},
|
2008-07-26 00:05:55 +02:00
|
|
|
prettydateformat => {
|
|
|
|
type => "string",
|
2008-07-27 03:07:15 +02:00
|
|
|
example => '%X, %B %o, %Y',
|
2008-07-26 00:05:55 +02:00
|
|
|
description => "format to use to display date",
|
2008-08-03 20:57:24 +02:00
|
|
|
advanced => 1,
|
2008-07-26 00:05:55 +02:00
|
|
|
safe => 1,
|
|
|
|
rebuild => 1,
|
|
|
|
},
|
|
|
|
timetable => {
|
2008-07-27 01:10:11 +02:00
|
|
|
type => "internal",
|
2008-07-26 00:05:55 +02:00
|
|
|
description => "array of time descriptions",
|
|
|
|
safe => 1,
|
|
|
|
rebuild => 1,
|
|
|
|
},
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-07-26 00:05:55 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub checkconfig () {
|
2007-02-15 08:53:04 +01:00
|
|
|
if (! defined $config{prettydateformat} ||
|
|
|
|
$config{prettydateformat} eq '%c') {
|
2007-02-15 21:06:14 +01:00
|
|
|
$config{prettydateformat}='%X, %B %o, %Y';
|
2007-02-15 08:53:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (! ref $config{timetable}) {
|
2007-02-15 21:06:14 +01:00
|
|
|
$config{timetable}=default_timetable();
|
2007-02-15 08:53:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Fill in the blanks.
|
|
|
|
for (my $h=0; $h < 24; $h++) {
|
|
|
|
if (! length $config{timetable}[$h]) {
|
|
|
|
$config{timetable}[$h] = $config{timetable}[$h - 1];
|
|
|
|
}
|
|
|
|
}
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-02-15 08:53:04 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub IkiWiki::formattime ($;$) {
|
2007-02-15 08:53:04 +01:00
|
|
|
my $time=shift;
|
2008-01-29 05:48:27 +01:00
|
|
|
my $format=shift;
|
|
|
|
if (! defined $format) {
|
|
|
|
$format=$config{prettydateformat};
|
|
|
|
}
|
2007-02-15 21:06:14 +01:00
|
|
|
|
|
|
|
eval q{use Date::Format};
|
|
|
|
error($@) if $@;
|
2007-02-15 08:53:04 +01:00
|
|
|
|
|
|
|
my @t=localtime($time);
|
2007-02-15 21:06:14 +01:00
|
|
|
my ($h, $m, $wday)=@t[2, 1, 6];
|
|
|
|
my $t;
|
2007-02-15 08:53:04 +01:00
|
|
|
if ($h == 16 && $m < 30) {
|
2007-02-15 21:06:14 +01:00
|
|
|
$t = gettext("at teatime on %A");
|
2007-02-15 08:53:04 +01:00
|
|
|
}
|
|
|
|
elsif (($h == 0 && $m < 30) || ($h == 23 && $m > 50)) {
|
|
|
|
# well, at 40 minutes it's more like the martian timeslip..
|
2007-02-15 21:06:14 +01:00
|
|
|
$t = gettext("at midnight");
|
2007-02-15 08:53:04 +01:00
|
|
|
}
|
|
|
|
elsif (($h == 12 && $m < 15) || ($h == 11 && $m > 50)) {
|
2007-02-15 21:06:14 +01:00
|
|
|
$t = gettext("at noon on %A");
|
2007-02-15 08:53:04 +01:00
|
|
|
}
|
|
|
|
# TODO: sunrise and sunset, but to be right I need to do it based on
|
|
|
|
# lat and long, and calculate the appropriate one for the actual
|
|
|
|
# time of year using Astro::Sunrise. Not tonight, it's wee hours
|
|
|
|
# already..
|
|
|
|
else {
|
2007-02-15 21:06:14 +01:00
|
|
|
$t = $config{timetable}[$h];
|
|
|
|
if (! length $t) {
|
|
|
|
$t = "sometime";
|
2007-02-15 08:53:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-03 03:27:55 +01:00
|
|
|
$t=~s{\%A-}{my @yest=@t; $yest[6]--; strftime("%A", \@yest)}eg;
|
2007-02-15 21:06:14 +01:00
|
|
|
|
|
|
|
$format=~s/\%X/$t/g;
|
2012-02-03 03:27:55 +01:00
|
|
|
return strftime($format, \@t);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-02-15 08:53:04 +01:00
|
|
|
|
|
|
|
1
|