将时间戳转换为17位精度unix时间(UTC)的pubnub时间戳

时间:2016-02-12 09:28:24

标签: pubnub

我正在尝试将时间戳2016-02-11 03:31:18转换为17位精度unix时间(UTC)的pubnub时间戳,如pubnub给出的参考URL中给出的13406746780720711 我试过以下但没有运气

function parseDateTime(s) {
  var b = s.split(/\D/);
  return new Date(b[0],b[1]-1,b[2],b[3],b[4],b[5])
}
date = new Date(parseDateTime("2015-02-11 02:10:54") / 10000);
console.log(date.getTime());//142360085

上面的示例给出了输出142360085,即10个字符,其中pubnub要求提供17位数的时间戳。

这样做的原因是我想要获取特定用户的未读消息,并通过电子邮件在EOD发送电子邮件。

将mytimestamp转换为17位精确的unix时间(UTC)后,我会将其传递给pubnub history function并获取未读消息。

3 个答案:

答案 0 :(得分:3)

将Unix时间戳转换为PubNub Timetoken 17位精度

简单方便: timestamp * 10000

除了PHP。 ❌ PHP ❌不支持此级别的整数精度!您可以改为使用String Concatenation和Coercion。

$tt = $unixtime_milliseconds . "0000";
  

你的unix时间必须以毫秒精度表示,没有分数/没有浮点数

使用JavaScript尝试此示例。

// Vars
var timestamp   = +new Date;
var unix_time   = document.getElementById("unix-timestamp");
var pubnub_time = document.getElementById("pubnub-timetoken");

// Conversion
function unix_to_pubnub(time) {
    return time * 10000;
}

// Update Time
unix_time.innerHTML   = timestamp;
pubnub_time.innerHTML = unix_to_pubnub(timestamp);
<span id="unix-timestamp"></span> - Unix Timestamp <br>
<span id="pubnub-timetoken"></span> - PubNub Timetoken

答案 1 :(得分:1)

在Android中获取当前时间令牌

  static Long getCurrentHourToken() {
        return (Calendar.getInstance().getTimeInMillis()) * 10000L;
    }

    static Long getBeforeHourToken(int hours) {
        return (Calendar.getInstance().getTimeInMillis() - TimeUnit.HOURS.toMillis(hours)) * 10000L;
    }

答案 2 :(得分:1)

在iOS中,如何获取当前时间戳。这是为PubNub编写的。

 static func currentTimeInMilliSeconds()-> CUnsignedLongLong {
           let currentDate = Date()
           let since1970 = currentDate.timeIntervalSince1970
           return CUnsignedLongLong(since1970 * 1000)
       }