get month year from a given date - perl

I have a file that has a list of dates. I want to get the information of Month Year. I was doing the following: (I omit the part of open a file)
$request_date = "2012-01-02 08:12:11";
chomp(my $monthdatefile = `date '+%B %Y' --date='$request_date'`);
but it takes too much.
From Unix will be that what I want:
$ date '+%B %Y' --date='2012-01-02 08:12:11'
January 2012

Use Time::Piece like this
use strict;
use warnings;
use Time::Piece;
my $request_date = '2012-01-02 08:12:11';
my $tp = Time::Piece->strptime($request_date, '%Y-%m-%d %H:%M:%S');
my $month_year = $tp->strftime('%B %Y');
print $month_year;
output
January 2012

If you care about speed, I'd just have a hash table of 12 entries for months.
Then extract the fields as follows:
my %hash = (1 => 'January', 2=>'February', ...);
$request_date =~ /^(\d+)-(\d+)-/;
my ($year,$month) = ($1,$2);
print $hash{$month}." ".$year;

Related

Perl Format dates

use Date::Calc qw(:all);
use Time::Piece;
use POSIX qw(strftime);
$a1 = '01.01.1963';
($year, $month, $day) = Decode_Date_US($a1);
print "$year $month $day\n";
$formatted = strftime('%m/%d/%Y',$month,$day,$year);
print "$formatted\n";
I am trying to format dates in a consistent format using POSIX(strftime). I am uncertain what the input format might be. I am using Decode_Date_US to extract the relevant year, month, day information. I am then trying to format the dates in consistent manner using strftime. I am getting following error
Usage: POSIX::strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1) at test_dates.pl line 60
Any help would appreciated.
"use Time::Piece" would be eventually used to sort the dates.
Thanks
Just use Time::Piece and strptime
#!/usr/bin/env perl
use strict;
use warnings;
use Time::Piece;
my $date = '01.01.1963';
my $timestamp = Time::Piece->strptime( $date, "%m.%d.%Y" );
print $timestamp -> strftime( "%m/%d/%Y")
But if the format is inconsistent, then you're onto a loser - for starters, there's plenty of places that transpose day/month ordering, or represent year as two digits (that sometimes will clash with month or day).
It's inherently ambiguous. You can - at best - apply some very crude guessing by grabbing groups of digits and assuming that they're in a consistent order.
E.g.
my ( $day, $month, $year ) = m/(\d{2}).*(\d{2}).*(\d{4})/;
$timestamp = Time::Piece -> strptime ( "$year-$month-$day", "%Y-%m-%d" );
But you'll be tripped up by dates like 10/11/12 because you simply can't know which number is which field. You can try guessing, by evaling the strptime and just retrying different formats until you get one that does decode to something valid.
As you indicate - Decode_Date_US basically does this so:
use strict;
use warnings;
use Date::Calc qw(:all);
use Time::Piece;
my $a1 = '01.01.1963';
my ($year, $month, $day) = Decode_Date_US($a1);
my $time = Time::Piece -> strptime("$year/$month/$day", "%Y/%m/%d");
print $time->strftime("%Y-%m-%d"),"\n";
Also - use strict; use warnings; is good.
And - there's one correct way to write dates. If you're going to reformat at all, then the one that isn't ambiguous is the best choice.
I ended up using DateTime module's strftime function which can handle dates earlier than 1970.
"Crashing" -Perl command line interpreter has stopped working. If it helps, I am using a windows machine and a very infrequent perl user.
this worked for me
use Date::Calc qw(:all);
use DateTime;
$dateformat = "%m/%d/%Y";
($year, $month, $day) = Decode_Date_US($date);
$date = DateTime->new(
year=>$year,
month=>$month,
day=>$day,
);
$date = $date->strftime($dateformat);

Add time to ISO 8601 times in Perl

