{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/19618","labels_url":"https://api.github.com/repos/rails/rails/issues/19618/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/19618/comments","events_url":"https://api.github.com/repos/rails/rails/issues/19618/events","html_url":"https://github.com/rails/rails/pull/19618","id":65843881,"number":19618,"title":"Added ActiveSupport::TimeZone#strptime.","user":{"login":"pjungwir","id":660353,"avatar_url":"https://avatars.githubusercontent.com/u/660353?v=3","gravatar_id":"","url":"https://api.github.com/users/pjungwir","html_url":"https://github.com/pjungwir","followers_url":"https://api.github.com/users/pjungwir/followers","following_url":"https://api.github.com/users/pjungwir/following{/other_user}","gists_url":"https://api.github.com/users/pjungwir/gists{/gist_id}","starred_url":"https://api.github.com/users/pjungwir/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pjungwir/subscriptions","organizations_url":"https://api.github.com/users/pjungwir/orgs","repos_url":"https://api.github.com/users/pjungwir/repos","events_url":"https://api.github.com/users/pjungwir/events{/privacy}","received_events_url":"https://api.github.com/users/pjungwir/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rails/rails/labels/activesupport","name":"activesupport","color":"FC9300"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-04-02T05:39:28Z","updated_at":"2015-04-06T21:42:30Z","closed_at":"2015-04-06T21:42:30Z","pull_request":{"url":"https://api.github.com/repos/rails/rails/pulls/19618","html_url":"https://github.com/rails/rails/pull/19618","diff_url":"https://github.com/rails/rails/pull/19618.diff","patch_url":"https://github.com/rails/rails/pull/19618.patch"},"body":"In Rails there is no easy way to parse a user-submitted date/time string,\r\ninterpreting it as from a given time zone (for instance from the user's preference).\r\nThis seems like a natural and common thing to do, and there are several StackOverflow questions about it (with flaky and/or wrong answers):\r\n\r\nhttp://stackoverflow.com/questions/4262550/how-do-i-get-ruby-to-parse-time-as-if-it-were-in-a-different-time-zone\r\n\r\nhttp://stackoverflow.com/questions/6615463/how-can-i-use-ruby-to-parse-a-time-as-though-it-is-in-a-time-zone-i-specify-wit\r\n\r\nhttp://stackoverflow.com/questions/5074238/how-to-get-rails-to-interpret-a-time-as-being-in-a-specific-time-zone\r\n\r\nFirst, some attempted answers. Suppose the user entered the string `2010-02-05 13:00:01` and has a time zone of `Eastern Time (US & Canada)`. This first attempt comes from StackOverflow, and can also be found suggested frequently elsewhere:\r\n\r\n    str = '2010-02-05 13:00:01'\r\n    tz = ActiveSupport::TimeZone['Eastern Time (US & Canada)']\r\n    ActiveSupport::TimeZone['UTC'].parse(str).in_time_zone(tz)\r\n\r\nThis fails because `in_time_zone` does not change the \"instant\" the time represents,\r\nbut the perspective on that instant.\r\nIn other words, the result is not 13:00:01 EST, but 8:00:01 EST\r\n(which on Feb 5 is the same instant as 13:00:01 UTC).\r\n\r\nAnother suggestion is to append a time zone to the user-submitted string before parsing it,\r\ne.g. `str += \" PDT\"`. But this is not great, because:\r\n\r\n- You may not know whether or not the string already has a timezone abbreviation.\r\n\r\n- The correct abbreviation may vary depending on time of year, e.g. PDT vs PST, and that requires first parsing the string.\r\n\r\nOne more suggestion is to say `Time.use_zone(tz) { Time.strptime(str, spec) }`,\r\nbut that is not effective because `use_zone` does not change the behavior of `Time.strptime`.\r\n\r\nA final solution is to use `tz.parse` instead of `strptime`,\r\nbut you may not want a heuristic-based approach\r\nand might need to parse the string according to a given format,\r\nwhich requires `strptime`.\r\n\r\nSo this patch adds a `strptime` method to `TimeZone` objects,\r\nwhich parses the given string as though it comes from the time zone\r\nrepresented by the method receiver.\r\nIn other words `tz.strptime(\"1:00\", \"%H:%M\")` means 1:00 in the timezone `tz`.\r\nIf the arguments contain an explicit timezone, e.g. with `%Z`,\r\nthen that is used instead,\r\nand then the time is converted to the time zone of `tz`.\r\nThis is the same behavior as `TimeZone#parse`."},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/90254020","html_url":"https://github.com/rails/rails/pull/19618#issuecomment-90254020","issue_url":"https://api.github.com/repos/rails/rails/issues/19618","id":90254020,"user":{"login":"sgrif","id":1529387,"avatar_url":"https://avatars.githubusercontent.com/u/1529387?v=3","gravatar_id":"","url":"https://api.github.com/users/sgrif","html_url":"https://github.com/sgrif","followers_url":"https://api.github.com/users/sgrif/followers","following_url":"https://api.github.com/users/sgrif/following{/other_user}","gists_url":"https://api.github.com/users/sgrif/gists{/gist_id}","starred_url":"https://api.github.com/users/sgrif/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sgrif/subscriptions","organizations_url":"https://api.github.com/users/sgrif/orgs","repos_url":"https://api.github.com/users/sgrif/repos","events_url":"https://api.github.com/users/sgrif/events{/privacy}","received_events_url":"https://api.github.com/users/sgrif/received_events","type":"User","site_admin":false},"created_at":"2015-04-06T21:42:30Z","updated_at":"2015-04-06T21:42:30Z","body":"http://api.rubyonrails.org/classes/String.html#method-i-in_time_zone\r\n\r\n```ruby\r\nstr = '2010-02-05 13:00:01'\r\ntz = ActiveSupport::TimeZone['Eastern Time (US & Canada)']\r\nstr.in_time_zone(tz) # => Fri, 05 Feb 2010 13:00:01 EST -05:00\r\n```"}}