Perl 人类可读时间(以毫秒为单位)到 EPOCH

发布于 2024-12-03 20:10:08 字数 341 浏览 1 评论 0原文

我正在解析 SIP 消息,我们需要在人类可读部分(即 2011/09/08 00:10:17.997)中包含毫秒,我们需要使用 Perl 将它们转换为纪元时间,我发现了一堆使用 Human 的示例Epoch可读,但不包括毫秒,这可能吗?我在 CPAN 中找到了 DateTime 但安装时遇到问题。所以寻找其他选择: 示例

$epoch = convert(2011/09/08 00:10:17.997)
print $epoch

和打印:1315183552

I'm parsing SIP messages and we need to include milliseconds in the human readable part (i.e 2011/09/08 00:10:17.997) we need to convert them to Epoch time using Perl, I have found bunch of examples using from Human Readable to Epoch, but not including milliseconds, is this possible? I have found DateTime in CPAN but im having a problem installing it. so looking for other options:
Example

$epoch = convert(2011/09/08 00:10:17.997)
print $epoch

and prints: 1315183552

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方的巷 2024-12-10 20:10:08

修复系统中阻止您安装模块的任何问题。

除此之外,大纪元时间只有第二个分辨率。去掉毫秒并使用 Time::Local 进行转换(您需要解析您的自己发送 SIP 消息)

$timestamp = "2011/09/08 00:10:17.997";
$timestamp =~ /(\d{4})\/(\d{2})\/(\d{2}) (\d{2}):(\d{2}):(\d{2})/;

# ($sec,$min,$hour,$mday,$mon,$year);
$epoch = timelocal($6, $5, $4, $3, $2 - 1, $1 - 1900);

如果确实很重要,请使用毫秒舍入秒。

Fix whatever problem it is with your system that is preventing you from installing a module.

Barring that, Epoch time only has second resolution. Strip off the milliseconds and convert using Time::Local (You'll need to parse your SIP message yourself)

$timestamp = "2011/09/08 00:10:17.997";
$timestamp =~ /(\d{4})\/(\d{2})\/(\d{2}) (\d{2}):(\d{2}):(\d{2})/;

# ($sec,$min,$hour,$mday,$mon,$year);
$epoch = timelocal($6, $5, $4, $3, $2 - 1, $1 - 1900);

If it's really important, round the second using the milliseconds.

深府石板幽径 2024-12-10 20:10:08

如果你在 Linux 下,又快又脏:

$epoch = `date -d "2011/09/08 00:10:17.997" +%s`; print $epoch;

Quick and dirty if you are under linux:

$epoch = `date -d "2011/09/08 00:10:17.997" +%s`; print $epoch;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文