Get week number with POSIX::strftime in perl - perl

I need to get the ISO 8601 week number of todays date in perl.
What is wrong woth the following code?
#! /usr/bin/perl
use strict;
use warnings;
use Time::Local;
use POSIX qw(strftime);
my $weekNumber = POSIX::strftime("%V", localtime time);
print $weekNumber, "\n";
The output I get is simply %V and my expected result (for epoch 1407769639) is 33.
FYI using POSIX::strftime("%W", localtime time); results in 32.

It is best to use Time::Piece, which has been a core module since version 10 of Perl 5, and so shouldn't need installing unless you are running a very old version.
Time::Piece replaces the core localtime function with one that returns a Time::Piece object, so the code would look like this
use strict;
use warnings;
use Time::Piece;
print localtime->week, "\n";
output
33

The POSIX functions are thin layers over your C library. What you get is based on your C library's behaviour. You get %V because your C library's strftime doesn't recognize %V.

Related

How to convert to proper time from stat() module

I have a code to retrieve last modified date from stat() module in Perl. But the output which displayed is not in proper time format. Could you please help me to get proper time (HH:MM:SS)?
Please find the sample program I have used:
my $file="filename";
my $time=(stat($file))[9];
say "$time";
Output: 149160
Please help me convert into HH:MM:SS.
Assuming you just want hours, minutes and seconds, you can use POSIX and gmtime:
use warnings;
use strict;
use feature 'say';
use POSIX qw(strftime);
my $file="filename";
my $time=(stat($file))[9];
say strftime('%H:%M:%S', gmtime($time));
Given that you only want hours, minutes and seconds, as an alternative approach, you may use localtime or its companion function, gmtime, without the POSIX module and simply select the desired time components.
use strict;
use warnings;
use feature qw(say);
my $file = "filename";
my #time;
#time = localtime((stat($file))[9]);
say join ":", #time [2,1,0]; #...HH:MM:SS in local time
#time = gmtime ((stat($file))[9]);
say join ":", #time [2,1,0]; #...HH:MM:SS in GMT time

Convert Date DD/MM/YYYY to 7 Digit Julian Date in Perl

I have not used Julian date before and have no experience with it. I do not know how the 7-Digit needs to be converted or if there is a library that does it.
I require help with 10/10/2020 to be converted to 2459133 in Perl.
Time::Piece is part of the standard Perl library.
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use Time::Piece;
# Get a Time::Piece object from your date string
my $d = Time::Piece->strptime('10/10/2020', '%d/%m/%Y');
# Call its julian_day() method.
say $d->julian_day;

How do I get the date in MM/DD/YYYY format in perl. I am using perl 5.8

Hi I am trying to store the date in a variable $date. I will then use Excel::Writer::XLSX to print the date into a cell. I am using perl 5.8. I know a lot of the modules used for getting the date such as TimePiece were installed in later versions of perl.
use POSIX qw( strftime );
strftime('%m/%d/%Y', localtime)
You are correct that Time::Piece is only core since Perl 5.10. But you can install it from CPAN. You could then use its strftime method:
use strict;
use warnings;
use Time::Piece;
my $date = localtime->strftime('%m/%d/%Y');
Without it, you can use the built-in localtime function, which also has a nicer wrapper Time::localtime (even in 5.8). You just have to be careful because the values returned by POSIX localtime aren't exactly what you'd expect.
use strict;
use warnings;
use Time::localtime;
my $now = localtime;
my $date = sprintf '%02d/%02d/%04d', $now->mon + 1, $now->mday, $now->year + 1900;

How to convert a time format to seconds in solaris

I have a time of format 2013-04-29 08:17:58 and I need to convert it into seconds.
In UNIX, we can use date +%s. Is there anyway to do it in Solaris?
Use Time::Piece. It has been part of the standard Perl 5 distribution since version 9.5 and shouldn't need installing.
use strict;
use warnings;
use 5.010;
use Time::Piece;
my $t = '2013-04-29 08:17:58';
$t = Time::Piece->strptime($t, '%Y-%m-%d %H:%M:%S');
say $t->epoch;
output
1367223478
With a little more effort, you can do this with your horribly outdated version of Perl.
#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;
my $str = '2013-04-29 08:17:58';
my #dt = split /[- :]/, $str;
$dt[0] -= 1900;
$dt[1] -= 1;
print timelocal reverse #dt;
Time::Local has been included with Perl since the first release of Perl 5 (in 1994).
But please do what you can to get your ancient version of Perl updated.
Update: Getting a few downvotes on this. But no-one has bothered to explain why.

Undefined subroutine &main::timelocal error

I have the following error when I execute my perl module :
Undefined subroutine &main::timelocal
I have defined time and I want in the format of DDMMYYYY without any seperators.
Can anyone help me on this?
To use timelocal like that, you need to import it:
use Time::Local 'timelocal';
(and make sure you are calling it correctly; see Time::Local)
But perhaps you meant localtime? Or you might want POSIX::strftime.
You was not very specific where to get the time. This works for current date, using core Time::Piece module:
use Time::Piece;
print localtime->dmy(''); # 05042011
If you have time in variable, you can do
use Time::Piece;
print localtime($time)->dmy('');
The empty string in dmy call is separator.
The core POSIX module contains a 'strftime' function that handles all of the standard Unix date/time formatting sequences.
$ perl -MPOSIX=strftime -le'print strftime "%d%m%Y", localtime'
Or, in a program,
use POSIX 'strftime';
print strftime '%d%m%Y', localtime, "\n";
It's an old one but I had this exact error and solved it from looking at the above examples with a different solution.
Reason was that I had not included the ';' at the end of the use statement!
use Time::Local ;