bjuppa / carbonator

Datetime helpers for timezone handling built on Carbon
3 stars 0 forks source link

Create trait that lets an object have preferred timezone and time format #3

Open bjuppa opened 5 years ago

bjuppa commented 5 years ago

The trait could require the object to implement methods getPreferredTimezone() / getPreferredDateTimeFormat().

From that, methods like parseToDefaultTz($time) can convert user input for UTC-handling, while parseToTz($time), parseToDatetimeLocal($time) & formatInTz($time, $format = null) can parse in default timezone and convert to user's timezone for display and processing.

bjuppa commented 5 years ago

Carbon 2 can set locale for formatting, perhaps getPreferredLocale() would be interesting for this trait too?

Example setting locale in a macro: https://carbon.nesbot.com/docs/#user-settings

bjuppa commented 3 years ago

Actually, I think this could be done using Carbon's factories in methods on the class you want to "own" it's timezone... It should be able to return a Carbon instance that is already prepared with a timezone, locale, and even default string format. Something like this:

public function carbon() {
    return new \Carbon\Factory([
        'locale' => 'fr_FR',
        'timezone' => 'Europe/Paris',
        'toStringFormat' => 'r',
    ]);
}

https://carbon.nesbot.com/docs/#api-localization

Actually... the factory could potentially be a good replacement for this whole package! 😄 Just let every object that has it's own timezone expose some methods for parsing in that timezone (and locale?) to default tz, and printing any input in that timezone! 🤷