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

Does anyone know how to generate a timestamp? I have a need to generate a timestamp for a specific date & time. This timestamp will then be used as the selection criteria to extract data from a UNIX file. Any ideas? I suspect that this will be either glaringly obvious or near impossible. I do hope it's the former. Cheers Ronnie Cruickshank

Replies are listed 'Best First'.
Re: Timestamps
by amw1 (Friar) on Jun 23, 2004 at 15:35 UTC
    You can get the current time with the time() function. YOu can generate a specific time value with Time::Local
    use Time::Local; $now = time(); $specific_time = timelocal($seconds, $minutes, $hour, $month_day, $mon +th, $year);
    $now and $specific_time are both represented as seconds since midnight jan 1, 1970. use localtime() to extract the seoncs, minues, hours etc from this.
      Thanks that did the trick! You have to enjoy Perl's consistency. Localtime to extract timestamp details and to reverse the process you reverse the verb name! Cheers, Ronnie
Re: Timestamps
by Limbic~Region (Chancellor) on Jun 23, 2004 at 15:34 UTC
    Ronnie,
    perldoc Time::Local should give you what you are looking for - it is btw, only one of many ways to do it.

    Cheers - L~R

Re: Timestamps
by Happy-the-monk (Canon) on Jun 23, 2004 at 15:36 UTC

    how to generate a timestamp?

    Just have a look at this node and at   perldoc -f localtime   and all that's left to do is to find the documentation that tells you how your timestamps are supposed to look like.

    Cheers, Sören

Re: Timestamps
by davido (Cardinal) on Jun 23, 2004 at 15:39 UTC

    How much resolution do you need? If "to the second" accuracy is sufficient, what's wrong with using localtime in scalar context?

    If you need better resolution than one second, you can use Time::HiRes's Time::HiRes::time() function, which returns floating-point seconds since epoc.


    Dave

Re: Timestamps
by Grygonos (Chaplain) on Jun 23, 2004 at 16:41 UTC
    An example of a timestamping method I use
    my @time = (localtime)[1..5]; my $filename = ($time[4]+1900)."-" .($time[3]+1)."-".$time[2]."_" .$time[1]."-".$time[0];
Re: Timestamps
by hsinclai (Deacon) on Jun 23, 2004 at 15:46 UTC
    There are a couple timestring formatting examples on my pad ..

    hsinclai





Re: Timestamps
by wufnik (Friar) on Jun 23, 2004 at 22:07 UTC
    simple code for a YYYYMMDDHHMMSS time stamp follows...
    use Time::localtime; my $tm = localtime; my $timestamp = sprintf("%04d%02d%02d%02d%02d%02d", $tm->year+1900, ($tm->mon)+1, $tm->mday, $tm->hour, $tm->min, $tm->sec);
    note irritating little additions to deal with year/month.

    all the best,
    ...wufnik

    -- in the world of the mules there are no rules --
Re: Timestamps
by Agyeya (Hermit) on Jul 06, 2004 at 17:11 UTC
    Hi!
    I hope this pertains to your porblem. Incase you want the timestamp in an MySql table, then you can set the field data type as timestamp. So whenever you insert a record into this table, the time stamp will be automatically generated.
    I hope this helps.