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