CTime.pm 5.6 KB

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