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