Skip to content

Add support for UUIDs before 1970 #133

@fwdekker

Description

@fwdekker

The UUID generators do not support generating UUIDs with a timestamp before 1970. Such UUIDs may be of interest for various reasons, such as (1) users wishing to better understand the UUID format, (2) users who want to test whether their application really supports any valid UUID, or (3) users interested in anachronistic video games.

The following snippet shows that this library does not support UUIDs with timestamps before 1970:

private UUIDTimer createFixedTimer(Long milliTimestamp) throws IOException {
    return new UUIDTimer(
            new Random(),
            null,
            new UUIDClock() {
                @Override
                public long currentTimeMillis() {
                    return milliTimestamp;
                }
            }
    );
}

public void myFunction() throws IOException {
    long milliTimestamp = -219292800000L;  // Corresponds to 1963-01-19 21:20:00

    TimeBasedGenerator generator = Generators.timeBasedGenerator(null, createFixedTimer(milliTimestamp));
    UUID uuid = generator.generate();

    System.out.println(uuid);  // e.g. 138140d9-1dd2-11b2-a49b-43863b6a1ebd, which corresponds to 1970-01-01 00:00:00
}

The cause appears to be inside UUIDTimer's getTimestamp function, which implicitly assumes that timestamps are non-negative:

if (systime <= _lastUsedTimestamp) {
    if (_clockCounter < kClockMultiplier) {
        systime = _lastUsedTimestamp;

Since _lastUsedTimestamp defaults to 0, if systime is negative, systime will always be set to _lastUsedTimestamp.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions