Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

String Operation

by Anonymous Monk
on Jan 02, 2002 at 22:23 UTC ( [id://135743]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I have a file name as follows: new.file.d{activity_date}.t{activity_time}. I need to replace {activity_date} with ddmmyyyy and {activity_time} with hhmmss

Replies are listed 'Best First'.
Re: String Operation
by mrbbking (Hermit) on Jan 02, 2002 at 22:47 UTC
    Don't let us stop you. :-)

    #!/usr/bin/perl -w use strict; sub timestamp { # return current timestamp in HHMMSS format my ($second, $minute, $hour) = (localtime)[0,1,2]; sprintf ("%02d%02d%02d", $hour, $minute, $second); } sub datestamp { # return current date in DDMMCCYY format my ($day, $month, $year) = (localtime)[3,4,5]; sprintf ("%02d%02d%04d", $day, $month + 1, $year + 1900); } my $filename = 'new.file.d{activity_date}.t{activity_time}'; $filename =~ s/{activity_date}/datestamp/e; $filename =~ s/{activity_time}/timestamp/e; print $filename;
Re: String Operation
by mkmcconn (Chaplain) on Jan 02, 2002 at 22:43 UTC
    You'll want to read about localtime and POSIX:
    Converts a time as returned by the time function to a 9-element array
    with the time analyzed for the local time zone.  Typically used as
    follows:
    
        #  0    1    2     3     4    5     6     7     8
        ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
                                                    localtime(time);
    
    mkmcconn
Re: String Operation
by count0 (Friar) on Jan 02, 2002 at 22:51 UTC
    Alternatively, POSIX's strftime() can be quite useful for this sort of thing:
    $activity_date = strftime("%d%m%Y", localtime(time)); for example.
Re: String Operation
by Juerd (Abbot) on Jan 02, 2002 at 22:47 UTC
    And your question is...?

    Maybe this helps, maybe not:
    my @now = localtime(); $now[4]++; $now[5] += 1900; $_ < 10 and $_ = "0$_" for @now[0..4]; my $ddmmyyyy = $now[3] . $now[4] . $now[5]; my $hhmmss = $now[2] . $now[1] . $now[0];

    2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

Re: String Operation
by Anonymous Monk on Jan 03, 2002 at 19:53 UTC
    I would suggest replacing {activity_date} by yyyymmdd.
    That way your files will sort in date (and time) order.

    Robin Barker

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://135743]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-23 10:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found