如何测试给定的时间戳是以秒或毫秒为单位? [英] How to test if a given time-stamp is in seconds or milliseconds?

查看:2141
本文介绍了如何测试给定的时间戳是以秒或毫秒为单位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一个给定的变量,它包含一个 UNIX 时间戳,但无论是秒或毫秒格式都是未知的,我想分配一个变量,以秒格式



例如:



  unknown = 1398494489444#这是毫秒
t = ???

更新:我知道不可能告诉没有给出一些限制,所以这里是




  • current_ts - 86400 * 365<未知< current_ts



假设current_ts =当前unix时间戳

解决方案

如果您将最大时间戳值转换为x位数你会得到这样的东西:




  • 9999999999999(13位数)表示Sat Nov 20 2286 17:46:39 UTC

  • 999999999999(12位数)表示日期:2006年9月9日01:46:39 UTC

  • 99999999999(11位数)表示Sat Mar 03 1973 09:46:39 UTC



您的时间戳可以早于2001年吗?如果不是,我觉得你可以安全地检查这个数字是否有13位数或更多 - 如果是,你有毫秒,如果没有你有秒。当然,这只能工作,直到几秒钟内的时间戳也将有13位数,这意味着以毫秒为单位的时间戳将有16位数:




  • 1000000000000000 (16位数字)表示Fri Sep 27 33658 01:46:40但是那时我将生活在阿尔法半人马座系统的星球上,时间标准可能会有所改变:)



PS如果您的时间戳不能超过1973年,您可以将条件放松到12位数以上。条件应该只能一直到:




  • 100000000000000(15位数)表示Wed Nov 16 5138 09:46:40
    因为这在几秒钟内将有12位数,并将与您的条件重叠


Assume a given variable, it is containing a UNIX time-stamp, but whether it is in seconds or milliseconds format is unknown, I want to assign to a variable which is in seconds format

For Example:

unknown = 1398494489444 # This is millisecond
t = ???

Updated: I understand it is not possible to tell without giving some limitations, so here is it

  • current_ts - 86400 * 365 < unknown < current_ts

Assume current_ts = current unix timestamp

解决方案

If you convert the maximum timestamp values with x digits in millis you get something like this:

  • 9999999999999 (13 digits) means Sat Nov 20 2286 17:46:39 UTC
  • 999999999999 (12 digits) means Sun Sep 09 2001 01:46:39 UTC
  • 99999999999 (11 digits) means Sat Mar 03 1973 09:46:39 UTC

Can your timestamps be older than 2001? If not i think you're safe to check whether the number has 13 digits or more - if yes you have milliseconds, if not you have seconds. Of course this will only work until timestamps in seconds will have 13 digits as well, which means timestamps in milliseconds will have 16 digits:

  • 1000000000000000 (16 digits) means Fri Sep 27 33658 01:46:40 but by then i will be living on a planet from the Alpha Centauri system and time standards will probably have changed a bit :)

P.S. you can loosen up the condition to 12 digits or more, if your timestamps can't go back further than 1973. The condition should only be ok up until:

  • 100000000000000 (15 digits) means Wed Nov 16 5138 09:46:40 because this in seconds will have 12 digits and will overlap with your condition

这篇关于如何测试给定的时间戳是以秒或毫秒为单位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