Utility methods
Helper methods for working with PubNub timetokens.
Timetoken to date
Convert a PubNub timetoken (100-nanosecond intervals since January 1, 1970) to an Instant object for displaying message timestamps in human-readable format.
Method signature
1TimetokenUtil.timetokenToInstant(timetoken: Long): Instant
Input
| Parameter | Description |
|---|---|
timetoken *Type: LongDefault: n/a | Represents the PubNub timetoken to convert into a human-readable date format. |
Output
| Type | Description |
|---|---|
Instant | The human-readable date representation of the timetoken. |
Sample code
Convert a timetoken value of 17276954606232118 to a human-readable date and time format.
1val timetoken: Long = 17276954606232118
2val instant: Instant = TimetokenUtil.timetokenToInstant(timetoken)
3val localDateTimeInUTC = instant.toLocalDateTime(TimeZone.UTC)
4
5println("PubNub timetoken: ${timetoken}")
6println("Current date: ${localDateTimeInUTC.date}")
7println("Current time: ${localDateTimeInUTC.time}")
The output of the method is as follows:
PubNub timetoken: 17276954606232118
Current time: 11:24:20.623211800
Current date: 2024-09-30
Date to timetoken
Convert an Instant object to a PubNub timetoken for retrieving messages at a specific date and time.
Method signature
1TimetokenUtil.instantToTimetoken(instant: Instant): Long
Input
| Parameter | Description |
|---|---|
instant *Type: InstantDefault: n/a | Represents the date and time to convert into a PubNub timetoken. |
Output
| Type | Description |
|---|---|
Long | Converted timetoken value. |
Sample code
Convert a human-readable date and time, September 30, 2024 12:12:24 GMT, to a timetoken.
1val localDateTime = LocalDateTime(year = 2024, monthNumber = 9, dayOfMonth = 30, hour = 12, minute = 12, second = 44, nanosecond = 123456789)
2val zone = TimeZone.currentSystemDefault()
3val instant = localDateTime.toInstant(zone)
4val timetoken = TimetokenUtil.instantToTimetoken(instant)
5
6println("Current date: ${localDateTime.date}")
7println("Current time: ${localDateTime.time}")
8println("PubNub timetoken: ${timetoken}")
Current date: 2024-09-30
Current time: 12:12:44.123456789
PubNub timetoken: 17276911641234567
Unix timestamp to timetoken
Convert a Unix timestamp (seconds since January 1, 1970) to a PubNub timetoken for retrieving historical messages within a specific time range.
Method signature
This method takes the following parameters:
1TimetokenUtil.unixToTimetoken(unixTime: Long): Long
Input
| Parameter | Description |
|---|---|
unixTime *Type: LongDefault: n/a | Represents the Unix timestamp to convert into a PubNub timetoken. |
Output
| Type | Description |
|---|---|
Long | Converted timetoken value. |
Sample code
Convert a Unix timestamp value of 1727866935316 representing 2024-10-02 11:02:15.316 to PubNub timetoken:
1val unixTime = 1727866935316
2val timetoken: Long = TimetokenUtil.unixToTimetoken(unixTime)
3val instant: Instant = TimetokenUtil.timetokenToInstant(timetoken)
4val localDateTime = instant.toLocalDateTime(TimeZone.UTC)
5
6println("Current date: ${localDateTime.date}")
7println("Current time: ${localDateTime.time}")
8println("PubNub timetoken: ${timetoken}")
The output of the method is as follows:
Current date: 2024-10-02
Current time: 11:02:15.316
PubNub timetoken: 17278669353160000
Timetoken to Unix timestamp
Convert a PubNub timetoken to a Unix timestamp for use with systems requiring Unix time format.
Method signature
1TimetokenUtil.timetokenToUnix(timetoken: Long): Long
Input
| Parameter | Description |
|---|---|
timetoken *Type: LongDefault: n/a | Represents the PubNub timetoken to convert into a Unix timestamp. |
Output
| Type | Description |
|---|---|
Long | Converted Unix timestamp value. |
Sample code
Convert a PubNub timetoken 17276954606232118 representing 2024-09-30 11:24:20.623211800 to Unix time:
1val timetoken = 17276954606232118
2val unixTime = TimetokenUtil.timetokenToUnix(timetoken)
3val instant = Instant.fromEpochMilliseconds(unixTime)
4val localDateTime = instant.toLocalDateTime(TimeZone.UTC)
5
6println("Current date: ${localDateTime.date}")
7println("Current time: ${localDateTime.time}")
8println("PubNub timetoken: ${timetoken}")
The output of the method is as follows:
Current date: 2024-09-30
Current time: 11:24:20.623
PubNub timetoken: 17276954606232118