Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Adds timestamp retrieval as Carbon instance from UUID/ULID #47046

Merged
merged 2 commits into from May 12, 2023

Conversation

DarkGhostHunter
Copy link
Contributor

What?

Extracts the timestamp from an UUID (v1, v2, v6, v7) or ULID into a Carbon instance.

use Illuminate\Support\Facades\Date;

// Dates from ULID
$date = Date::createFromUid('01H06ZMA15EBJASEXHWJC1SY5F');

// Dates from UUID
$date = Date::createFromUid('01880dfa-2825-72e4-acbb-b1e4981cf8af');

Returns null the UID has no timestamp, or false if the UID is invalid. This allows to check both cases:

$date = Date::createFromUid($uid);

if (is_null($date)) {
    return 'The UID is invalid';
} elseif (!$date) {
    return 'The UID has no timestamp';
}

return $date;

Why?

It saves the step to detect an incoming UID, transform into a UUID/ULID, and then extracting the date, and then transform it into Carbon, all that inside a try-catch if the UID is invalid or has no timestamp.

use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Date;
use Ramsey\Uuid\Uuid;
use Symfony\Component\Uid\Ulid;

// Before
public function date(Request $request)
{
    $uid = $request->uid;
    
    if (Str::isUlid($uid)) {
        return Date::make(Ulid::fromString($uid)->getDateTime()));
    }
    
    try {
        return Date::make(Uuid::fromString($uid)->getDateTime());
    } catch (\Throwable) {
        return null;
    }
}

}

try {
$date = Uuid::fromString($uid)->getDateTime();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel that instead of having an API that returns null|false etc, this method should return an exception - specific to Carbon in Laravel - maybe InvalidUidException or something, that specifies what happen on the string message. What do you think @DarkGhostHunter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can just return a Carbon instance always, and leave it to the UUID class to throw its own exceptions, one for malformed UUID and the other for not time-based UUID. They're pretty much self explanatory.

This way the dev can know why it failed, at least if it's not passing an ULID.

Better, I could wrap these into an InvalidArgumentException telling the dev he passed confetti.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DarkGhostHunter Let's have @taylorotwell deciding this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay.

Just to note aside, I think the UUID validator could use a min option to check if the UUID validates to a given version, saving an additional check afterwards. Currently only checks if it's an UUID.

@taylorotwell
Copy link
Member

I agree let's just let it throw - updated.

@taylorotwell taylorotwell merged commit 01092bd into laravel:10.x May 12, 2023
16 checks passed
@DarkGhostHunter DarkGhostHunter deleted the feat/uuid-carbon branch June 27, 2023 02:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants