Best way to convert date to epoch in perl [closed] - perl

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
What is the best way to convert --> Mar 11 10:29:47 2021 GMT <-- into epoch on multiple platforms.
Date::Parse module is not available by default on all platforms.

Use the strptime() ("string parse time") method from Time::Piece which has been a part of the standard Perl distribution since 5.10.0 in 2007.
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use Time::Piece;
my $str = 'Mar 11 10:29:47 2021 GMT';
my $fmt = '%b %d %H:%M:%S %Y %Z';
my $date = Time::Piece->strptime($str, $fmt);
say $date->epoch; # prints 1615458587
strptime is a standard Unix tool which is available in many forms. You can get more information about the format strings that it uses from its manual page.

Related

what does it mean 'push #{bla bla }' in Perl? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I read try to read perl code of annovar and there is a line like this:
push #{$genedb{$chr, $nextbin}}, [$name, $dbstrand, $txstart, $txend, $cdsstart, $cdsend, [#exonstart], [#exonend], $name2];
Can someone explain what does it mean?
which values put which array or hash ?
Just run this program and Data::Dumper will show the results
#! /usr/bin/env perl
use warnings;
use strict;
use utf8;
use feature qw<say>;
use Data::Dumper;
my %genedb;
my $chr = 'G';
my $nextbin = 4143;
push #{$genedb{$chr, $nextbin}}, [1..10];
print Dumper(\%genedb);
exit(0);

how to convert datetime to a yyyy-mm-dd hh:mm:ss format in perl? [duplicate]

This question already has answers here:
Converting date to specified format and two date comparisions
(2 answers)
Closed 9 years ago.
How do I convert datetime to a different format?
This is my date: Tue Feb 11 08:47:59 2014
how to convert to 2014-02-11 08:47:59
Thanks!
The Time::Piece module has been included with Perl since 2007.
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use Time::Piece;
my $input = 'Tue Feb 11 08:47:59 2014';
my $dt = Time::Piece->strptime($input, '%a %b %d %H:%M:%S %Y');
say $dt->strftime('%Y-%m-%d %H:%M:%S');
You can use the DateTime module.

Changing format of date [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
How can I change the format of dates 12/23/02 to 23/12/2002 or 03/35/55 to 35/03/2055?
I can read the dates from text file but I can't change their format:
15/06/17 ====> 06/15/2017
Here's the example using DateTime::Format::Strptime:
UPDATE:
use DateTime::Format::Strptime;
my $str = '12/23/02';
my $parser = DateTime::Format::Strptime->new( pattern => '%m/%d/%y');
my $dt = $parser->parse_datetime( $str );
print $dt->strftime('%d/%m/%Y')
output:
23/12/2002
I think the Perl Module Time::Piece should work
use Time::Piece 'strptime';
my %date = strptime('%d/%m/%y', '15/06/17');
The date hash contains then the parsed date elements, $date{d}, $date{m}, and $date{y} which you can use to reformat the date.

unicode to itrans in a text file in ubuntu 13.04 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 8 years ago.
Improve this question
I want 0 replacing '०' Unicode character. I am using a Perl script and trying the code given below.
my %table_digits =
(
'०' => '0'
'१' => '1', .....)
It's working fine with other Unicode characters. Those are being replaced by other numbers. But it is not able to replace '०' with 0. How it can be done?
See Unicode numeric value in Unicode::UCD:
use 5.010;
use utf8;
use open ':std', ':utf8';
use Unicode::UCD qw(num);
for my $digit (qw( ० १ २ ३ ४ ५ ६ ७ ८ ९ )) {
say "$digit==".num($digit);
}

How to append system date to a filename in perl ? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to append system date to a filename in perl ?
As i am very new to perl programming , can you give me a simple example for the above query.
The strftime() function from the POSIX module gives an easy way to get a date in whatever format you want.
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use POSIX 'strftime';
my $date = strftime '%Y-%m-%d', localtime;
say $date;
You can then use that string in the filename of your file. If you are renaming a file, then you can use move() from the File::Copy module.
This might help u out!
#!/usr/bin/perl
my $date=`date +%Y%m%d`;
chomp($date);
my $source_file="/tmp/fileName_.tgz";
my $destination_file="/misc/fileName_" . $date . ".tgz";
print "$source_file\n";
print "$destination_file\n";
system("sudo mv /tmp/fileName_.tgz /misc/fileName_$date.tgz");
or try this
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900;
my $out = "$dir/$host-$mday-$mon-$year"
or this one
# grab the current time
my #now = localtime();
# rearrange the following to suit your stamping needs.
# it currently generates YYYYMMDDhhmmss
my $timeStamp = sprintf("%04d%02d%02d%02d%02d%02d",
$now[5]+1900, $now[4]+1, $now[3],
$now[2], $now[1], $now[0]);
# insert stamp into constant portion of file name.
# the constant portion of the name could be included
# in the sprintf() above.
my $fileName = "File$timeStamp.log";