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