1 # Blosxom Plugin: storydate
2 # Author(s): Gavin Carr <gavin@openfusion.com.au>
3 # (based on work by Frank Hecker <hecker@hecker.org>
4 # and Bob Schumaker <cobblers@pobox.com>)
6 # Documentation: See the bottom of this file or type: perldoc storydate
14 # Uncomment next line to enable debug output (don't uncomment debug() lines)
15 #use Blosxom::Debug debug_level => 1;
17 # --- Configuration variables -----
19 # Perl modules to use for strftime, in search order
20 my @strftime_modules = qw(Time::Piece Date::Format POSIX);
22 # ---------------------------------
36 rfc822 => "%a, %d %b %Y %T %z",
37 # ISO 8601 format (localtime, including time zone offset)
38 # Format is YYYY-MM-DDThh:mm:ssTZD per http://www.w3.org/TR/NOTE-datetime
39 iso8601 => "%Y-%m-%dT%T %z",
44 $now_rfc822 = format_date('rfc822', $now);
45 $now_iso8601 = format_date('iso8601', $now);
46 # debug(1, "now_rfc822: $now_rfc822");
47 # debug(1, "now_iso8601: $now_iso8601");
52 my ($pkg, $path, $filename, $story_ref, $title_ref, $body_ref) = @_;
54 ($story_rfc822, $story_mtime_rfc822)
55 = get_story_dates('rfc822', $path, $filename);
56 ($story_iso8601, $story_mtime_iso8601)
57 = get_story_dates('iso8601', $path, $filename);
62 # Get the 'file' (publish/official) and 'mtime' formatted dates for the given story file
64 my ($format, $path, $filename) = @_;
65 my ($file_dt, $mtime_dt) = ('', '');
68 my $story_file = "$blosxom::datadir$path/$filename.$blosxom::file_extension";
69 my $timestamp = $blosxom::files{ $story_file };
70 # debug(2, "$path/$filename timestamp: $timestamp");
72 if (my $stat = stat( $story_file )) {
73 if (my $mtime = $stat->mtime) {
74 # debug(2, "$path/$filename mtime: = $mtime");
76 $mtime_dt = format_date($format, $mtime);
78 $timestamp ||= $mtime;
81 warn "storydate: cannot get mtime on story file '$story_file'\n";
85 warn "storydate: cannot stat story file '$story_file'\n";
88 $file_dt = format_date($format, $timestamp) if $timestamp;
90 return wantarray ? ( $file_dt, $mtime_dt ) : $file_dt;
94 my ($format, $time, $use_gmtime) = @_;
95 return unless $format;
96 if ($format =~ m/%/) {
97 return strftime($format, $time, $use_gmtime);
99 elsif ($strftime_formats{ $format }) {
100 return strftime($strftime_formats{ $format }, $time, $use_gmtime);
104 # strftime wrapper, using the first strftime() it finds in @strftime_modules
106 my $strftime_module = '';
108 my ($format, $time, $use_gmtime) = @_;
109 return unless $format && $time;
110 $use_gmtime = 0 unless defined $use_gmtime;
113 my $cache_key = "$time:$use_gmtime:$format";
114 return $cache{ $cache_key } if exists $cache{ $cache_key };
117 $strftime_module = $ENV{BLOSXOM_STORYDATE_STRFTIME_MODULE};
119 # Search for a strftime module, and load
120 if (! $strftime_module) {
121 for my $module (@strftime_modules) {
122 if (eval "require $module") {
123 $strftime_module = $module;
124 # debug(2, "strftime_module: $strftime_module");
128 if (! $strftime_module) {
129 warn "storydate: cannot find any suitable strftime module\n";
135 if ($strftime_module eq 'Time::Piece') {
136 my $t = $use_gmtime ? Time::Piece->gmtime($time) : Time::Piece->localtime($time);
138 # Seems to be a bug in Time::Piece %z handling - workaround
139 my $tz_offset = sprintf("%+03d%02d", int($t->tzoffset / 3600), ($t->tzoffset % 3600) / 60);
140 $format =~ s/%z/$tz_offset/g;
142 $datetime = $t->strftime( $format );
145 elsif ($strftime_module eq 'Date::Format') {
146 my @t = $use_gmtime ? gmtime($time) : localtime($time);
147 $datetime = Date::Format::strftime($format, \@t);
150 elsif ($strftime_module eq 'POSIX') {
151 my @t = $use_gmtime ? gmtime($time) : localtime($time);
152 $datetime = POSIX::strftime($format, @t);
155 $cache{ $cache_key } = $datetime if $datetime;
166 storydate - blosxom plugin to provide story dates in various formats
167 for use by other plugins
172 storydate is a blosxom plugin that provides story dates in various
173 formats for use by other plugins. It defines the following variables:
179 The official/publication date of the story in RFC822 format.
183 The official/publication date of the story in ISO8601 format.
185 =item $story_mtime_rfc822
187 The last modification time of the story in RFC822 format.
189 =item $story_mtime_iso8601
191 The last modification time of the story in ISO8601 format.
195 The current time in RFC822 format.
199 The current time in ISO8601 format.
203 In addition, storydate defines a few subroutines that might be useful
208 =item strftime($format, $time)
210 Returns $time, formatted according to the L<strftime(3)> format
211 $format. Requires one of the following perl modules be available to
212 provide a strftime() function: Time::Piece, Date::Format, or POSIX.
214 =item get_story_dates($format, $path, $filename)
216 Returns the publication date and the last modified date of the story
217 at "$path/$filename.$blosxom::file_extension", formatted according
218 to the L<strftime(3)> format $format.
225 storydate should be loaded early, before any story plugins that want
233 Blosxom: http://blosxom.sourceforge.net/
236 =head1 ACKNOWLEDGEMENTS
238 storydate is based on Frank Hecker's lastmodified2 plugin, which is
239 itself based on Bob Schumaker's lastmodified plugin.
241 The problem with lastmodified2 is that its HTTP header functionality
242 requires that it runs very late, which makes it useless for providing
243 dates to story plugins. storydate is basically just all the parts of
244 lastmodified2 that can be run early pulled out into a separate plugin.
249 Gavin Carr <gavin@openfusion.com.au>, http://www.openfusion.net/
254 Copyright 2007, Gavin Carr.
256 This plugin is licensed under the same terms as blosxom itself i.e.
258 Permission is hereby granted, free of charge, to any person obtaining a
259 copy of this software and associated documentation files (the "Software"),
260 to deal in the Software without restriction, including without limitation
261 the rights to use, copy, modify, merge, publish, distribute, sublicense,
262 and/or sell copies of the Software, and to permit persons to whom the
263 Software is furnished to do so, subject to the following conditions:
265 The above copyright notice and this permission notice shall be included
266 in all copies or substantial portions of the Software.
268 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
269 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
270 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
271 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
272 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
273 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
274 OTHER DEALINGS IN THE SOFTWARE.