I have an ISO 8601 time stored in a variable and I have some number of hours stored in another variable like this:
my $current_time = shift; #looks like: 2015-07-01T15:38:08Z
my $hours = shift; # looks like: 12
My goal is to add the hours to the current time, but there doesn't seem to be any built in Perl function to do it. In Powershell, you can do something like this:
$currentTime = $currentTime .AddHours($hours)
Is there an easy way to do this in Perl?
That specific ISO 8601 profile is also known as RFC3339.
use DateTime::Format::RFC3339;
my $dt = DateTime::Format::RFC3339->parse_datetime('2015-07-01T15:38:08Z');
$dt->add( hours => 1 );
print "$dt\n"; # 2015-07-01T16:38:08Z
If you want to accept arbitrary ISO 8601 profiles, you can use DateTime::Format::ISO8601.
use DateTime::Format::ISO8601;
my $dt = DateTime::Format::ISO8601->parse_datetime('2015-07-01T15:38:08Z');
$dt->set_time_zone('UTC'); # Convert to UTC ("Z") if it's not already.
$dt->add( hours => 1 );
print $dt->iso8601().'Z', "\n"; # 2015-07-01T16:38:08Z
I posted these alternatives because these modules are far less error-prone to use than Time::Piece.
You can also use Time::Moment. In the interest of full disclosure, I am the author of Time::Moment.
say Time::Moment->from_string('2015-07-01T15:38:08Z')
->plus_hours(1);
Output:
2015-07-01T16:38:08Z
Rather easy with Time::Piece:
#! /usr/bin/perl
use warnings;
use strict;
use Time::Piece;
use Time::Seconds;
my $current_time = '2015-07-01T15:38:08Z';
my $hours = 12;
my $format = '%Y-%m-%dT%H:%M:%SZ';
my $time = 'Time::Piece'->strptime($current_time, $format);
$time += $hours * ONE_HOUR;
print $time->strftime($format), "\n";

Finding the time between two dates in Perl

