1DateFormat(3) User Contributed Perl Documentation DateFormat(3)
2
3
4
6 Log::Log4perl::DateFormat - Log4perl advanced date formatter helper
7 class
8
10 use Log::Log4perl::DateFormat;
11
12 my $format = Log::Log4perl::DateFormat->new("HH:mm:ss,SSS");
13
14 # Simple time, resolution in seconds
15 my $time = time();
16 print $format->format($time), "\n";
17 # => "17:02:39,000"
18
19 # Advanced time, resultion in milliseconds
20 use Time::HiRes;
21 my ($secs, $msecs) = Time::HiRes::gettimeofday();
22 print $format->format($secs, $msecs), "\n";
23 # => "17:02:39,959"
24
26 "Log::Log4perl::DateFormat" is a low-level helper class for the
27 advanced date formatting functions in
28 "Log::Log4perl::Layout::PatternLayout".
29
30 Unless you're writing your own Layout class like
31 Log::Log4perl::Layout::PatternLayout, there's probably not much use for
32 you to read this.
33
34 "Log::Log4perl::DateFormat" is a formatter which allows dates to be
35 formatted according to the log4j spec on
36
37 http://java.sun.com/j2se/1.5.0/docs/api/java/text/SimpleDateFormat.html
38
39 which allows the following placeholders to be recognized and processed:
40
41 Symbol Meaning Presentation Example
42 ------ ------- ------------ -------
43 G era designator (Text) AD
44 y year (Number) 1996
45 M month in year (Text & Number) July & 07
46 d day in month (Number) 10
47 h hour in am/pm (1~12) (Number) 12
48 H hour in day (0~23) (Number) 0
49 m minute in hour (Number) 30
50 s second in minute (Number) 55
51 S millisecond (Number) 978
52 E day in week (Text) Tuesday
53 D day in year (Number) 189
54 F day of week in month (Number) 2 (2nd Wed in July)
55 w week in year (Number) 27
56 W week in month (Number) 2
57 a am/pm marker (Text) PM
58 k hour in day (1~24) (Number) 24
59 K hour in am/pm (0~11) (Number) 0
60 z time zone (Text) Pacific Standard Time
61 Z RFC 822 time zone (Text) -0800
62 ' escape for text (Delimiter)
63 '' single quote (Literal) '
64
65 For example, if you want to format the current Unix time in "MM/dd
66 HH:mm" format, all you have to do is this:
67
68 use Log::Log4perl::DateFormat;
69
70 my $format = Log::Log4perl::DateFormat->new("MM/dd HH:mm");
71
72 my $time = time();
73 print $format->format($time), "\n";
74
75 While the "new()" method is expensive, because it parses the format
76 strings and sets up all kinds of structures behind the scenes, followup
77 calls to "format()" are fast, because "DateFormat" will just call
78 "localtime()" and "sprintf()" once to return the formatted date/time
79 string.
80
81 So, typically, you would initialize the formatter once and then reuse
82 it over and over again to display all kinds of time values.
83
84 Also, for your convenience, the following predefined formats are
85 available, just as outlined in the log4j spec:
86
87 Format Equivalent Example
88 ABSOLUTE "HH:mm:ss,SSS" "15:49:37,459"
89 DATE "dd MMM yyyy HH:mm:ss,SSS" "06 Nov 1994 15:49:37,459"
90 ISO8601 "yyyy-MM-dd HH:mm:ss,SSS" "1999-11-27 15:49:37,459"
91 APACHE "[EEE MMM dd HH:mm:ss yyyy]" "[Wed Mar 16 15:49:37 2005]"
92
93 So, instead of passing
94
95 Log::Log4perl::DateFormat->new("HH:mm:ss,SSS");
96
97 you could just as well say
98
99 Log::Log4perl::DateFormat->new("ABSOLUTE");
100
101 and get the same result later on.
102
103 Known Shortcomings
104 The following placeholders are currently not recognized, unless someone
105 (and that could be you :) implements them:
106
107 F day of week in month
108 w week in year
109 W week in month
110 k hour in day
111 K hour in am/pm
112 z timezone (but we got 'Z' for the numeric time zone value)
113
114 Also, "Log::Log4perl::DateFormat" just knows about English week and
115 month names, internationalization support has to be added.
116
118 Copyright 2002-2009 by Mike Schilli <m@perlmeister.com> and Kevin Goess
119 <cpan@goess.org>.
120
121 This library is free software; you can redistribute it and/or modify it
122 under the same terms as Perl itself.
123
124
125
126perl v5.12.2 2010-08-31 DateFormat(3)