• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

lib/DateTime/Format/H03-May-2022-998418

t/H03-May-2022-772629

Build.PLH A D15-Mar-2021301 134

ChangesH A D15-Mar-20213.9 KiB11988

LICENSEH A D15-Mar-202120.1 KiB384309

MANIFESTH A D15-Mar-2021506 3535

META.jsonH A D15-Mar-20212.4 KiB8988

META.ymlH A D15-Mar-20211.5 KiB5251

MakefileH A D15-Mar-202123.2 KiB806490

README.mdH A D15-Mar-202112.6 KiB341231

cpanfileH A D15-Mar-2021207 97

minil.tomlH A D15-Mar-2021113 64

README.md

1# NAME
2
3DateTime::Format::Pg - Parse and format PostgreSQL dates and times
4
5# SYNOPSIS
6
7    use DateTime::Format::Pg;
8
9    my $dt = DateTime::Format::Pg->parse_datetime( '2003-01-16 23:12:01' );
10
11    # 2003-01-16 23:12:01
12    DateTime::Format::Pg->format_datetime($dt);
13
14# DESCRIPTION
15
16This module understands the formats used by PostgreSQL for its DATE, TIME,
17TIMESTAMP, and INTERVAL data types.  It can be used to parse these formats in
18order to create `DateTime` or `DateTime::Duration` objects, and it can take a
19`DateTime` or `DateTime::Duration` object and produce a string representing
20it in a format accepted by PostgreSQL.
21
22# CONSTRUCTORS
23
24The following methods can be used to create `DateTime::Format::Pg` objects.
25
26- new( name => value, ... )
27
28    Creates a new `DateTime::Format::Pg` instance. This is generally not
29    required for simple operations. If you wish to use a different parsing
30    style from the default then it is more comfortable to create an object.
31
32        my $parser = DateTime::Format::Pg->new()
33        my $copy = $parser->new( 'european' => 1 );
34
35    This method accepts the following options:
36
37    - european
38
39        If european is set to non-zero, dates are assumed to be in european
40        dd/mm/yyyy format. The default is to assume US mm/dd/yyyy format
41        (because this is the default for PostgreSQL).
42
43        This option only has an effect if PostgreSQL is set to output dates in
44        the 'PostgreSQL' (DATE only) and 'SQL' (DATE and TIMESTAMP) styles.
45
46        Note that you don't have to set this option if the PostgreSQL server has
47        been set to use the 'ISO' format, which is the default.
48
49    - server\_tz
50
51        This option can be set to a `DateTime::TimeZone` object or a string
52        that contains a time zone name.
53
54        This value must be set to the same value as the PostgreSQL server's time
55        zone in order to parse TIMESTAMP WITH TIMEZONE values in the
56        'PostgreSQL', 'SQL', and 'German' formats correctly.
57
58        Note that you don't have to set this option if the PostgreSQL server has
59        been set to use the 'ISO' format, which is the default.
60
61- clone()
62
63    This method is provided for those who prefer to explicitly clone via a
64    method called `clone()`.
65
66        my $clone = $original->clone();
67
68    If called as a class method it will die.
69
70# METHODS
71
72This class provides the following methods. The parse\_datetime, parse\_duration,
73format\_datetime, and format\_duration methods are general-purpose methods
74provided for compatibility with other `DateTime::Format` modules.
75
76The other methods are specific to the corresponding PostgreSQL date/time data
77types. The names of these methods are derived from the name of the PostgreSQL
78data type.  (Note: Prior to PostgreSQL 7.3, the TIMESTAMP type was equivalent
79to the TIMESTAMP WITH TIME ZONE type. This data type corresponds to the
80format/parse\_timestamp\_with\_time\_zone method but not to the
81format/parse\_timestamp method.)
82
83## PARSING METHODS
84
85This class provides the following parsing methods.
86
87As a general rule, the parsing methods accept input in any format that the
88PostgreSQL server can produce. However, if PostgreSQL's DateStyle is set to
89'SQL' or 'PostgreSQL', dates can only be parsed correctly if the 'european'
90option is set correctly (i.e. same as the PostgreSQL server).  The same is true
91for time zones and the 'australian\_timezones' option in all modes but 'ISO'.
92
93The default DateStyle, 'ISO', will always produce unambiguous results
94and is also parsed most efficiently by this parser class. I strongly
95recommend using this setting unless you have a good reason not to.
96
97- parse\_datetime($string,...)
98
99    Given a string containing a date and/or time representation, this method
100    will return a new `DateTime` object.
101
102    If the input string does not contain a date, it is set to 1970-01-01.
103    If the input string does not contain a time, it is set to 00:00:00.
104    If the input string does not contain a time zone, it is set to the
105    floating time zone.
106
107    If given an improperly formatted string, this method may die.
108
109- parse\_timestamptz($string,...)
110- parse\_timestamp\_with\_time\_zone($string,...)
111
112    Given a string containing a timestamp (date and time) representation,
113    this method will return a new `DateTime` object. This method is
114    suitable for the TIMESTAMPTZ (or TIMESTAMP WITH TIME ZONE) type.
115
116    If the input string does not contain a time zone, it is set to the
117    floating time zone.
118
119    Please note that PostgreSQL does not actually store a time zone along
120    with the TIMESTAMP WITH TIME ZONE (or TIMESTAMPTZ) type but will just
121    return a time stamp converted for the server's local time zone.
122
123    If given an improperly formatted string, this method may die.
124
125- parse\_timestamp($string,...)
126- parse\_timestamp\_without\_time\_zone($string,...)
127
128    Similar to the functions above, but always returns a `DateTime` object
129    with a floating time zone. This method is suitable for the TIMESTAMP (or
130    TIMESTAMP WITHOUT TIME ZONE) type.
131
132    If the server does return a time zone, it is ignored.
133
134    If given an improperly formatted string, this method may die.
135
136- parse\_timetz($string,...)
137- parse\_time\_with\_time\_zone($string,...)
138
139    Given a string containing a time representation, this method will return
140    a new `DateTime` object. The date is set to 1970-01-01. This method is
141    suitable for the TIMETZ (or TIME WITH TIME ZONE) type.
142
143    If the input string does not contain a time zone, it is set to the
144    floating time zone.
145
146    Please note that PostgreSQL stores a numerical offset with its TIME WITH
147    TIME ZONE (or TIMETZ) type. It does not store a time zone name (such as
148    'Europe/Rome').
149
150    If given an improperly formatted string, this method may die.
151
152- parse\_time($string,...)
153- parse\_time\_without\_time\_zone($string,...)
154
155    Similar to the functions above, but always returns an `DateTime` object
156    with a floating time zone. If the server returns a time zone, it is
157    ignored. This method is suitable for use with the TIME (or TIME WITHOUT
158    TIME ZONE) type.
159
160    This ensures that the resulting `DateTime` object will always have the
161    time zone expected by your application.
162
163    If given an improperly formatted string, this method may die.
164
165- parse\_date($string,...)
166
167    Given a string containing a date representation, this method will return
168    a new `DateTime` object. The time is set to 00:00:00 (floating time
169    zone). This method is suitable for the DATE type.
170
171    If given an improperly formatted string, this method may die.
172
173- parse\_duration($string)
174- parse\_interval($string)
175
176    Given a string containing a duration (SQL type INTERVAL) representation,
177    this method will return a new `DateTime::Duration` object.
178
179    If given an improperly formatted string, this method may die.
180
181## FORMATTING METHODS
182
183This class provides the following formatting methods.
184
185The output is always in the format mandated by the SQL standard (derived
186from ISO 8601), which is parsed by PostgreSQL unambiguously in all
187DateStyle modes.
188
189- format\_datetime($datetime,...)
190
191    Given a `DateTime` object, this method returns a string appropriate as
192    input for all date and date/time types of PostgreSQL. It will contain
193    date and time.
194
195    If the time zone of the `DateTime` part is floating, the resulting
196    string will contain no time zone, which will result in the server's time
197    zone being used. Otherwise, the numerical offset of the time zone is
198    used.
199
200- format\_time($datetime,...)
201- format\_time\_without\_time\_zone($datetime,...)
202
203    Given a `DateTime` object, this method returns a string appropriate as
204    input for the TIME type (also known as TIME WITHOUT TIME ZONE), which
205    will contain the local time of the `DateTime` object and no time zone.
206
207- format\_timetz($datetime)
208- format\_time\_with\_time\_zone($datetime)
209
210    Given a `DateTime` object, this method returns a string appropriate as
211    input for the TIME WITH TIME ZONE type (also known as TIMETZ), which
212    will contain the local part of the `DateTime` object and a numerical
213    time zone.
214
215    You should not use the TIME WITH TIME ZONE type to store dates with
216    floating time zones.  If the time zone of the `DateTime` part is
217    floating, the resulting string will contain no time zone, which will
218    result in the server's time zone being used.
219
220- format\_date($datetime)
221
222    Given a `DateTime` object, this method returns a string appropriate as
223    input for the DATE type, which will contain the date part of the
224    `DateTime` object.
225
226- format\_timestamp($datetime)
227- format\_timestamp\_without\_time\_zone($datetime)
228
229    Given a `DateTime` object, this method returns a string appropriate as
230    input for the TIMESTAMP type (also known as TIMESTAMP WITHOUT TIME
231    ZONE), which will contain the local time of the `DateTime` object and
232    no time zone.
233
234- format\_timestamptz($datetime)
235- format\_timestamp\_with\_time\_zone($datetime)
236
237    Given a `DateTime` object, this method returns a string appropriate as
238    input for the TIMESTAMP WITH TIME ZONE type, which will contain the
239    local part of the `DateTime` object and a numerical time zone.
240
241    You should not use the TIMESTAMP WITH TIME ZONE type to store dates with
242    floating time zones.  If the time zone of the `DateTime` part is
243    floating, the resulting string will contain no time zone, which will
244    result in the server's time zone being used.
245
246- format\_duration($du)
247- format\_interval($du)
248
249    Given a `DateTime::Duration` object, this method returns a string appropriate
250    as input for the INTERVAL type.
251
252# LIMITATIONS
253
254Some output formats of PostgreSQL have limitations that can only be passed on
255by this class.
256
257As a general rules, none of these limitations apply to the 'ISO' output
258format.  It is strongly recommended to use this format (and to use
259PostgreSQL's to\_char function when another output format that's not
260supposed to be handled by a parser of this class is desired). 'ISO' is
261the default but you are advised to explicitly set it at the beginning of
262the session by issuing a SET DATESTYLE TO 'ISO'; command in case the
263server administrator changes that setting.
264
265When formatting DateTime objects, this class always uses a format that's
266handled unambiguously by PostgreSQL.
267
268## TIME ZONES
269
270If DateStyle is set to 'PostgreSQL', 'SQL', or 'German', PostgreSQL does
271not send numerical time zones for the TIMESTAMPTZ (or TIMESTAMP WITH
272TIME ZONE) type. Unfortunately, the time zone names used instead can be
273ambiguous: For example, 'EST' can mean -0500, +1000, or +1100.
274
275You must set the 'server\_tz' variable to a time zone that is identical to that
276of the PostgreSQL server. If the server is set to a different time zone (or the
277underlying operating system interprets the time zone differently), the parser
278will return wrong times.
279
280You can avoid such problems by setting the server's time zone to UTC
281using the SET TIME ZONE 'UTC' command and setting 'server\_tz' parameter
282to 'UTC' (or by using the ISO output format, of course).
283
284## EUROPEAN DATES
285
286For the SQL (for DATE and TIMSTAMP\[TZ\]) and the PostgreSQL (for DATE)
287output format, the server can send dates in both European-style
288'dd/mm/yyyy' and in US-style 'mm/dd/yyyy' format. In order to parse
289these dates correctly, you have to pass the 'european' option to the
290constructor or to the `parse_xxx` routines.
291
292This problem does not occur when using the ISO or German output format
293(and for PostgreSQL with TIMESTAMP\[TZ\] as month names are used then).
294
295## INTERVAL ELEMENTS
296
297`DateTime::Duration` stores months, days, minutes and seconds
298separately. PostgreSQL only stores months and seconds and disregards the
299irregular length of days due to DST switching and the irregular length
300of minutes due to leap seconds. Therefore, it is not possible to store
301`DateTime::Duration` objects as SQL INTERVALs without the loss of some
302information.
303
304## NEGATIVE INTERVALS
305
306In the SQL and German output formats, the server does not send an
307indication of the sign with intervals. This means that '1 month ago' and
308'1 month' are both returned as '1 mon'.
309
310This problem can only be avoided by using the 'ISO' or 'PostgreSQL'
311output format.
312
313# SUPPORT
314
315Support for this module is provided via the datetime@perl.org email
316list.  See http://lists.perl.org/ for more details.
317
318# AUTHOR
319
320Daisuke Maki <daisuke@endeworks.jp>
321
322# AUTHOR EMERITUS
323
324Claus A. Faerber <perl@faerber.muc.de>
325
326# COPYRIGHT
327
328Copyright (c) 2003 Claus A. Faerber. Copyright (c) 2005-2007 Daisuke Maki
329
330This program is free software; you can redistribute it and/or modify it under
331the same terms as Perl itself.
332
333The full text of the license can be found in the LICENSE file included with
334this module.
335
336# SEE ALSO
337
338datetime@perl.org mailing list
339
340http://datetime.perl.org/
341