Perl : How to obtain epoch time [duplicate] - perl

This question already has answers here:
How do I convert a date/time to epoch time (unix time/seconds since 1970) in Perl?
(14 answers)
Closed 6 years ago.
I need a quick way to convert date "Tue Jul 02 17:50:55 MDT 2024" into epoch time.
Effectively, I need the shell equivalent of date -d "Tue Jul 02 17:50:55 MDT 2024" +%s
I currently use the following in perl but it requires a lot of conversion. Hoping to find a simpler and more elegant solution.
$current_epoch = timelocal($seconds, $minute, $hour, $day_of_month, $month_num, $year)
I currently use Perl 5.8 ; I dont believe it has "Time::Piece"
I am only interested in solutions that do not require downloading another library

The Date::Parse module can turn datetime stamps into epoch times. In general, if you have something you need to do, search the Comprehensive Perl Archive Network for a module to handle it. Searching Stackoverflow is another good way to get answers to common questions.

Related

Subtracting a Minute in Perl

I have a variable in Perl that I initialized as $invoice_date = '1/6/14' (June 1st 2014). How can I determine the datatype that Perl considers this variable to be?
I'd like to subtract a minute from the invoice date to get May 31 2014 11:59PM. How can I do this with or without declaring $invoice_date to be a certain datatype?
Update: Thanks for the comments and answers. Since it is a string, I am going to try to concatenate the time portion. I have a another variable $period_end_date which is set to May 31, 2014. I'm going to try to concatenate the 11:59PM to it.
The string is subsequently sent in a SQL statement. If I can figure out what SQL expects for the string, it should be possible to insert the time portion.
You need some date manipulation module as '1/6/14' is plain string, and two digit years were abandoned prior to Y2K event.
use Time::Piece;
use Time::Seconds;
my $t = Time::Piece->strptime("1/6/2014", "%d/%m/%Y");
$t -= ONE_MINUTE;
print $t;
output
Sat May 31 23:59:00 2014

Perl workweek conversion is incorrect

I'm faced a weird problem.
I have date in form of Tue Feb 25 00:20:13 2014.
my task is to calculate the week number and the week day.
I tried the following
use Time::Piece;
my $date="Tue Feb 25 00:20:13 2014";
my $db_date=Time::Piece->strptime($date, "%a %b %d %H:%M:%S %Y");
my $ww=$db_date->strftime("%W.%w-%Y);
print $ww;
When I run the script I get the output as
08.2-2014
which is wrong, the expected output is
09.2-2014
I want to know where did i go wrong?
pls help...
You're using the "%W" strftime() conversion. Time::Piece doesn't specify the meaning of "%W", but the documentation for the equivalent C function says that "%W" starts counting with the first week that contains a Monday. It sounds like you want the ISO 8601 week number, which starts counting with the first week that contains at least four days, in which case the "%V" conversion should do what you want.

Time::Piece is returning a wrong value

I am trying to convert date from yymmdd to YYYY-MM-DD with Time::Piece module. With the input as Nov 31, 2000 (20001131), I am getting output as 2000-12-01. In reality, Nov 31 doesn't even exists.
use Time::Piece;
my $dt_str = Time::Piece->strptime('20001131', '%Y%m%d')->strftime('%Y-%m-%d');
print $dt_str;
Am I missing something here?
Internally, it does only rough validation and error reporting, and then performs the same transformations as POSIX::mktime does; any days beyond the end of a month will just cause it to advance the produced date into the next month. This does seem a little inconsistent; since it allows that for days, I'd also expect it to treat '20005931' as '2004-12-01', but instead it errors out.

Perl equivalent of PHP's strtotime()?

I realize that Perl has strftime, which allows you to pass a formatting object. The functionality I'm wondering if I can find is more like the following (from PHP):
$string1 = "Jun 6, 2012";
$string2 = "June 06 2012";
if (strtotime($string1) == strtotime($string2)) {
echo "BLAMMO!";
}
// will echo "BLAMMO!"
The reason for this is a business need in which user-provided dates need to be compared for validation and extended logic (does this date fall within another daterange also provided, etc). Now, I realize I can write an entire library devoted to doing this, and I realize there are any number of potential pitfalls with date-parsing and you should never trust user input, but here are some basic assumptions.
The input is actually output from any number of software packages that conform to their own internal specifications for date formatting. They all follow some standard, but those standards are not uniformly normalized between programs. That being said, I should always be comparing two dates from the same program, but I may never know what format they may follow.
I realize the standards of any given system are likely to be different, but the assumption here is that we're feeding ALL of our dates into the same thing, so we can trust a consistent implementation, hopefully something in CPAN or another easily updated module.
Date::Parse supplies str2time, which does this.
The documentation lists some examples that the module can parse:
1995:01:24T09:08:17.1823213 ISO-8601
1995-01-24T09:08:17.1823213
Wed, 16 Jun 94 07:29:35 CST Comma and day name are optional
Thu, 13 Oct 94 10:13:13 -0700
Wed, 9 Nov 1994 09:50:32 -0500 (EST) Text in ()'s will be ignored.
21 dec 17:05 Will be parsed in the current time zone
21-dec 17:05
21/dec 17:05
21/dec/93 17:05
1999 10:02:18 "GMT"
16 Nov 94 22:28:20 PST
The standard for date/time manipulation in Perl is the DateTime project.
https://metacpan.org/pod/DateTime
strptime for Perl can be obtained in the core module Time::Piece. This offer a core module solution whereas the rich DateTime module unfortunately isn't part of core Perl.

What's the opposite of the localtime function in Perl?

In Perl, localtime takes a Unix timestamp and gives back year/month/day/hour/min/sec etc. I'm looking for the opposite of localtime: I have the parts, and I'd like to build a unix timestamp from them.
You can use the timelocal function in the Time::Local CPAN module.
NAME
Time::Local - efficiently compute time
from local and GMT time
SYNOPSIS
$time = timelocal($sec,$min,$hour,$mday,$mon,$year);
$time = timegm($sec,$min,$hour,$mday,$mon,$year);
DESCRIPTION
This module provides functions that
are the inverse of built-in perl
functions localtime() and gmtime().
They accept a date as a six-element
array, and return the corresponding
time(2) value in seconds since the
system epoch (Midnight, January 1,
1970 GMT on Unix, for example). This
value can be positive or negative,
though POSIX only requires support for
positive values, so dates before the
system's epoch may not work on all
operating systems.
It is worth drawing particular
attention to the expected ranges for
the values provided. The value for the
day of the month is the actual day (ie
1..31), while the month is the number of months since January (0..11). This
is consistent with the values returned
from localtime() and gmtime().
Note: POSIX::mktime is a just a wrapper around your C library's mktime() function. Time::Local is a pure-Perl implementation, and always returns results matching Perl's localtime. Also, Time::Local offers gmtime, while mktime only works in local time. (Well, you could try changing $ENV{TZ}, but that doesn't work on some systems.)
POSIX::mktime
DateTime on CPAN might of of some use. It also has a lot of time manipulation/translation methods.
Just create the DateTime using your parts and call $datetime->formatter("%s") ;