Open bjuppa opened 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
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! 🤷
The trait could require the object to implement methods
getPreferredTimezone()
/getPreferredDateTimeFormat()
.From that, methods like
parseToDefaultTz($time)
can convert user input forUTC
-handling, whileparseToTz($time)
,parseToDatetimeLocal($time)
&formatInTz($time, $format = null)
can parse in default timezone and convert to user's timezone for display and processing.