Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl datetime generated?

Tags:

datetime

perl

I have a perl script that takes system date and write that date to a filename.

System date is assigned to TRH1 variable and then it set to a filename.

$TRH1 =`date + %Y%m%d%H%M`;
print "TRH1 => $TRH1\n";
open(TPFILE, ">./log/errTS_TPList_$TRH1.csv"); 

However, TRH1 seems to have correct value but file name has a question mark, that I do not desire.

TRH1 => 202103031940
errTS_TPList_202103031940?.csv

Can you show me how to get rid of this "?" ?

like image 706
yed2393 Avatar asked Jun 30 '26 17:06

yed2393


1 Answers

As you've already been shown, it's because the output from date has a newline character at the end that you need to remove with chomp().

However, there's really no need to use an external program to get the date and time. Modern versions of Perl come with Time::Piece which makes this trivial:

use Time::Piece;

# Perl style generally reserves uppercase variable names
# for constants
my $trh1 = localtime->strftime("%Y%m%d%H%M");

With an older version of Perl, you can use strftime() from POSIX:

use POSIX 'strftime';

my $trh1 = strftime("%Y%m%d%H%M", localtime);
like image 94
Dave Cross Avatar answered Jul 03 '26 13:07

Dave Cross



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!