summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/prettydate.pm
blob: 745e6a1de436e2db15b97ebe897858f891de2a51 (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. my $format=shift;
  61. if (! defined $format) {
  62. $format=$config{prettydateformat};
  63. }
  64. eval q{use Date::Format};
  65. error($@) if $@;
  66. my @t=localtime($time);
  67. my ($h, $m, $wday)=@t[2, 1, 6];
  68. my $t;
  69. if ($h == 16 && $m < 30) {
  70. $t = gettext("at teatime on %A");
  71. }
  72. elsif (($h == 0 && $m < 30) || ($h == 23 && $m > 50)) {
  73. # well, at 40 minutes it's more like the martian timeslip..
  74. $t = gettext("at midnight");
  75. }
  76. elsif (($h == 12 && $m < 15) || ($h == 11 && $m > 50)) {
  77. $t = gettext("at noon on %A");
  78. }
  79. # TODO: sunrise and sunset, but to be right I need to do it based on
  80. # lat and long, and calculate the appropriate one for the actual
  81. # time of year using Astro::Sunrise. Not tonight, it's wee hours
  82. # already..
  83. else {
  84. $t = $config{timetable}[$h];
  85. if (! length $t) {
  86. $t = "sometime";
  87. }
  88. }
  89. $t=~s{\%A-}{my @yest=@t; $yest[6]--; strftime("%A", \@yest)}eg;
  90. $format=~s/\%X/$t/g;
  91. return strftime($format, \@t);
  92. } #}}}
  93. 1