Kit
Functions

assertIsUnixTimestamp

assertIsUnixTimestamp(putativeTimestamp): asserts putativeTimestamp is UnixTimestamp

Timestamp values returned from the RPC API conform to the type UnixTimestamp. You can use a value of that type wherever a timestamp is expected.

Parameters

ParameterType
putativeTimestampbigint

Returns

asserts putativeTimestamp is UnixTimestamp

Example

From time to time you might acquire a number that you expect to be a timestamp, from an untrusted network API or user input. To assert that such an arbitrary number is usable as a Unix timestamp, use this function.

import { assertIsUnixTimestamp } from '@solana/rpc-types';
 
// Imagine having received a value that you presume represents a timestamp.
// At this point we know only that it conforms to the `bigint` type.
try {
    // If this type assertion function doesn't throw, then
    // Typescript will upcast `timestamp` to `UnixTimestamp`.
    assertIsUnixTimestamp(timestamp);
    // At this point, `timestamp` is a `UnixTimestamp`.
    timestamp satisfies UnixTimestamp;
} catch (e) {
    // `timestamp` turned out not to be a valid Unix timestamp
}

On this page