On this page

Utility methods

Helper methods for working with PubNub timetokens.

Timetoken to date

Convert a PubNub timetoken (100-nanosecond intervals since January 1, 1970) to a Date object for displaying message timestamps in human-readable format.

Method signature

1static TimetokenUtils.timetokenToDate(timetoken: string | number): Date

Input

* required
ParameterDescription
timetoken *
Type: string or number
Default:
n/a
Represents the PubNub timetoken to convert into a Date object.

Output

TypeDescription
number
Converted Date object.

Sample code

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

1// import the "TimetokenUtils" class from a module
2import { TimetokenUtils } from './TimetokenUtils'
3
4// declare a variable 'timetoken' and assign it a value of a PubNub timetoken
5const timetoken = 16200000000000000
6
7// declare a "date" variable and assign it a "Date" object representing the corresponding date and time by calling the "timetokenToDate()" method of the "TimetokenUtils" class, passing the timetoken as a parameter
8const date = TimetokenUtils.timetokenToDate(timetoken)
9
10 // log the values of the timetoken and date to the console using the "toDateString" and "toLocaleTimeString" methods to format the date in a human-readable date and time format
11console.log(`Timetoken: ${timetoken}`)
12console.log(`Date: ${date.toDateString()} ${date.toLocaleTimeString()}`)
13

The output of the method is as follows:

Timetoken: 16200000000000000
Date: Mon May 03 2021 8:26:40 PM

Date to timetoken

Convert a Date object to a PubNub timetoken for retrieving messages at a specific date and time.

Method signature

1static TimetokenUtils.dateToTimetoken(date: Date): number

Input

* required
ParameterDescription
date *
Type: Date
Default:
n/a
Represents the date and time to convert into a PubNub timetoken.

Output

TypeDescription
number
Converted timetoken value.

Errors

If the passed value is not a valid Date object, you'll get the exception The value passed as date is not an instance of Date. If the passed Date object is valid, the method will return a number value representing the corresponding PubNub timetoken for that date and time.

Sample code

Convert a human-readable date and time, May 3, 2021 00:00:00 GMT, to a timetoken.

1// import the "TimetokenUtils" class from a module
2import { TimetokenUtils } from './TimetokenUtils'
3
4// declare a "Date" object representing a specific date and time
5const date = new Date('May 3, 2021 00:00:00 GMT')
6
7// convert the "Date" object to a PubNub timetoken using the "dateToTimetoken()" method of the "TimetokenUtils" class
8const timetoken = TimetokenUtils.dateToTimetoken(date)
9
10// log the values of the date and timetoken to the console
11console.log(`Date: ${date.toDateString()} ${date.toLocaleTimeString()} GMT`)
12console.log(`Timetoken: ${timetoken}`)
Date: Mon May 03 2021 12:00:00 GMT+0000 (GMT)
Timetoken: 16200000000000000

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

1static TimetokenUtils.unixToTimetoken(unixTime: string | number): number

Input

* required
ParameterDescription
unixTime *
Type: string or number
Default:
n/a
Represents the Unix timestamp to convert into a PubNub timetoken.

Output

TypeDescription
number
Converted timetoken value.

Errors

If the passed value is not a number (NaN()), you'll get the exception The value passed as unixTime is NaN. If the number is valid, the unixToTimetoken() method multiplies it by 10000 and returns the timetoken value.

Sample code

Convert a Unix timestamp value of 1620000000 (representing May 3, 2021 at 12:00:00 AM GMT) to a timetoken.

1// import the "TimetokenUtils" class from a module
2import { TimetokenUtils } from './TimetokenUtils'
3
4// declare the "unixTime" variable and assign it a value of 1620000000
5const unixTime = 1620000000
6
7// call the "unixToTimetoken()" method of the "TimetokenUtils" class, passing in the "unixTime" value as a parameter
8// this method converts the Unix timestamp value to a PubNub timetoken and returns it
9const timetoken = TimetokenUtils.unixToTimetoken(unixTime)
10
11// log the original Unix timestamp value and the resulting timetoken value to the console
12console.log(`Unix time: ${unixTime}`)
13console.log(`Timetoken: ${timetoken}`)

The output of the method is as follows:

Unix time: 1620000000
Timetoken: 16200000000000000

Timetoken to Unix timestamp

Convert a PubNub timetoken to a Unix timestamp for use with systems requiring Unix time format.

Method signature

1static TimetokenUtils.timetokenToUnix(timetoken: string | number): number

Input

* required
ParameterDescription
timetoken *
Type: string or number
Default:
n/a
Represents the PubNub timetoken to convert into a Unix timestamp.

Output

TypeDescription
number
Converted Unix timestamp value.

Errors

If the passed value is not a number (NaN()), you'll get the exception The value passed as timetoken is NaN. If the number is valid, the timetokenToUnix() method divides the timetoken value by 10000 to obtain the Unix timestamp value.

Sample code

Convert a timetoken value of 16200000000000000 to a Unix timestamp.

1// import the "TimetokenUtils" class from a module
2import { TimetokenUtils } from './TimetokenUtils',
3
4// declare a "timetoken" variable and assign it a value of a PubNub timetoken
5const timetoken = 16200000000000000,
6
7// declare a "unixTime" variable and assign it a Unix timestamp value by calling the "timetokenToUnix()" method of the "TimetokenUtils" class, passing the timetoken as a parameter
8const unixTime = TimetokenUtils.timetokenToUnix(timetoken),
9
10// log the value of "timetoken" and the resulting "unixTime" to the console using the date/time format that is commonly used in computer systems and programming languages
11console.log(`Timetoken: ${timetoken}`),
12console.log(`Unix time: ${unixTime}`),

The output of the method is as follows:

Timetoken: 16200000000000000
Unix time: 1620000000

Decrypt messages/files

Manually decrypt a message or file that failed automatic decryption by the crypto module. Requires the original cipher key used for encryption.

Method signature

1static CryptoUtils.decrypt({ chat, message, decryptor, }: {
2 chat: Chat;
3 message: Message;
4 decryptor: (encryptedContent: string) => TextMessageContent;
5}): Message

Input

* required
ParameterDescription
chat *
Type: Chat
Default:
n/a
Instance of the Chat SDK.
message *
Type: Message
Default:
n/a
Instance of the unsuccessfully decrypted message, which contains the decryption error under the error field.
decryptor *
Type: (encryptedContent: string) => TextMessageContent
Default:
n/a
Function that takes an encrypted content as a string and returns the decrypted content as a TextMessageContent.

Output

TypeDescription
Message
Returned Message object with decrypted content and the error field on the Message object set to undefined (no decryption error).

Sample code

Manually decrypt a message with the cipher key enigma.

1// assuming the CryptoUtils class and its decrypt method are defined
2
3// sample message to be decrypted
4const encryptedChat = /* ... */; // provide the encrypted content here
5
6// decrypt the message with the cipher key "enigma"
7const decryptedMessage = CryptoUtils.decrypt({
8 chat: /* ... */, // provide the chat context
9 message: /* ... */, // provide the message to be decrypted
10 decryptor: (encryptedContent) => {
11 // assuming "enigma" is the cipher key for decryption
12 const cryptoModule = CryptoModule.aesCbcCryptoModule({
13 cipherKey: "enigma",
14 });
15
show all 26 lines
Last updated on