PERL localtime Warning

Hello All,

I am facing a warning "Argument "" isn't numeric in localtime at"
what i m using is below
my $timestamp = Timestamp(time);

go_log("###############$timestamp###############");

can some one please suggest the way to avoid this message :confused:

And can you please suggest what you are trying to do here ?
For I don't see that warning -

$
$ perl -le 'my $timestamp = Timestamp(time); go_log("###############$timestamp###############");'
Undefined subroutine &main::Timestamp called at -e line 1.
$
$

tyler_durden

Would have to know what your

  Timestamp()  

function looks like....

if you want seconds since the epoch ( 00:00:00 Jan 01, 1970 GMT, not counting leap seconds):

do one of these:

bash> date +%s
bash> perl -e 'print time'

If you want a text formatted string for "$timestamp", based on the current time, the cannonical way to do this in perl is simply

bash> perl -e 'print scalar localtime'
ub Timestamp
{
 my ($time) = (@_);
 my ($sec, $min, $hour, $mday, $month, $year, $wday, $yday, $isdst) = localtime($time);
if(length($hour) == 1) {
		$hour = "0" . $hour;
	}
	if(length($min) == 1) {
		$min = "0" . $min;
	}
	if(length($sec) == 1) {
		$sec = "0" . $sec;
	}

	# Set to 2 digit month from 01 to 12
	$month += 1;
	if(length($month) == 1) {
  		$month = "0" . $month;
	}
	# Set to 2 digit day
	if(length($mday) == 1) {
		$mday = "0" . $mday;
	}
	# Use four digit year ($year is a value based on # of years since 1900)
	$year += 1900;
	
	$currentTime = "$hour$min";			# Set for checking suspending activity
	
	return("$year-$month-$mday $hour:$min:$sec");
}

This works fine for me -

$
$
$ cat timecalc_1.pl
#!perl -w
my $timestamp = Timestamp(time);
print "Timestamp = $timestamp\n";
sub Timestamp {
  my ($time) = (@_);
  my ($sec, $min, $hour, $mday, $month, $year, $wday, $yday, $isdst) = localtime($time);
  if(length($hour) == 1) {
    $hour = "0" . $hour;
  }
  if(length($min) == 1) {
    $min = "0" . $min;
  }
  if(length($sec) == 1) {
    $sec = "0" . $sec;
  }
  # Set to 2 digit month from 01 to 12
  $month += 1;
  if(length($month) == 1) {
    $month = "0" . $month;
  }
  # Set to 2 digit day
  if(length($mday) == 1) {
    $mday = "0" . $mday;
  }
  # Use four digit year ($year is a value based on # of years since 1900)
  $year += 1900;
  return("$year-$month-$mday $hour:$min:$sec");
}
$
$

The code for formatting the output is way too verbose. You could probably use "sprintf" here.

$
$ cat timecalc_2.pl
#!perl -w
my $fmt_ts = formatted_timestamp(time);
print "Formatted timestamp = $fmt_ts\n";
sub formatted_timestamp {
  my ($t) = (@_);
  #@x = ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst)
  my @x = localtime($t);
  return sprintf("%4d-%02d-%02d %02d:%02d:%02d", $x[5]+1900, ++$x[4], $x[3], $x[2], $x[1], $x[0]);
}
$
$

tyler_durden

If your security policy will permit shelling out, I find this idiom easier than what Tyler sent; It's less error prone:

chomp( my $timestamp = qx~date +'%F %T'~);

... do something with "$timestamp" ...

If your policy does not permit shelling out; have a look at Graham Barr's Date::Format on cpan:

Date::Format - search.cpan.org

Thank you very much for bright suggestions :slight_smile: