-
Notifications
You must be signed in to change notification settings - Fork 459
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
Changes to Time.zone are not reflected in Chronic parsing #182
Comments
I tried the suggestion at #158 (comment) but it didn't seem to make a difference (though it's not clear to me if any changes were actually made or if @injekt was just suggesting possible changes)
|
Yeah I was suggesting that as a change, it's not yet possible. I'll mark this as a feature. |
Is there a usable workaround currently? All I can see to work this around is the following: def parse_with_tz(value)
previous = Chronic.time_class
begin
Chronic.time_class = Time.zone
return Chronic.parse(value)
ensure
Chronic.time_class = previous
end
end I was expecting this NOT to be thread-safe. So I'm a little puzzled as to whether there's a need in syncronisation (Mutex) or not. |
UPDATE: I've tested it on JRuby and it does indeed break there. So I can confirm that synchronisation is indeed required for the SYNC = Mutex.new
def parse_with_tz(value)
SYNC.synchronize do
previous = Chronic.time_class
begin
Chronic.time_class = Time.zone
return Chronic.parse(value)
ensure
Chronic.time_class = previous
end
end
end |
UPDATE 2: All implementations will require synchronisation. The fact that MRI and Rubinius didn't in my has no conclusion yet. |
Maybe this will help someone:
|
I don't know how that SO answer can help because time_class option is Not
|
It's correct that this is not thread-safe. There is currently no workaround until we build something into Chronic (see the other issues). This shouldn't be so hard now that To clarify and reiterate what @dnagir has said; that SO answer is incorrect. I've commented on it. |
@leejarvis roger that. I'll see if I can send a PR within a few days or so with all that sorted. Unless you have it sorted already of course? |
@dnagir Honestly, I don't. I haven't managed to find time for Chronic for quite a while. As frustrating as it is, I do plan to find some time to work on it (though I am working on a replacement, too). Work is very busy. Please feel free to submit a PR for it. @davispuh is currently managing a lot of the code also so one of us can review. Happy to expand collaborators. Still hoping to move the repo or sort something out so I don't have to bother @mojombo every time. |
I'm still waiting for PRs to be reviewed by @leejarvis some could be merged, but this isn't my lib so I'm waiting ;) |
Do you guys develop Chronic on Ruby 2? I'm given error even when I try to bundle it: $ bundle
There was a LoadError while loading chronic.gemspec:
cannot load such file -- numerizer from
/Users/dnagir/proj/chronic/chronic.gemspec:2:in `<main>'
Does it try to require a relative path? That's been removed in Ruby 1.9. |
it's because gemspec includes whole chronic and thus you get this exception because you don't have |
Well, I just had a closer look at the This whole drama can be avoided easily by using This makes me wonder what's the point of the The solution to the whole issue is as simple as: |
In the meanwhile, this PR would make it clear about the |
Well, I see why now The
So whether we add it not it as an option, or whether we use I'm giving up on this :( |
EDIT: So I think correct behavior would be that when parsing 'Today at 5pm' it would use same Timezone as used for |
Would there be any issue with doing the following in rails?
Thank you |
As @kbaum pointed out in the other issue, module Chronic
def self.time_class
::Time.zone
end
end Then you can safely do this. Time.use_zone('Pacific Time (US & Canada)') do
Chronic.parse("Sunday")
end |
That looks like a good workaround @chewi ,have you been using this in production? |
Unfortunately I've completely forgotten why I looked into this. I don't believe we're using that workaround in production at the moment. |
great @mphalliday, thanks for the feedback. My tests so far looks good. Thanks for sharing @chewi |
Theis will be usefull to eliminate all the possible confusion going on what options are and are not supported by chronic. One such example is: mojombo/chronic#182 (comment) It is a good idea to tell people know about it in advance if they provide invalid or unsupported options.
My rails app sets Time.zone on a user level, using an
around_filter
based on the current user. This causes subtle problems in Chronic parsing, which is why I've (reluctantly) stuck toTime.zone.parse
so far.The only solution I can think of is to
Chronic.time_class
on each request, after changing theTime.zone
. But that makes things harder to test and doesn't feel right. I wonder if there is a better way of doing it?The text was updated successfully, but these errors were encountered: