3

I am trying to convert timestamp 2016-02-11 03:31:18 to pubnub timestamp of 17-digit precision unix time (UTC) something like 13406746780720711 given in reference url given by pubnub I have tried following but no luck

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

Above example gives output 142360085 which is 10 characters where pubnub asks for 17 digit timestamp.

Reason behind doing this is i want to fetch unread messages of particular user and send an email at EOD via email.

After converting mytimestamp to 17-digit precision unix time (UTC) i will pass it to pubnub history function and get unread messages.

Shashank Shah
  • 2,077
  • 4
  • 22
  • 46
  • Possible duplicate of [Convert 17-digit precision unix time (UTC) to date fromat In javascript](http://stackoverflow.com/questions/34487986/convert-17-digit-precision-unix-time-utc-to-date-fromat-in-javascript) – Sam Texas Feb 20 '16 at 20:50
  • In Android https://stackoverflow.com/a/60126795/5788247 – Shomu Feb 08 '20 at 12:34

3 Answers3

5

Convert Unix Timestamp to PubNub Timetoken 17-digit Precision

Easy as pie: timestamp * 10000

Except for PHP. ❌ PHP ❌ does not support this level of integer precision! You can instead use String Concatenation and Coercion.

$tt = $unixtime_milliseconds . "0000";

Your unix time must be represented in millisecond precision, no fractions / no floats.

Try out this example using 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
Stephen Blum
  • 6,498
  • 2
  • 34
  • 46
  • function parseDateTime(s) { var b = s.split(/\D/); return new Date(b[0],b[1]-1,b[2],b[3],b[4],b[5]) } var timestamp = parseDateTime("2015-02-11 02:10:54"); 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); @PubNub can you please check the code?i mean this is correct what i am doing ?i am passing date not date object here. correct me if wrong. – Shashank Shah Feb 14 '16 at 16:47
  • On above comment i am trying to convert date (string) to pubnub timetoken which produce 14236008540000000 - PubNub Timetoken i hope this is correct. – Shashank Shah Feb 14 '16 at 16:49
  • Yes, that is correct. It will not match a message that is in your history for that channel but it will return results before or after that timetoken depending on which parameter you use (start or end). – Craig Conover Feb 16 '16 at 16:56
  • Nope! It's not working for me! :( even i tried using php sdk as well but no luck please review http://stackoverflow.com/questions/35451863/get-unread-messages-on-particular-channel-pubnub-php-sdk hope you'll help. – Shashank Shah Feb 17 '16 at 16:45
  • ❌ **PHP** ❌ does not support this level of integer precision! you may instead want to use String Concatenation and Coercion. **`$tt = $unixtime_milliseconds . "0000";`** You unix time must be millisecond precision. – Stephen Blum Feb 17 '16 at 17:37
  • 1
    This answer unfortunately conflicts with the PubNub history API calls in the sense that you no longer exclude (start) or include (end) a history entry if using a "last read" timetoken that artificially ends in '0000.' – lilbiscuit Dec 18 '17 at 20:20
  • Good point. If your VM doesn't support the required precision then you will need to round to the closest decimal or use string format. – Stephen Blum Dec 29 '17 at 22:35
3

In Android get Current TimeToken

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

    static Long getBeforeHourToken(int hours) {
        return (Calendar.getInstance().getTimeInMillis() - TimeUnit.HOURS.toMillis(hours)) * 10000L;
    }
Shomu
  • 2,734
  • 24
  • 32
3

In iOS how you can get current timestamp. This is written for PubNub.

 static func currentTimeInMilliSeconds()-> CUnsignedLongLong {
           let currentDate = Date()
           let since1970 = currentDate.timeIntervalSince1970
           return CUnsignedLongLong(since1970 * 1000)
       }
Md Jahirul Islam
  • 211
  • 2
  • 10
  • That's incorrect. PubNub converts Timetoken to date with `Int64(self / 10_000_000)`. So in another direction, we need to do like this `Int64(date.timeIntervalSince1970 * 10_000_000)`. – Borut Tomazin Apr 29 '21 at 10:28