summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/prettydate.pm
blob: db5a94f413b567de7c73f2cb93ac90e1d9697461 (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 => "getsetup", id => "prettydate", call => \&getsetup);
  42. hook(type => "checkconfig", id => "prettydate", call => \&checkconfig);
  43. } # }}}
  44. sub getsetup () { #{{{
  45. return
  46. prettydateformat => {
  47. type => "string",
  48. example => '%X, %B %o, %Y',
  49. description => "format to use to display date",
  50. safe => 1,
  51. rebuild => 1,
  52. },
  53. timetable => {
  54. type => "internal",
  55. description => "array of time descriptions",
  56. safe => 1,
  57. rebuild => 1,
  58. },
  59. } #}}}
  60. sub checkconfig () { #{{{
  61. if (! defined $config{prettydateformat} ||
  62. $config{prettydateformat} eq '%c') {
  63. $config{prettydateformat}='%X, %B %o, %Y';
  64. }
  65. if (! ref $config{timetable}) {
  66. $config{timetable}=default_timetable();
  67. }
  68. # Fill in the blanks.
  69. for (my $h=0; $h < 24; $h++) {
  70. if (! length $config{timetable}[$h]) {
  71. $config{timetable}[$h] = $config{timetable}[$h - 1];
  72. }
  73. }
  74. } #}}}
  75. sub IkiWiki::displaytime ($;$) { #{{{
  76. my $time=shift;
  77. my $format=shift;
  78. if (! defined $format) {
  79. $format=$config{prettydateformat};
  80. }
  81. eval q{use Date::Format};
  82. error($@) if $@;
  83. my @t=localtime($time);
  84. my ($h, $m, $wday)=@t[2, 1, 6];
  85. my $t;
  86. if ($h == 16 && $m < 30) {
  87. $t = gettext("at teatime on %A");
  88. }
  89. elsif (($h == 0 && $m < 30) || ($h == 23 && $m > 50)) {
  90. # well, at 40 minutes it's more like the martian timeslip..
  91. $t = gettext("at midnight");
  92. }
  93. elsif (($h == 12 && $m < 15) || ($h == 11 && $m > 50)) {
  94. $t = gettext("at noon on %A");
  95. }
  96. # TODO: sunrise and sunset, but to be right I need to do it based on
  97. # lat and long, and calculate the appropriate one for the actual
  98. # time of year using Astro::Sunrise. Not tonight, it's wee hours
  99. # already..
  100. else {
  101. $t = $config{timetable}[$h];
  102. if (! length $t) {
  103. $t = "sometime";
  104. }
  105. }
  106. $t=~s{\%A-}{my @yest=@t; $yest[6]--; strftime("%A", \@yest)}eg;
  107. $format=~s/\%X/$t/g;
  108. return strftime($format, \@t);
  109. } #}}}
  110. 1