|
| 1 | +package Time::CTime; |
| 2 | + |
| 3 | + |
| 4 | +require 5.000; |
| 5 | + |
| 6 | +use Time::Timezone; |
| 7 | +use Time::CTime; |
| 8 | +require Exporter; |
| 9 | +@ISA = qw(Exporter); |
| 10 | +@EXPORT = qw(ctime asctime strftime); |
| 11 | +@EXPORT_OK = qw(asctime_n ctime_n @DoW @MoY @DayOfWeek @MonthOfYear); |
| 12 | + |
| 13 | +use strict; |
| 14 | + |
| 15 | +# constants |
| 16 | +use vars qw(@DoW @DayOfWeek @MoY @MonthOfYear %strftime_conversion $VERSION); |
| 17 | +use vars qw($template $sec $min $hour $mday $mon $year $wday $yday $isdst); |
| 18 | + |
| 19 | +$VERSION = 2011.0505; |
| 20 | + |
| 21 | +CONFIG: { |
| 22 | + @DoW = qw(Sun Mon Tue Wed Thu Fri Sat); |
| 23 | + @DayOfWeek = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday); |
| 24 | + @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); |
| 25 | + @MonthOfYear = qw(January February March April May June |
| 26 | + July August September October November December); |
| 27 | + |
| 28 | + %strftime_conversion = ( |
| 29 | + '%', sub { '%' }, |
| 30 | + 'a', sub { $DoW[$wday] }, |
| 31 | + 'A', sub { $DayOfWeek[$wday] }, |
| 32 | + 'b', sub { $MoY[$mon] }, |
| 33 | + 'B', sub { $MonthOfYear[$mon] }, |
| 34 | + 'c', sub { asctime_n($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst, "") }, |
| 35 | + 'd', sub { sprintf("%02d", $mday); }, |
| 36 | + 'D', sub { sprintf("%02d/%02d/%02d", $mon+1, $mday, $year%100) }, |
| 37 | + 'e', sub { sprintf("%2d", $mday); }, |
| 38 | + 'f', sub { fracprintf ("%3.3f", $sec); }, |
| 39 | + 'F', sub { fracprintf ("%6.6f", $sec); }, |
| 40 | + 'h', sub { $MoY[$mon] }, |
| 41 | + 'H', sub { sprintf("%02d", $hour) }, |
| 42 | + 'I', sub { sprintf("%02d", $hour % 12 || 12) }, |
| 43 | + 'j', sub { sprintf("%03d", $yday + 1) }, |
| 44 | + 'k', sub { sprintf("%2d", $hour); }, |
| 45 | + 'l', sub { sprintf("%2d", $hour % 12 || 12) }, |
| 46 | + 'm', sub { sprintf("%02d", $mon+1); }, |
| 47 | + 'M', sub { sprintf("%02d", $min) }, |
| 48 | + 'n', sub { "\n" }, |
| 49 | + 'o', sub { sprintf("%d%s", $mday, (($mday < 20 && $mday > 3) ? 'th' : ($mday%10 == 1 ? "st" : ($mday%10 == 2 ? "nd" : ($mday%10 == 3 ? "rd" : "th"))))) }, |
| 50 | + 'p', sub { $hour > 11 ? "PM" : "AM" }, |
| 51 | + 'r', sub { sprintf("%02d:%02d:%02d %s", $hour % 12 || 12, $min, $sec, $hour > 11 ? 'PM' : 'AM') }, |
| 52 | + 'R', sub { sprintf("%02d:%02d", $hour, $min) }, |
| 53 | + 'S', sub { sprintf("%02d", $sec) }, |
| 54 | + 't', sub { "\t" }, |
| 55 | + 'T', sub { sprintf("%02d:%02d:%02d", $hour, $min, $sec) }, |
| 56 | + 'U', sub { wkyr(0, $wday, $yday) }, |
| 57 | + 'v', sub { sprintf("%2d-%s-%4d", $mday, $MoY[$mon], $year+1900) }, |
| 58 | + 'w', sub { $wday }, |
| 59 | + 'W', sub { wkyr(1, $wday, $yday) }, |
| 60 | + 'y', sub { sprintf("%02d",$year%100) }, |
| 61 | + 'Y', sub { $year + 1900 }, |
| 62 | + 'x', sub { sprintf("%02d/%02d/%02d", $mon + 1, $mday, $year%100) }, |
| 63 | + 'X', sub { sprintf("%02d:%02d:%02d", $hour, $min, $sec) }, |
| 64 | + 'Z', sub { &tz2zone(undef,undef,$isdst) } |
| 65 | + # z sprintf("%+03d%02d", $offset / 3600, ($offset % 3600)/60); |
| 66 | + ); |
| 67 | + |
| 68 | + |
| 69 | +} |
| 70 | + |
| 71 | +sub fracprintf { |
| 72 | + my($t,$s) = @_; |
| 73 | + my($p) = sprintf($t, $s-int($s)); |
| 74 | + $p=~s/^0+//; |
| 75 | + $p; |
| 76 | +} |
| 77 | + |
| 78 | +sub asctime_n { |
| 79 | + my($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst, $TZname) = @_; |
| 80 | + ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst, $TZname) = localtime($sec) unless defined $min; |
| 81 | + $year += 1900; |
| 82 | + $TZname .= ' ' |
| 83 | + if $TZname; |
| 84 | + sprintf("%s %s %2d %2d:%02d:%02d %s%4d", |
| 85 | + $DoW[$wday], $MoY[$mon], $mday, $hour, $min, $sec, $TZname, $year); |
| 86 | +} |
| 87 | + |
| 88 | +sub asctime |
| 89 | +{ |
| 90 | + return asctime_n(@_)."\n"; |
| 91 | +} |
| 92 | + |
| 93 | +# is this formula right? |
| 94 | +sub wkyr { |
| 95 | + my($wstart, $wday, $yday) = @_; |
| 96 | + $wday = ($wday + 7 - $wstart) % 7; |
| 97 | + return int(($yday - $wday + 13) / 7 - 1); |
| 98 | +} |
| 99 | + |
| 100 | +# ctime($time) |
| 101 | + |
| 102 | +sub ctime { |
| 103 | + my($time) = @_; |
| 104 | + asctime(localtime($time), &tz2zone(undef,$time)); |
| 105 | +} |
| 106 | + |
| 107 | +sub ctime_n { |
| 108 | + my($time) = @_; |
| 109 | + asctime_n(localtime($time), &tz2zone(undef,$time)); |
| 110 | +} |
| 111 | + |
| 112 | +# strftime($template, @time_struct) |
| 113 | +# |
| 114 | +# Does not support locales |
| 115 | + |
| 116 | +sub strftime { |
| 117 | + local ($template, $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = @_; |
| 118 | + |
| 119 | + undef $@; |
| 120 | + $template =~ s/%([%aAbBcdDefFhHIjklmMnopQrRStTUvwWxXyYZ])/&{$Time::CTime::strftime_conversion{$1}}()/egs; |
| 121 | + die $@ if $@; |
| 122 | + return $template; |
| 123 | +} |
| 124 | + |
| 125 | +1; |
| 126 | + |
| 127 | +__END__ |
| 128 | +
|
| 129 | +=head1 NAME |
| 130 | +
|
| 131 | +Time::CTime -- format times ala POSIX asctime |
| 132 | +
|
| 133 | +=head1 SYNOPSIS |
| 134 | +
|
| 135 | + use Time::CTime |
| 136 | + print ctime(time); |
| 137 | + print asctime(localtime(time)); |
| 138 | + print strftime(template, localtime(time)); |
| 139 | +
|
| 140 | +=head2 strftime conversions |
| 141 | +
|
| 142 | + %% PERCENT |
| 143 | + %a day of the week abbr |
| 144 | + %A day of the week |
| 145 | + %b month abbr |
| 146 | + %B month |
| 147 | + %c ctime format: Sat Nov 19 21:05:57 1994 |
| 148 | + %d DD |
| 149 | + %D MM/DD/YY |
| 150 | + %e numeric day of the month |
| 151 | + %f floating point seconds (milliseconds): .314 |
| 152 | + %F floating point seconds (microseconds): .314159 |
| 153 | + %h month abbr |
| 154 | + %H hour, 24 hour clock, leading 0's) |
| 155 | + %I hour, 12 hour clock, leading 0's) |
| 156 | + %j day of the year |
| 157 | + %k hour |
| 158 | + %l hour, 12 hour clock |
| 159 | + %m month number, starting with 1, leading 0's |
| 160 | + %M minute, leading 0's |
| 161 | + %n NEWLINE |
| 162 | + %o ornate day of month -- "1st", "2nd", "25th", etc. |
| 163 | + %p AM or PM |
| 164 | + %r time format: 09:05:57 PM |
| 165 | + %R time format: 21:05 |
| 166 | + %S seconds, leading 0's |
| 167 | + %t TAB |
| 168 | + %T time format: 21:05:57 |
| 169 | + %U week number, Sunday as first day of week |
| 170 | + %v DD-Mon-Year |
| 171 | + %w day of the week, numerically, Sunday == 0 |
| 172 | + %W week number, Monday as first day of week |
| 173 | + %x date format: 11/19/94 |
| 174 | + %X time format: 21:05:57 |
| 175 | + %y year (2 digits) |
| 176 | + %Y year (4 digits) |
| 177 | + %Z timezone in ascii. eg: PST |
| 178 | +
|
| 179 | +=head1 DESCRIPTION |
| 180 | +
|
| 181 | +This module provides routines to format dates. They correspond |
| 182 | +to the libc routines. &strftime() supports a pretty good set of |
| 183 | +conversions -- more than most C libraries. |
| 184 | + |
| 185 | +strftime supports a pretty good set of conversions. |
| 186 | +
|
| 187 | +The POSIX module has very similar functionality. You should consider |
| 188 | +using it instead if you do not have allergic reactions to system |
| 189 | +libraries. |
| 190 | +
|
| 191 | +=head1 GENESIS |
| 192 | +
|
| 193 | +Written by David Muir Sharnoff <muir@idiom.org>. |
| 194 | +
|
| 195 | +The starting point for this package was a posting by |
| 196 | +Paul Foley <paul@ascent.com> |
| 197 | +
|
| 198 | +=head1 LICENSE |
| 199 | +
|
| 200 | +Copyright (C) 1996-2010 David Muir Sharnoff. |
| 201 | +Copyright (C) 2011 Google, Inc. |
| 202 | +License hereby |
| 203 | +granted for anyone to use, modify or redistribute this module at |
| 204 | +their own risk. Please feed useful changes back to cpan@dave.sharnoff.org. |
| 205 | +
|
0 commit comments