develooper Front page | perl.datetime | Postings from January 2003

Discarding some fifteen functions/methods in favour of an enhanced strftime

Thread Next
Antonios Christofides
January 12, 2003 11:13
Discarding some fifteen functions/methods in favour of an enhanced strftime
Message ID:
Before going into the subject, I have one request to make: is it
possible, please, to modularise the discussion on DateTime? There are
two huge threads discussing all sorts of things. I have great difficulty
following the discussion. Let's try to discuss one thing in each thread,
and give it a clear subject line.

I'm proposing the following, which is an idea borrowed from Oracle's
date/time API and extended. Get rid of the methods/functions returning
year, month, day of week, day of month, day of year, hour, minute,
second, week of year, week of month, whatever, and use an enhanced
strftime instead, as in the following examples:

    ($year, $month, $day) = datetime_object -> strftime('%Y', '%m', '%d');

    $iso8601 = datetime_object -> strftime('%Y-%m-%dT%H:%M:%S');

    # Change the hour to 17
    datetime_object -> strptime('%Y-%m-%d %H:%M:%S',
           datetime_object -> strftime('%Y', '%m', '%d', '17', '%M', %S'));

I find this better for a number of reasons:

(a) The API is greatly simplified, as we get rid of around fifteen
    functions (whose functionality anyway duplicates strftime's).

(b) It's faster to use

      ($year, $month, $day) = datetime_object -> strftime('%Y', '%m', '%d');


      $year = datetime_object->year;
      $month = datetime_object->month;
      $day = datetime_object->day;

    because in the second case the same conversion from datetime_object
    internal format has to be done three times.

(c) I generally find it very simple and very consistent with Perl's
    syntax and philosophy.

As all things, it has a downside: some additional format specifiers may
have to be defined for the enhanced strftime (and possibly strptime),
creating incompatibility with the standard strftime.

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About