0

We have 2 environments: old (CentOS 6.9) and new (Debian 10).

Also we have this script:

#!/usr/bin/perl
use Time::ParseDate;
$seconds = parsedate('Jan 1, 1970');
print "$seconds\n";

It produces the following on old environment:

[user@old ~]$ ./test.pl
1577829600

And, It produces the following on new environment:

user@new:~$ ./test.pl
-10800

How do we fix this so that test.pl will output the same as on the old environment? Also please share some link(s) to read about this difference.

User9102d82
  • 1,172
  • 9
  • 19
real_sm
  • 35
  • 6
  • 1
    The old environment is quite clearly wrong, as that is the amount of seconds from the Unix epoch at Jan 1st, 2020 (in your time zone). – Grinnz Feb 03 '20 at 23:37
  • Shouldn't both systems return 0? Since parsedate should return the number of seconds since the Unix epoch, which is Jan 1, 1970? That's what I get on my system. – Rob Sweet Feb 04 '20 at 00:18
  • 1
    @Rob Sweet, Depends on whether `parsedate` accepts a local timestamp or not. If it expects a UTC timestamps, then yes, `parsedate('Jan 1, 1970')` should return `0`. But that's not necessarily the case. By default, `parsedate` accepts a timestamp in the time zone specified by the `TZ` env var. `parsedate('Jan 1, 1970')` should return `-5*60*60` for me in Toronto, since that represents `1970-01-01T00:00:00-05:00` aka `1969-12-31T19:00:00Z`. That is `5*60*60` seconds before the unix epoch, which is to say `-5*60*60` seconds past the unix epoch. – ikegami Feb 04 '20 at 00:34
  • Are you sure you didn't do `parsedate('Jan 1, 2020');` on the old system? – ikegami Feb 04 '20 at 00:41

1 Answers1

2

1577829600 is the number of seconds between 2019-12-31T22:00:00Z and the unix epoch (1970-01-01T00:00:00Z).

If you're trying to get the number of seconds since the unix epoch, you can use the builtin time.

$ perl -e'CORE::say time'
1580776856

On the other hand, if you're trying to get the epoch time for Jan 1st of the current year of your local time zone, you can use parsedate('Jan 1').

ikegami
  • 367,544
  • 15
  • 269
  • 518