Utility methods

This section lists various helper methods that provide a convenient way to work with PubNub timetokens

Timetoken

A unique identifier for each message that represents the number of 100-nanosecond intervals since January 1, 1970, for example, 16200000000000000.
.

Timetoken to date

The timetokenToInstant() method of the TimetokenUtil class converts a PubNub timetoken (a unique identifier for each message sent and received in a PubNub channel that is a number of 100-nanosecond intervals since January 1, 1970) to an Instant object representing the corresponding date and time.

Use this method when you want to display the timetoken of each message or event in the chat history in a human-readable format.

Method signature

TimetokenUtil.timetokenToInstant(timetoken: Long): Instant

Input

ParameterTypeRequiredDefaultDescription
timetokenLongYesn/aRepresents the PubNub timetoken to convert into a human-readable date format.

Output

TypeDescription
InstantThe human-readable date representation of the timetoken.

Basic usage

Convert a timetoken value of 17276954606232118 to a human-readable date and time format.

val timetoken: Long = 17276954606232118
val instant: Instant = TimetokenUtil.timetokenToInstant(timetoken)
val localDateTimeInUTC = instant.toLocalDateTime(TimeZone.UTC)

println("PubNub timetoken: ${timetoken}")
println("Current date: ${localDateTimeInUTC.date}")
println("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

The instantToTimetoken() method of the TimetokenUtil class converts the Instant object representing the corresponding date and time into a PubNub timetoken (a unique identifier for each message sent and received in a PubNub channel that is a number of 100-nanosecond intervals since January 1, 1970).

Use this method to represent a specific date and time as a PubNub timetoken value, for example, to retrieve messages from a PubNub channel at a particular date and time.

Method signature

TimetokenUtil.instantToTimetoken(instant: Instant): Long

Input

ParameterTypeRequiredDefaultDescription
instantInstantYesn/aRepresents the date and time to convert into a PubNub timetoken.

Output

TypeDescription
LongConverted timetoken value.

Basic usage

Convert a human-readable date and time, September 30, 2024 12:12:24 GMT, to a timetoken.

val localDateTime = LocalDateTime(year = 2024, monthNumber = 9, dayOfMonth = 30, hour = 12, minute = 12, second =  44, nanosecond =  123456789)
val zone = TimeZone.currentSystemDefault()
val instant = localDateTime.toInstant(zone)
val timetoken = TimetokenUtil.instantToTimetoken(instant)

println("Current date: ${localDateTime.date}")
println("Current time: ${localDateTime.time}")
println("PubNub timetoken: ${timetoken}")
Current date: 2024-09-30
Current time: 12:12:44.123456789
PubNub timetoken: 17276911641234567

Unix timestamp to timetoken

The unixToTimetoken() method of the TimetokenUtil class converts a Unix timestamp (a number of seconds since January 1, 1970) to a PubNub timetoken (a unique identifier for each message sent and received in a PubNub channel that is a number of 100-nanosecond intervals since January 1, 1970).

Use this method when you need a timetoken to retrieve historical messages with a specific timestamp range from Message Persistence.

Method signature

This method takes the following parameters:

TimetokenUtil.unixToTimetoken(unixTime: Long): Long 

Input

ParameterTypeRequiredDefaultDescription
unixTimeLongYesn/aRepresents the Unix timestamp to convert into a PubNub timetoken.

Output

TypeDescription
LongConverted timetoken value.

Basic usage

Convert a Unix timestamp value of 1727866935316 representing 2024-10-02 11:02:15.316 to PubNub timetoken:

val unixTime = 1727866935316
val timetoken: Long = TimetokenUtil.unixToTimetoken(unixTime)
val instant: Instant = TimetokenUtil.timetokenToInstant(timetoken)
val localDateTime = instant.toLocalDateTime(TimeZone.UTC)

println("Current date: ${localDateTime.date}")
println("Current time: ${localDateTime.time}")
println("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

The timetokenToUnix() method of the TimetokenUtil class converts a PubNub timetoken (a unique identifier for each message sent and received in a PubNub channel that is a number of 100-nanosecond intervals since January 1, 1970) to a Unix timestamp (a number of seconds since January 1, 1970).

Use this method to convert PubNub timetoken for use in another context or system that requires a Unix timestamp.

Method signature

TimetokenUtil.timetokenToUnix(timetoken: Long): Long 

Input

ParameterTypeRequiredDefaultDescription
timetokenLongYesn/aRepresents the PubNub timetoken to convert into a Unix timestamp.

Output

TypeDescription
LongConverted Unix timestamp value.

Basic usage

Convert a PubNub timetoken 17276954606232118 representing 2024-09-30 11:24:20.623211800 to Unix time:

val timetoken = 17276954606232118
val unixTime = TimetokenUtil.timetokenToUnix(timetoken)
val instant = Instant.fromEpochMilliseconds(unixTime)
val localDateTime = instant.toLocalDateTime(TimeZone.UTC)

println("Current date: ${localDateTime.date}")
println("Current time: ${localDateTime.time}")
println("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
Last updated on