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