I am trying to calculate and display, in minutes, the difference between the entered date and the present time. I have:
print "Enter a date YYYY MM DD. Remember perl's months go from 0-11.\n";
while ( #dateEnt < 1 ) {
my $dateEntered = <STDIN>;
chomp $dateEntered;
push #dateEnt, $dateEntered;
(#datedata) = split( /\s+/, $dateEntered );
$year = $datedata[0];
$month = $datedata[1];
$day = $datedata[2];
}
$time = time;
$readabletime = localtime($time);
use Time::Local;
$timeB = localtime [1];
$timeBetween = $readabletime[1] - $timeB;
print "The time between the dates is $timeBetween\n";
When I execute this, I get nothing for an answer. Any help?
Lets take a look at this last chunk
use Time::Local;
$timeB = localtime[1];
$timeBetween = $readabletime[1] - $timeB;
print "The time between the dates is $timeBetween\n";
The use statement is normally put at the top of the program because it's actually executed at compile time, but it's fine anywhere really
localtime[1] looks like you're trying to access the second element (Perl arrays start at index zero) of an array called localtime. But what you're actually doing is creating an anonymous array that contains the single value 1 and passing a reference to it to localtime. If you try print[1] instead, you'll see that you get something like ARRAY(0x22c4000). So localtime is trying to convert a memory address into a date and time
$readabletime[1] is attempting to access the second element of array #readabletime, which doesn't exists so it will evaluate to undef
You end up trying to subtract something like the string "Thu Mar 4 18:50:24 1971" from undef which is meaningless. I think you should get a fatal error something like
Argument "Thu Mar 4 18:50:24 1971" isn't numeric in subtraction
use strict; and use warnings; would have told you about a lot of the problems.
that while loop is redundant, because you push without any validation. So it'll always be true on the second iteration.
which makes #dateEnt redundant.
localtime in a scalar context gives a string. You cannot do maths with a string. (Occasionally you can cheat, because perl can convert it to a number, but that doesn't work with a date string).
use is usually at the top of a program, because it's the first thing 'done' regardless
localtime in an array context returns an array of values. ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); you can do (localtime)[1] which gives you $min. But that's not too meaningful for comparing anyway.
$readabletime[1] refers to the second element of an array called #readabletime. This doesn't exist.
So to accomplish what you're after - I would suggest using Time::Piece (core perl module) like this:
#!/usr/bin/perl
use strict;
use warnings;
use Time::Piece
my $date;
while ( not $date ) {
print "Enter a date YYYY MM DD\n";
my $dateEntered = <STDIN>;
chomp $dateEntered;
$date = Time::Piece->strptime( $dateEntered, "%Y %m %d" );
}
print "Understood: $date\n";
my $timebetween_s = ( time() - $date->epoch );
print $timebetween_s, "s between then and now\n";
Note - it uses strptime to convert the entered date to a Time::Piece object (which you can just print if you want and get a stringified date, but you can also do other transformations). strptime supports lots of different formats.
But you just subtract $date->epoch - which gets the time in seconds since 1st Jan 1970, from time() which is also the time in seconds since 1st Jan 1970. This gives you a time delta like you want - in seconds. You'll need to divide it if you want different units.
you can try this
use strict;
use warnings;
use DateTime;
my $from_date = DateTime->new(
year => 2014,
month => 9,
day => 24,
hour => 9,
minute => 13,
second => 8,
time_zone => 'local',
);
print "$from_date\n";
my $current_date = DateTime->now( time_zone => 'local' )->set_time_zone('floating');
print "$current_date\n";
my $time_diff = $current_date - $from_date;
print 'year = ', $time_diff->years, "\n";
print 'month = ', $time_diff->months, "\n";
print 'days = ', $time_diff->days, "\n";
print 'hours = ', $time_diff->hours, "\n";
print 'minutes = ', $time_diff->minutes, "\n";
print 'secondss = ', $time_diff->seconds, "\n";
Result:
2014-09-24T09:13:08
2015-09-25T21:31:37
year = 1
month = 0
days = 1
hours = 12
minutes = 18
secondss = 29

convert date format from 9/15/12 to 255 in perl

I was wondering if someone could show me how to convert 9/15/12 to 255 format.
Something in php from getdate array you can get ydate.
I think you're asking how you can get the 1 <= yday <= 366 day representation of a date, similar to yday in php's getdate(). As is common in PERL, there's more than one way to do it. The simplest mechanism would be to use localtime() for today's date:
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
If you want to do it for a different date, I'd probably use the TimeDate CPAN module
use Date::Parse;
print time2str("%j",str2time("9/15/2012"));
I think Tawnos's mentioning php's yday from getdate() is on target. Here's one option (assuming 2012 is the year in your date string):
use strict;
use warnings;
use Date::Calc qw/Day_of_Year/;
my $date = '9/15/12';
my ( $month, $day, $year ) = split '/', $date;
my $doy = Day_of_Year( "20$year", $month, $day ) - 1;
print $doy; # (0 - 365)
Output:
258
Do you want the day of the year? That would be day 258 (0-based) or the 259th day (1-based), though. Using only core Perl:
use Time::Local qw( timegm );
my $date = '9/15/12';
my ($m,$d,$y) = split(qr{/}, $date);
my $epoch = timegm(0,0,0, $d,$m-1,$y);
my $yday = ( gmtime($epoch) )[7]; # 258 (0-based index)
Note that timegm+gmtime is applicable no matter the time zone of the date.
Here's another way, using the core module Time::Piece :
#!/usr/bin/env perl
use strict;
use warnings;
use Time::Piece;
my $t = Time::Piece->strptime(shift,"%m/%d/%y");
print $t->yday, "\n";
The day output is zero-relative: January 01 = 0.
my $dt = '9/15/2012';
my ( $m, $d, $y ) = split( '/', $dt );
my $t = POSIX::mktime( 0, 0, 0, $d, $m - 1, $y - 1900 );
say [ localtime $t ]->[7] - 3;
Julian date (minus 1) is slot 7 in the list return from localtime and gmtime.
Why subtract 3? I don't know; Sept 15 is 259 Julian. Still it performs the function mapping '9/15/2012' to 255.
You can use only function strftime from core module POSIX.
use strict;
use POSIX qw(strftime);
print strftime "%j", localtime(time);

How can I change the timezone of a datetime value in Perl?

Using this function:
perl -e 'use Time::Local; print timelocal("00","00","00","01","01","2000"),"\n";'
It will return an epochtime - but only in GMT - if i want the result in GMT+1 (which is the systems localtime(TZ)), what do i need to change?
Thanks in advance,
Anders
use DateTime;
my $dt = DateTime->now;
$dt->set_time_zone( 'Europe/Madrid' );
There is only one standard definition for epochtime, based on UTC, and not different epochtimes for different timezones.
If you want to find the offset between gmtime and localtime, use
use Time::Local;
#t = localtime(time);
$gmt_offset_in_seconds = timegm(#t) - timelocal(#t);
While Time::Local is a reasonable solution, you may be better off using the more modern DateTime object oriented module. Here's an example:
use strict;
use DateTime;
my $dt = DateTime->now;
print $dt->epoch, "\n";
For the timezones, you can use the DateTime::TimeZone module.
use strict;
use DateTime;
use DateTime::TimeZone;
my $dt = DateTime->now;
my $tz = DateTime::TimeZone->new(name => "local");
$dt->add(seconds => $tz->offset_for_datetime($dt));
print $dt->epoch, "\n";
CPAN Links:
DateTime
You just need to set the timezone. Try:
env TZ=UTC+1 perl -e 'use Time::Local; print timelocal("00","00","00","01","01","2000"),"\n";'
Time::Local::timelocal is the inverse of localtime. The result will be in your host's local time:
$ perl -MTime::Local -le \
'print scalar localtime timelocal "00","00","00","01","01","2000"'
Tue Feb 1 00:00:00 2000
Do you want the gmtime that corresponds to that localtime?
$ perl -MTime::Local' -le \
'print scalar gmtime timelocal "00","00","00","01","01","2000"'
Mon Jan 31 23:00:00 2000
Do you want it the other way around, the localtime that corresponds to that gmtime?
$ perl -MTime::Local -le \
'print scalar localtime timegm "00","00","00","01","01","2000"'
Tue Feb 1 01:00:00 2000
An other example based on DateTime::Format::Strptime
use strict;
use warnings;
use v5.10;
use DateTime::Format::Strptime;
my $s = "2016-12-22T06:16:29.798Z";
my $p = DateTime::Format::Strptime->new(
pattern => "%Y-%m-%dT%T.%NZ",
time_zone => "UTC"
);
my $dt = $p->parse_datetime($s);
$dt->set_time_zone("Europe/Berlin");
say join ' ', $dt->ymd, $dt->hms; # shows 2016-12-22 07:16:29
The Algorithm
If you want to change a time value from one timezone to another timezone, you must be able to indicate both timezones.
After all, if you set if you want to convert "12:30" to GMT or US/Eastern or Venezuelan time, which means adding/subtracting some amount of hours or hours and minutes, you need to know what timezone is the starting time zone, otherwise, the calculation won't know how much to add or subtract.
If you use DateTime->now;, the timezone is defaulted to the system-time, which may not be the timezone you want to convert from.
In the below code, I demonstrate how to initialize the datetime object to the right starting timezone (fromtimezone) and how to convert that time to the ending timezone (totimezone)...
Working Code
I could not find a Perl sandbox online with the DateTime CPAN module installed.
use strict;
use DateTime;
sub convertTimeZonesForTime {
my ($args) = #_;
my $time = $args->{time};
my $date = $args->{date};
my $totimezone = $args->{totimezone};
my $fromtimezone = $args->{fromtimezone};
my $format = $args->{format} || '%H:%M:%S';
my ($year, $month, $day) = map {int $_} split('-', $date);
my ($hour, $minute, $second) = map {int $_} split(':', $time);
$year ||= 1999 if !defined $year;
$month ||= 1 if !defined $month;
$day ||= 1 if !defined $day;
$hour ||= 12 if !defined $hour;
$minute ||= 30 if !defined $minute;
$second ||= 0 if !defined $second;
my $dt = DateTime->new(
year=>$year,
month=>$month,
day=>$day,
hour=>$hour,
minute=>$minute,
second=>$second,
time_zone => $fromtimezone,
);
my $formatter = new DateTime::Format::Strptime(pattern => $format);
$dt->set_formatter($formatter);
$dt->set_time_zone($totimezone);
return "$dt";
}
print(convertTimeZonesForTime({
'totimezone'=>'America/Denver',
'fromtimezone'=>'US/Eastern',
'time'=>'12:30:00',
}));
Output:
10:30:00