1Time::Piece(3pm) Perl Programmers Reference Guide Time::Piece(3pm)
2
3
4
6 Time::Piece - Object Oriented time objects
7
9 use Time::Piece;
10
11 my $t = localtime;
12 print "Time is $t\n";
13 print "Year is ", $t->year, "\n";
14
16 This module replaces the standard "localtime" and "gmtime" functions
17 with implementations that return objects. It does so in a backwards
18 compatible manner, so that using localtime/gmtime in the way documented
19 in perlfunc will still return what you expect.
20
21 The module actually implements most of an interface described by Larry
22 Wall on the perl5-porters mailing list here:
23 http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2000-01/msg00241.html
24
26 After importing this module, when you use localtime or gmtime in a
27 scalar context, rather than getting an ordinary scalar string
28 representing the date and time, you get a Time::Piece object, whose
29 stringification happens to produce the same effect as the localtime and
30 gmtime functions. There is also a new() constructor provided, which is
31 the same as localtime(), except when passed a Time::Piece object, in
32 which case it's a copy constructor. The following methods are available
33 on the object:
34
35 $t->sec # also available as $t->second
36 $t->min # also available as $t->minute
37 $t->hour # 24 hour
38 $t->mday # also available as $t->day_of_month
39 $t->mon # 1 = January
40 $t->_mon # 0 = January
41 $t->monname # Feb
42 $t->month # same as $t->monname
43 $t->fullmonth # February
44 $t->year # based at 0 (year 0 AD is, of course 1 BC)
45 $t->_year # year minus 1900
46 $t->yy # 2 digit year
47 $t->wday # 1 = Sunday
48 $t->_wday # 0 = Sunday
49 $t->day_of_week # 0 = Sunday
50 $t->wdayname # Tue
51 $t->day # same as wdayname
52 $t->fullday # Tuesday
53 $t->yday # also available as $t->day_of_year, 0 = Jan 01
54 $t->isdst # also available as $t->daylight_savings
55
56 $t->hms # 12:34:56
57 $t->hms(".") # 12.34.56
58 $t->time # same as $t->hms
59
60 $t->ymd # 2000-02-29
61 $t->date # same as $t->ymd
62 $t->mdy # 02-29-2000
63 $t->mdy("/") # 02/29/2000
64 $t->dmy # 29-02-2000
65 $t->dmy(".") # 29.02.2000
66 $t->datetime # 2000-02-29T12:34:56 (ISO 8601)
67 $t->cdate # Tue Feb 29 12:34:56 2000
68 "$t" # same as $t->cdate
69
70 $t->epoch # seconds since the epoch
71 $t->tzoffset # timezone offset in a Time::Seconds object
72
73 $t->julian_day # number of days since Julian period began
74 $t->mjd # modified Julian date (JD-2400000.5 days)
75
76 $t->week # week number (ISO 8601)
77
78 $t->is_leap_year # true if it's a leap year
79 $t->month_last_day # 28-31
80
81 $t->time_separator($s) # set the default separator (default ":")
82 $t->date_separator($s) # set the default separator (default "-")
83 $t->day_list(@days) # set the default weekdays
84 $t->mon_list(@days) # set the default months
85
86 $t->strftime(FORMAT) # same as POSIX::strftime (without the overhead
87 # of the full POSIX extension)
88 $t->strftime() # "Tue, 29 Feb 2000 12:34:56 GMT"
89
90 Time::Piece->strptime(STRING, FORMAT)
91 # see strptime man page. Creates a new
92 # Time::Piece object
93
94 Note that "localtime" and "gmtime" are not listed above. If called as
95 methods on a Time::Piece object, they act as constructors, returning a
96 new Time::Piece object for the current time. In other words: they're
97 not useful as methods.
98
99 Local Locales
100 Both wdayname (day) and monname (month) allow passing in a list to use
101 to index the name of the days against. This can be useful if you need
102 to implement some form of localisation without actually installing or
103 using locales.
104
105 my @days = qw( Dimanche Lundi Merdi Mercredi Jeudi Vendredi Samedi );
106
107 my $french_day = localtime->day(@days);
108
109 These settings can be overridden globally too:
110
111 Time::Piece::day_list(@days);
112
113 Or for months:
114
115 Time::Piece::mon_list(@months);
116
117 And locally for months:
118
119 print localtime->month(@months);
120
121 Date Calculations
122 It's possible to use simple addition and subtraction of objects:
123
124 use Time::Seconds;
125
126 my $seconds = $t1 - $t2;
127 $t1 += ONE_DAY; # add 1 day (constant from Time::Seconds)
128
129 The following are valid ($t1 and $t2 are Time::Piece objects):
130
131 $t1 - $t2; # returns Time::Seconds object
132 $t1 - 42; # returns Time::Piece object
133 $t1 + 533; # returns Time::Piece object
134
135 However adding a Time::Piece object to another Time::Piece object will
136 cause a runtime error.
137
138 Note that the first of the above returns a Time::Seconds object, so
139 while examining the object will print the number of seconds (because of
140 the overloading), you can also get the number of minutes, hours, days,
141 weeks and years in that delta, using the Time::Seconds API.
142
143 In addition to adding seconds, there are two APIs for adding months and
144 years:
145
146 $t->add_months(6);
147 $t->add_years(5);
148
149 The months and years can be negative for subtractions. Note that there
150 is some "strange" behaviour when adding and subtracting months at the
151 ends of months. Generally when the resulting month is shorter than the
152 starting month then the number of overlap days is added. For example
153 subtracting a month from 2008-03-31 will not result in 2008-02-31 as
154 this is an impossible date. Instead you will get 2008-03-02. This
155 appears to be consistent with other date manipulation tools.
156
157 Date Comparisons
158 Date comparisons are also possible, using the full suite of "<", ">",
159 "<=", ">=", "<=>", "==" and "!=".
160
161 Date Parsing
162 Time::Piece has a built-in strptime() function (from FreeBSD), allowing
163 you incredibly flexible date parsing routines. For example:
164
165 my $t = Time::Piece->strptime("Sunday 3rd Nov, 1943",
166 "%A %drd %b, %Y");
167
168 print $t->strftime("%a, %d %b %Y");
169
170 Outputs:
171
172 Wed, 03 Nov 1943
173
174 (see, it's even smart enough to fix my obvious date bug)
175
176 For more information see "man strptime", which should be on all unix
177 systems.
178
179 Alternatively look here:
180 http://www.unix.com/man-page/FreeBSD/3/strftime/
181
182 YYYY-MM-DDThh:mm:ss
183 The ISO 8601 standard defines the date format to be YYYY-MM-DD, and the
184 time format to be hh:mm:ss (24 hour clock), and if combined, they
185 should be concatenated with date first and with a capital 'T' in front
186 of the time.
187
188 Week Number
189 The week number may be an unknown concept to some readers. The ISO
190 8601 standard defines that weeks begin on a Monday and week 1 of the
191 year is the week that includes both January 4th and the first Thursday
192 of the year. In other words, if the first Monday of January is the
193 2nd, 3rd, or 4th, the preceding days of the January are part of the
194 last week of the preceding year. Week numbers range from 1 to 53.
195
196 Global Overriding
197 Finally, it's possible to override localtime and gmtime everywhere, by
198 including the ':override' tag in the import list:
199
200 use Time::Piece ':override';
201
203 Setting $ENV{TZ} in Threads on Win32
204 Note that when using perl in the default build configuration on Win32
205 (specifically, when perl is built with PERL_IMPLICIT_SYS), each perl
206 interpreter maintains its own copy of the environment and only the main
207 interpreter will update the process environment seen by strftime.
208
209 Therefore, if you make changes to $ENV{TZ} from inside a thread other
210 than the main thread then those changes will not be seen by strftime if
211 you subsequently call that with the %Z formatting code. You must change
212 $ENV{TZ} in the main thread to have the desired effect in this case
213 (and you must also call _tzset() in the main thread to register the
214 environment change).
215
216 Furthermore, remember that this caveat also applies to fork(), which is
217 emulated by threads on Win32.
218
219 Use of epoch seconds
220 This module internally uses the epoch seconds system that is provided
221 via the perl "time()" function and supported by "gmtime()" and
222 "localtime()".
223
224 If your perl does not support times larger than "2^31" seconds then
225 this module is likely to fail at processing dates beyond the year 2038.
226 There are moves afoot to fix that in perl. Alternatively use 64 bit
227 perl. Or if none of those are options, use the DateTime module which
228 has support for years well into the future and past.
229
231 Matt Sergeant, matt@sergeant.org Jarkko Hietaniemi, jhi@iki.fi (while
232 creating Time::Piece for core perl)
233
235 Copyright 2001, Larry Wall.
236
237 This module is free software, you may distribute it under the same
238 terms as Perl.
239
241 The excellent Calendar FAQ at
242 http://www.tondering.dk/claus/calendar.html
243
245 The test harness leaves much to be desired. Patches welcome.
246
247
248
249perl v5.26.3 2018-03-01 Time::Piece(3pm)