-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
\I18n\Time setTimezone does not work as expected #1807
Comments
Any particular concerns, or just a general thumbing your nose at the code? |
What exactly were you trying to fix? All times are converted internally to UTC to handle any conversions, and, IIRC, might have been how the DateTime instance that it's extending handles things internally. It's been a while since I've looked at it. If you print the time out does it give the correct timezone? The tests seem to indicate that it's working as expected. |
With the current version of the code, here's what I get:
and setting $appTimezone to UTC I get:
Both of which act as expected. Is your code up to date? Though we haven't made changes to that in quite a while, I don't think. If it is reasonably up to date then what environment is that running on? OS? Server? PHP version? Can you verify what the system thinks |
Mainly thumbing nose, but I cannot understand why in almost all the cases we are returning new instance instead of working on the one which was passed / using to make operation on it. Not sure about performance.
I didn't get right time when I am printing properties like created_at / updated_at of Entities or when I am using format() method. I think that dates should be casted to appTimezone and imho they didn't. According to:
The original issue was because I've passed string instead of \DateTimeZone instance - that's is because I looked many times in \I18n\Time code and internally it is using strings or \DateTimeZone interchangeably - explanation bellow. ///DO NOT READ THIS PART - BEGIN
/// DO NOT READ THIS PART - END Hovewer I am experiencing another issue which is related to preious one and mentioned it above. Using code like this:
I get natural and expected results - time is converted to AEDT:
When I am trying to do the same with CI and \I18n\Time:
I get:
|
That's not a mess, it's a conscious design choice :) There's a large swath of PHP devs that feel that all objects should be immutable. While I'm not one of those, somewhere during the development of this library it started making sense for it to be an immutable class. Don't remember the specifics. That was awhile ago. I just verified and the first line of the docs does state it is immutable. Because it's immutable and doesn't make changes on the original class, your final example won't work because you're not capturing the returned new Time instance. It would need to be like:
According to the docs passing a string should work just fine. If it's not then that's a bug. I'll check into that. |
I didn't notice it is immutable in docs, but in fact I didn't check for it and only for specific methods explanations. However I think I write my tests in a way as you pointed it out. I will pay more attention to docs. Thank you for wide explanation. According to your hints about how to use it. I've checked it and:
gives me:
so date looks like it is converted between UTC and my "PHP set: Default timezone | Europe/Berlin" option 1,2,3 and there is no change in date for option4. |
Adds possibility to set mutateDate to return \DateTime instead of \CodeIgniter\I18n\Time instances. To make it work simpy use this code in your Entity child constructor: ``` public function __construct(?array $data = null) { parent::__construct($data, ['dateMutateAs' => 'DateTime']); } ``` or ``` public function __construct(?array $data = null) { parent::__construct($data); $this->setMutateDateAs('DateTime'); // will return boolean - value changed === true. } ``` I think it is required to add some code to make timezone changes work for \CodeIgniter\I18n\Time instance. For make this work in predictable way I think we should make some changes in \CodeIgniter\I18n\Time (codeigniter4#1807)
@nowackipawel This is a long & convoluted thread. Has the issue been resolved? Can the issue be closed? |
@nowackipawel, any update on this? |
@jim-parry , @atishamte
|
I think the question, though, is with the 4 examples, are they all working as expected within how the framework is designed (nothing with extending, etc)... |
Most of this thread is opinions about immutable classes, which won't change. I confirmed that "Option 4" above now works as expected (changes the time). Since all other tests are passing this is officially resolved. |
I looked into \Time code and.... it does not look good at all.
Additionally setTimezone does not work as excepted .
https://codeigniter4.github.io/CodeIgniter4/libraries/time.html?highlight=settimezone
gives me:
https://screenshots.firefox.com/QrGBqc9Is0QPTGKo/dev.zdamyto.com
my $appTimezone === 'UTC'
I tried to fix it, but this code looks completely messy.
The text was updated successfully, but these errors were encountered: