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