CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-ulid

A universally-unique, lexicographically-sortable, identifier generator

78

1.34x
Overview
Eval results
Files

time-utilities.mddocs/

Time Utilities

Functions for encoding and decoding timestamps in ULID format, enabling extraction of creation time from any ULID and custom timestamp encoding.

Capabilities

Decode Time

Extracts the timestamp from a ULID string, returning the original millisecond timestamp used during generation.

/**
 * Decode time from a ULID
 * @param id - The ULID string to decode
 * @returns The decoded timestamp in milliseconds since Unix epoch
 * @throws ULIDError if the ULID is malformed or contains invalid characters
 */
function decodeTime(id: ULID): number;

Usage Examples:

import { decodeTime, ulid } from "ulid";

// Generate a ULID and extract its timestamp
const id = ulid();
const timestamp = decodeTime(id);
const createdAt = new Date(timestamp);

console.log(`ULID ${id} was created at ${createdAt.toISOString()}`);

// Decode timestamp from existing ULID
const existingId = "01HNZX8JGFACFA36RBXDHEQN6E";
const originalTime = decodeTime(existingId); // 1469918176385

// Use with specific ULID
try {
  const time = decodeTime("01ARYZ6S41TSV4RRFFQ69G5FAV");
  console.log(new Date(time)); // Original creation date
} catch (error) {
  console.error("Invalid ULID:", error.message);
}

Error Conditions:

  • Throws ULIDError with code DecodeTimeValueMalformed if ULID length is incorrect
  • Throws ULIDError with code DecodeTimeInvalidCharacter if ULID contains invalid Base32 characters
  • Throws ULIDError with code DecodeTimeValueMalformed if decoded timestamp exceeds maximum value

Encode Time

Encodes a timestamp into ULID time format using Crockford Base32 encoding.

/**
 * Encode the time portion of a ULID
 * @param now - The timestamp to encode (milliseconds since Unix epoch)
 * @param len - Length to generate (default: TIME_LEN = 10)
 * @returns The encoded time string
 * @throws ULIDError for invalid timestamp values
 */
function encodeTime(now: number, len?: number): string;

Usage Examples:

import { encodeTime, TIME_LEN } from "ulid";

// Encode current timestamp
const now = Date.now();
const encoded = encodeTime(now); // "01HNZXD07M"

// Encode specific timestamp
const specificTime = 1469918176385;
const timeStr = encodeTime(specificTime); // "01ARYZ6S41"

// Encode with custom length (advanced usage)
const shortTime = encodeTime(now, 8); // Shorter time encoding

// Use with Date objects
const futureDate = new Date("2030-01-01T00:00:00Z");
const futureEncoded = encodeTime(futureDate.getTime());

Error Conditions:

  • Throws ULIDError with code EncodeTimeValueMalformed if timestamp is NaN or not an integer
  • Throws ULIDError with code EncodeTimeNegative if timestamp is negative
  • Throws ULIDError with code EncodeTimeSizeExceeded if timestamp exceeds TIME_MAX (2^48 - 1)

Time Format Details:

  • Time portion is always 10 characters (when using default length)
  • Uses Crockford Base32 alphabet: 0123456789ABCDEFGHJKMNPQRSTVWXYZ
  • Represents milliseconds since Unix epoch (January 1, 1970 UTC)
  • Maximum supported timestamp: 281,474,976,710,655 (approximately year 10,889)

Constants

const TIME_LEN: number; // 10 - Standard length of time portion
const TIME_MAX: number; // 281474976710655 - Maximum timestamp value (2^48 - 1)

Install with Tessl CLI

npx tessl i tessl/npm-ulid

docs

base32-utilities.md

generation.md

index.md

time-utilities.md

uuid-conversion.md

validation.md

tile.json