Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I format dates in Perl?

I'm modifying a pre-existing script in Xcode to customize my file headers. The script is Perl and it's not my best langage. :)

I just need to insert the current date in the header in dd/mm/yy format.

Here is my script :

#! /usr/bin/perl -w
# Insert HeaderDoc comment for a header
#
# Inserts a template HeaderDoc comment for the header.
use strict;

# get path to document
my $headerPath = <<'HEADERPATH';
%%%{PBXFilePath}%%%
HEADERPATH
chomp $headerPath;
my $rootFileName = &rootFileNameFromPath($headerPath);

print "/*";
print " * $rootFileName\n";
print " * Project\n";
print " *\n";
print " * Created by Me on ";
# in bash it would be something like that :
# date +%d/%m/%y | awk '{printf "%s\n", $1}';
print " * Copyright 2009 My_companie. All rights reserved.\n";
print " *\n";
print " */\n";

sub rootFileNameFromPath {
    my $path = shift;

    my @pathParts = split (m'/', $path);
    my $filename = pop (@pathParts);
    my $rootFileName = "$filename";
    $rootFileName =~ s/\.h$//;
    return $rootFileName;
}

exit 0;

I've just modified the print command so don't ask me for the rest of the code :)

like image 506
claf Avatar asked Feb 22 '09 16:02

claf


People also ask

How do I get current month in Perl?

To get the day, month, and year, you should use localtime : my($day, $month, $year)=(localtime)[3,4,5]; To then get the last day of the month, use your code from above: $av_tmp_TODAY = DateTime->last_day_of_month({ year => $year + 1900, month => $month +1 })->ymd('-');

How do I add a timestamp in Perl?

Creating a Timestamp Timestamp can be created by creating a DateTime object and then calling the now constructor. my $datetime = DateTime->now; print "$datetime\n" ; A DateTime object can also be created by providing all the details part wise like date, hour, minute, second, etc.

What is Strftime in Perl?

You can use the POSIX function strftime() in Perl to format the date and time with the help of the following table. Please note that the specifiers marked with an asterisk (*) are locale-dependent. Specifier. Replaced by.

How do I get yesterday's date in Perl?

If you want yesterday's date: use DateTime qw( ); my $yday_date = DateTime ->now( time_zone => 'local' ) ->set_time_zone('floating') ->truncate( to => 'day' ) ->subtract( days => 1 ) ->strftime('%Y-%m-%d');


2 Answers

Rather than removing strict (!), why not just make the code strict clean?

my ($mday, $mon, $year) = (localtime(time))[3, 4, 5];

$mon  += 1;
$year += 1900;

printf "%02d/%02d/%02d\n", $mday, $mon, $year % 100;

Maybe even better (since more familiar looking to someone who asked in terms of Bash):

# At the top, under use strict;
use POSIX qw/strftime/;

# then later...
my $date = strftime "%d/%m/%y", localtime;
print "$date\n";

Funny coincidence: Perl Training Australia publishes semi-regular tips (you can get them via email or online), and just today there's a new one on strftime.

like image 90
Telemachus Avatar answered Oct 18 '22 08:10

Telemachus


You could also use DateTime and related modules, which is of course complete overkill for a little script like this. But for a larger app, you should use solid modules rather than doing everything the long way. For the record, with DateTime you'd write:

DateTime->today()->strftime('%d/%m/%y');

Or you could use the more modern CLDR format language:

DateTime->today->format_cldr('dd/MM/YYYY');
like image 20
Dave Rolsky Avatar answered Oct 18 '22 08:10

Dave Rolsky