Permalink
Browse files

String.to_time documentation along examples.

  • Loading branch information...
1 parent 4fc620c commit bc87712bc6938f445d7441c5709a89aeaa1abb54 @sheerun sheerun committed Sep 24, 2012
Showing with 11 additions and 1 deletion.
  1. +11 −1 activesupport/lib/active_support/core_ext/string/conversions.rb
@@ -2,7 +2,17 @@
require 'active_support/core_ext/time/calculations'
class String
- # Form can be either :utc (default) or :local.
+ # Converts a string to a Time value.
+ # The +form+ can be either :utc or :local (default :utc).
+ #
+ # The time is parsed using Date._parse method.
+ # If +form+ is :local, then time is formatted using Time.zone
+ #
+ # "3-2-2012".to_time # => 2012-02-03 00:00:00 UTC
+ # "12:20".to_time # => ArgumentError: invalid date
+ # "2012-12-13 06:12".to_time # => 2012-12-13 06:12:00 UTC
+ # "2012-12-13T06:12".to_time # => 2012-12-13 06:12:00 UTC
+ # "2012-12-13T06:12".to_time(:local) # => 2012-12-13 06:12:00 +0100
def to_time(form = :utc)
unless blank?
date_values = ::Date._parse(self, false).

1 comment on commit bc87712

klevo commented on bc87712 Oct 1, 2012

What about "12:20".to_time using today's date automatically, instead of an exception. Works for me in terms of the principle of least surprise.

Please sign in to comment.