DateTi...Oracle(3pm)
β¨ DateTime::Format::Oracle β¨
okay bestie, gather round π this is the DateTime::Format::Oracle page from the User Contributed Perl Documentation corner of the manual. it's a Perl module that speaks Oracle's date dialect, and honestly? it's giving translator-girlboss energy. let's get into it π¦
πΈ NAME πΈ
DateTime::Format::Oracle β parse and format Oracle dates and timestamps π
β¨ SYNOPSIS β¨
use DateTime::Format::Oracle;
$ENV{'NLS_DATE_FORMAT'} = 'YYYY-MM-DD HH24:MI:SS';
my $dt = DateTime::Format::Oracle->parse_datetime('2003-01-16 23:12:01');
my $string = DateTime::Format::Oracle->format_datetime($dt);
like, that's it. that's the whole vibe. π
π DESCRIPTION π
so this module is your little bridge between the Oracle world and the DateTime world. it takes Oracle date and timestamp values and turns them into DateTime objects, and it can also go the other way β hand it a DateTime object and it'll hand you back a date string matching the NLS_DATE_FORMAT. π
Oracle is flexible with date formatting, thanks to its NLS_DATE_FORMAT session variable. dates coming out of Oracle show up according to whatever that variable is currently set to. and dates going into Oracle have to match the current NLS_DATE_FORMAT setting too. no cap. π
timestamps play by either NLS_TIMESTAMP_FORMAT or NLS_TIMESTAMP_TZ_FORMAT β those are the session variables in charge there.
and here's the tea β: this module keeps track of those Oracle session variable values by peeking at environment variables of the same name. so every single time one of Oracle's formatting session variables gets updated, you must update the %ENV hash too. don't forget, bestie, or the whole thing falls apart π
π METHODS π
this class is serving the following methods π
nls_date_formatβ this one figures out the current value of Oracle'sNLS_DATE_FORMAT. right now it just reads from
$ENV{'NLS_DATE_FORMAT'}
or, if that's not set, from the package variable $nls_date_format, which defaults to "YYYY-MM-DD HH24:MI:SS". honestly a great default to have β but it is not Oracle's default. dates will fail to parse if Oracle's NLS_DATE_FORMAT and the value from this method aren't the same. so like, keep them matching fr fr π
if you want to use this module's default, you can do something like this right after you connect to Oracle:
$dbh->do(
"alter session set nls_date_format = '" .
DateTime::Format::Oracle->nls_date_format .
"'"
);
nls_timestamp_formatβ this one figures out the current value of Oracle'sNLS_TIMESTAMP_FORMAT. right now it just reads from
$ENV{'NLS_TIMESTAMP_FORMAT'}
or, if that's not set, from the package variable $nls_timestamp_format, which defaults to "YYYY-MM-DD HH24:MI:SS". again, lovely default, not Oracle's default. dates will fail to parse if Oracle's NLS_TIMESTAMP_FORMAT and the value from this method don't match π
if you want to use this module's default, you can do something like this right after you connect to Oracle:
$dbh->do(
"alter session set nls_timestamp_format = '" .
DateTime::Format::Oracle->nls_timestamp_format .
"'"
);
nls_timestamp_tz_formatβ this one figures out the current value of Oracle'sNLS_TIMESTAMP_TZ_FORMAT. right now it just reads from
$ENV{'NLS_TIMESTAMP_TZ_FORMAT'}
or, if that's not set, from the package variable $nls_timestamp_tz_format, which defaults to "YYYY-MM-DD HH24:MI:SS TZHTZM". yes bestie, this one's got the time zone on the end πβ¨ still not Oracle's default though. dates will fail to parse if Oracle's NLS_TIMESTAMP_TZ_FORMAT and the value from this method are not the same.
if you want to use this module's default, you can do something like this right after you connect to Oracle:
$dbh->do(
"alter session set nls_timestamp_tz_format = '" .
DateTime::Format::Oracle->nls_timestamp_tz_format .
"'"
);
parse_datetimeβ give it a string holding a date and/or time representation matchingNLS_DATE_FORMAT, and it hands back a shiny newDateTimeobject. π
and if the string is improperly formatted? it may die. π
parse_dateβ alias toparse_datetime. because Oracle's date datatype also holds time information, iykyk πparse_timestampβ give it a string holding a date and/or time representation matchingNLS_TIMESTAMP_FORMAT, and you get back a newDateTimeobject.
improperly formatted string? it may die. π
parse_timestamptzβ give it a string holding a date and/or time representation matchingNLS_TIMESTAMP_TZ_FORMAT, and boom, newDateTimeobject.
improperly formatted string? you already know. it may die. π
parse_timestamp_with_time_zoneβ (yes bestie, this is the other name sitting right next toparse_timestamptzin the original) β give it a string holding a date and/or time representation matchingNLS_TIMESTAMP_TZ_FORMAT, and it returns a newDateTimeobject.
improperly formatted string? may die. π
current_date_parserβ the currentDateTime::Format::Builder-generated parsing method thatparse_datetimeandparse_dateuse. π§current_timestamp_parserβ the currentDateTime::Format::Builder-generated parsing method thatparse_timestampuses.current_timestamptz_parserβ the currentDateTime::Format::Builder-generated parsing method thatparse_timestamptzuses.format_datetimeβ hand it aDateTimeobject and it returns a string matching the current value ofNLS_DATE_FORMAT. β¨
super important: keep $ENV{'NLS_DATE_FORMAT'} the same as the Oracle session variable NLS_DATE_FORMAT. they have to match, periodt.
to determine the current value of Oracle's NLS_DATE_FORMAT:
select NLS_DATE_FORMAT from NLS_SESSION_PARAMETERS
to reset Oracle's NLS_DATE_FORMAT:
alter session set NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'
and generally? it's a good idea to set NLS_DATE_FORMAT to an unambiguous value β four-digit year, plus hour, minute and second. no ambiguity, no drama π
format_dateβ alias toformat_datetime. π«format_timestampβ hand it aDateTimeobject and it returns a string matching the current value ofNLS_TIMESTAMP_FORMAT.
keep it matched: $ENV{'NLS_TIMESTAMP_FORMAT'} has to equal the Oracle session variable NLS_TIMESTAMP_FORMAT.
to determine the current value of Oracle's NLS_TIMESTAMP_FORMAT:
select NLS_TIMESTAMP_FORMAT from NLS_SESSION_PARAMETERS
to reset Oracle's NLS_TIMESTAMP_FORMAT:
alter session set NLS_TIMESTAMP_FORMAT='YYYY-MM-DD HH24:MI:SS'
and again, set it to an unambiguous value with a four-digit year and hour, minute and second. you'll thank yourself later πΈ
format_timestamptzβ hand it aDateTimeobject and it returns a string matching the current value ofNLS_TIMESTAMP_TZ_FORMAT.
keep it matched: $ENV{'NLS_TIMESTAMP_TZ_FORMAT'} has to equal the Oracle session variable NLS_TIMESTAMP_TZ_FORMAT.
to determine the current value of Oracle's NLS_TIMESTAMP_TZ_FORMAT:
select NLS_TIMESTAMP_TZ_FORMAT from NLS_SESSION_PARAMETERS
to reset Oracle's NLS_TIMESTAMP_TZ_FORMAT:
alter session set NLS_TIMESTAMP_TZ_FORMAT='YYYY-MM-DD HH24:MI:SS TZHTZM'
and yes, once more with feeling: unambiguous value, four-digit year, hour, minute, second. π
format_timestamp_with_time_zoneβ (the other name riding shotgun withformat_timestamptzin the original) β hand it aDateTimeobject and it returns a string matching the current value ofNLS_TIMESTAMP_TZ_FORMAT.
keep it matched: $ENV{'NLS_TIMESTAMP_TZ_FORMAT'} must equal the Oracle session variable NLS_TIMESTAMP_TZ_FORMAT.
to determine the current value of Oracle's NLS_TIMESTAMP_TZ_FORMAT:
select NLS_TIMESTAMP_TZ_FORMAT from NLS_SESSION_PARAMETERS
to reset Oracle's NLS_TIMESTAMP_TZ_FORMAT:
alter session set NLS_TIMESTAMP_TZ_FORMAT='YYYY-MM-DD HH24:MI:SS TZHTZM'
unambiguous value, four-digit year, hour, minute, second. that's the whole sermon π
current_date_formatβ the current generated method thatformat_datetime,format_date, andcurrent_date_parseruse to keep track of thestrptimetranslation ofNLS_DATE_FORMAT.current_timestamp_formatβ the current generated method thatformat_timestamp,format_timestamp_with_time_zone, andcurrent_timestamp_parseruse to keep track of thestrptimetranslation ofNLS_TIMESTAMP_FORMAT.current_timestamptz_formatβ the current generated method thatformat_timestamptz,format_timestamp_with_time_zone, andcurrent_timestamp_parseruse to keep track of thestrptimetranslation ofNLS_TIMESTAMP_FORMAT.oracle_to_posixβ hand it anNLS_DATE_FORMAT,NLS_TIMESTAMP_FORMAT, orNLS_TIMESTAMP_TZ_FORMATvalue and it returns aDateTime-compatiblestrptimeformat value. π
the translation is currently handled by Convert::NLS_DATE_FORMAT.
π LIMITATIONS π
Oracle is way more chill about the case of names β months and stuff β whereas DateTime generally returns names in ucfirst format. so like:
MONTH -> FEBRUARY
Month -> February
month -> february
all three of those translate to:
%B -> February
one format to rule them all, bestie π
π TIME ZONES π
Oracle returns all dates and timestamps in a time zone similar to the DateTime floating time zone β except for 'timestamp with time zone' columns. those are the special ones π
π§ INTERVAL ELEMENTS π§
ngl, the author has not implemented parse_duration, format_duration, parse_interval, nor format_interval β and has no plans to do so. π
but if you need these features? unit tests, method implementations, and pointers to documentation are all welcome. come through, bestie π«Ά
π«Ά SUPPORT π«Ά
support for this module is provided via the datetime@perl.org email list. see http://lists.perl.org/ for more details. βοΈ
π TODO π
- possibly read an environment variable to determine a time zone to use instead of 'floating'. π
- test and document creating an instance via
new. π§
π AUTHOR π
Nathan Gray, <kolibrie@cpan.org> π
π ACKNOWLEDGEMENTS π
ngl, the author might have put this module off for another couple years without the lure of Jifty, Catalyst, and DBIx::Class pulling at them. π
thanks to Dan Horne for his RFC draft of this module. π«Ά
β COPYRIGHT & LICENSE β
Copyright (C) 2006, 2008, 2011 Nathan Gray.
this library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.4 or, at your option, any later version of Perl 5 you may have available. π
π₯ SEE ALSO π₯
Convert::NLS_DATE_FORMAT- the datetime@perl.org mailing list βοΈ
- http://datetime.perl.org/ π
and that's the whole page, bestie π this one came from perl v5.32.0 and was dated 2021-01-04 β the DateTi...Oracle(3pm) of it all. go forth and parse some dates, you absolute icon πβ¨
π¦ where this page comes from
- debian bookworm: libdatetime-format-oracle-perl 0.06-1.1
- debian trixie: libdatetime-format-oracle-perl 0.06-1.1
- ubuntu jammy: libdatetime-format-oracle-perl 0.06-1.1
- ubuntu noble: libdatetime-format-oracle-perl 0.06-1.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms