javascript - Is Perl DateTime epoch in milliseconds or seconds? - Stack Overflow

I need to pare the timestamp given by my backend (which is in Perl) and the timestamp of by front-end (

I need to pare the timestamp given by my backend (which is in Perl) and the timestamp of by front-end (which is in JS), so I need to be sure that they both use the same time unit.

For JS it's easy, quoting:

A number representing the milliseconds elapsed between 1 January 1970 00:00:00 UTC and the given date.

For Perl, the documentation about $dt->epoch() says:

Return the UTC epoch value for the datetime object. Datetimes before the start of the epoch will be returned as a negative number.

The return value from this method is always an integer.

Since the epoch does not account for leap seconds, the epoch time for 1972-12-31T23:59:60 (UTC) is exactly the same as that for 1973-01-01T00:00:00.

To me, it's not clear if the returned integer is in milliseconds or seconds (in the second case I would need to convert JS epoch in seconds or viceversa).

Which one is correct?

I need to pare the timestamp given by my backend (which is in Perl) and the timestamp of by front-end (which is in JS), so I need to be sure that they both use the same time unit.

For JS it's easy, quoting:

A number representing the milliseconds elapsed between 1 January 1970 00:00:00 UTC and the given date.

For Perl, the documentation about $dt->epoch() says:

Return the UTC epoch value for the datetime object. Datetimes before the start of the epoch will be returned as a negative number.

The return value from this method is always an integer.

Since the epoch does not account for leap seconds, the epoch time for 1972-12-31T23:59:60 (UTC) is exactly the same as that for 1973-01-01T00:00:00.

To me, it's not clear if the returned integer is in milliseconds or seconds (in the second case I would need to convert JS epoch in seconds or viceversa).

Which one is correct?

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Sep 20, 2018 at 14:37 justHelloWorldjustHelloWorld 6,84014 gold badges72 silver badges154 bronze badges 1
  • 2 Can't you just make the same date in both languages and pare the timestamps? – Pointy Commented Sep 20, 2018 at 14:39
Add a ment  | 

2 Answers 2

Reset to default 10

In the context of DateTime, "epoch" refers to Unix time, which is the number of seconds that aren't leap seconds since 1970-01-01T00:00:00Z.

$ perl -MDateTime -e'CORE::say DateTime->from_epoch( epoch => 1 )'
1970-01-01T00:00:01

$ perl -MDateTime -e'CORE::say DateTime->from_epoch( epoch => 1000 )'
1970-01-01T00:16:40

However, DateTime supports times with a resolution of nanosecond, so you could use the following to get a JavaScript timestamp.

my $js_time = $dt->epoch * 1000 + $dt->millisecond;

Of course, getting a value other than zero for the millisecond ponent assumes the DateTime object was created with a sufficiently precise time. That's not the case for

my $dt = DateTime->now();

because it uses time operator to obtain the current time. To get a higher-resolution timestamp, you could use the following:

use Time::HiRes qw( time );

my $dt = DateTime->from_epoch( epoch => time() );
$ perl -MDateTime -wE 'say DateTime->new( year => 1971 )->epoch'
31536000
$ bc <<< 365*24*60*60
31536000

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743798352a4508823.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>