Skip to main content

Handling timestamps

By default, the collaboration system uses Unix timestamps (in ms) internally.

However, this date format will not fit in with every database, and therefore timestamps need to be transformed into the correct format.

Transforming timestamps#

There are two moments where timestamps need to be transformed - reading from a database and writing to the database.

When writing to your database, we need to convert to {your format} from Unix.

When reading from your database, we need to convert from {your format} to Unix.

Writing timestamps#

Writing the current timestamp#

Timestamps such as 'createdAt', 'updatedAt', etc (current time) are set to whatever is passed into the CollabServer's getNow option. By default, this function returns CURRENT_TIMESTAMP which works with most SQL databases.

See the server timestamp guide for more information.

Writing past timestamps#

To transform timestamps in the past, you can provide a getDatabaseTimestamp option. This function accepts a unix timestamp and must return a timestamp in your desired format.

note

This function is only used to transform a known value to your required format. For setting current timestamps, see current timestamps

import SQLResolverGenerator from '@pdftron/collab-sql-resolver-generator';
const resolvers = SQLResolverGenerator({
getDatabaseTimestamp: (unixTimestamp) => {
// If your database uses ISO strings
return new Date(unixTimestamp).toISOString();
}
...otherOptions
})

Reading timestamps#

The Collab server uses Unix timestamps internally, meaning that your database's timestamps must be converted into this format.

To convert these timestamps, you can provide a parseToUnixTimestamp option. This function accepts the timestamp from your database, and must return a unix timestamp in MS.

import SQLResolverGenerator from '@pdftron/collab-sql-resolver-generator';
const resolvers = SQLResolverGenerator({
parseToUnixTimestamp: (yourDBTimestamp) => {
return new Date(yourDBTimestamp).getTime()
}
...otherOptions
})

We will try to convert timestamps as best as possible, but in many cases you will have to provide this function.