Jaap has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed Monks, My database wants me to generate a datetime in this format:
YYYYMMDDHHMMSS
Perl's localtime() function has annoyed me since the begining of time but i still use it to solve this problem. Anyone can think of a better/faster/prettier/shorter(golf) solution (not using DateTime etc.)?
#!/usr/bin/perl -w use strict; use Benchmark; use POSIX; Benchmark::cmpthese(100000, { 'Reference Implementation' => ' my @time = localtime(); my $time = sprintf("%04d%02d%02d%02d%02d%02d", $time[5] + 1900 +, $time[4] + 1, $time[3], $time[2], $time[1], $time[0]); ', 'Reverse Array' => ' my @time = localtime(); my $time = sprintf("%04d%02d%02d%02d%02d%02d", $time[5] + 1900 +, $time[4] + 1, reverse(@time[0 .. 3])); ', 'Shorter Array' => ' my @time = (localtime())[0 .. 5]; my $time = sprintf("%04d%02d%02d%02d%02d%02d", $time[5] + 1900 +, $time[4] + 1, $time[3], $time[2], $time[1], $time[0]); ', 'strftime by Joost' => ' my $time = strftime("%Y%m%d%H%S",localtime); ' });

Replies are listed 'Best First'.
Re: datetimestamp faster/golf/prettier
by Joost (Canon) on Jul 20, 2004 at 14:50 UTC
Re: datetimestamp faster/golf/prettier
by dragonchild (Archbishop) on Jul 20, 2004 at 14:53 UTC
    What database are you using that requires you to do such a ridiculous thing?? Surely there's a function that your database provides that will allow you to either cast a string to the appropriate structure and/or provide the timestamp yourself.

    For example, MySQL will do timestamping for you with the TIMESTAMP column. You can create a trigger in Oracle that will populate the timestamping column at the right time. Other RDBMS's have similar features. The only one I can think of that might require this is SQLite, but you aren't required to put it in any format for SQLite (as it stores everything as strings, anyway).

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

      I'm using MySQL and i use the column with the timestamp as a (shared) primary key. I tried to insert a NULL and it gave me a nice error.
        I have a few questions:
        • Which version of MySQL? The handling of TIMESTAMP changes in v4.1.0 and 4.1.2
        • What error are you seeing?
        • Can you show me the structure of the table and the code used to insert the value?

        ------
        We are the carpenters and bricklayers of the Information Age.

        Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

        I shouldn't have to say this, but any code, unless otherwise stated, is untested

Re: datetimestamp faster/golf/prettier
by Jaap (Curate) on Jul 20, 2004 at 14:59 UTC
    Benchmark: timing 100000 iterations of Reference Implementation, Rever +se Array, Shorter Array, strftime... Reference Implementation: 1 wallclock secs ( 0.66 usr + 0.03 sys = +0.69 CPU) @ 144927.54/s (n=100000) Reverse Array: 1 wallclock secs ( 0.85 usr + 0.03 sys = 0.88 CPU) @ + 113636.36/s (n=100000) Shorter Array: 0 wallclock secs ( 0.63 usr + 0.04 sys = 0.67 CPU) @ + 149253.73/s (n=100000) strftime: 1 wallclock secs ( 0.52 usr + 0.07 sys = 0.59 CPU) @ 16 +9491.53/s (n=100000) Rate Reverse Array Reference Implementati +on Shorter Array strftime Reverse Array 113636/s -- -2 +2% -24% -33% Reference Implementation 144928/s 28% +-- -3% -14% Shorter Array 149254/s 31% +3% -- -12% strftime 169492/s 49% 1 +7% 14% --
      I would strongly recommend timing for a given number of seconds, preferably 5 or 10. Look at the POD for Benchmark as to how to do this.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

      I shouldn't have to say this, but any code, unless otherwise stated, is untested