7stud2
(7stud --)
2 July 2013 18:23
1
require 'date'
require 'time'
text_t = "1:40 PM ET"
dt = DateTime.parse(text_t) # => #<DateTime: 2013-07-02T13:40:00+00:00
((2456476j,49200s,0n),+0s,2299161j)>
dt.send(:minute) # => 40
dt.minute # => 40
Any way to add say 2 hours,5 minutes and 2 seconds to the `dt` object.
···
--
Posted via http://www.ruby-forum.com/ .
No, you cannot modify Date/DateTime objects. You must create a new one with the values you want: Class: Date (Ruby 2.0.0)
-Justin
···
On 07/02/2013 11:23 AM, Love U Ruby wrote:
require 'date'
require 'time'
text_t = "1:40 PM ET"
dt = DateTime.parse(text_t) # => #<DateTime: 2013-07-02T13:40:00+00:00
((2456476j,49200s,0n),+0s,2299161j)>
dt.send(:minute) # => 40
dt.minute # => 40
Any way to add say 2 hours,5 minutes and 2 seconds to the `dt` object.
7stud2
(7stud --)
2 July 2013 19:41
3
finally I came up with as below :
require 'date'
text_t = '2:12:03 PM ET'
dt = DateTime.parse(text_t, '%I:%M %p %Z')
# => #<DateTime: 2013-07-03T14:12:03+00:00
((2456477j,51123s,0n),+0s,2299161j)>
dt.to_s
# => "2013-07-03T14:12:03+00:00" # !> invalid offset is ignored
dh = Date._strptime(dt.to_s,'%Y-%m-%dT%H:%M:%S%z')
# => {:year=>2013,
# :mon=>7,
# :mday=>3,
# :hour=>14,
# :min=>12,
# :sec=>3,
# :zone=>"+00:00",
# :offset=>0}
dh[:hour] += 30 # => 44
dh
# => {:year=>2013,
# :mon=>7,
# :mday=>3,
# :hour=>44,
# :min=>12,
# :sec=>3,
# :zone=>"+00:00",
# :offset=>0}
dh.values[0..-2].join(" ")
# => "2013 7 3 44 12 3 +00:00"
DateTime.ordinal(*dh.values[0..-3])
# => #<DateTime: 2013-01-07T03:44:12+00:00
((2456300j,13452s,0n),+0s,2299161j)>
Please suggest me if any wrong calculation has been made by me? Please
Thanks
···
--
Posted via http://www.ruby-forum.com/ .
7stud2
(7stud --)
2 July 2013 19:24
4
Justin Collins wrote in post #1114234:
No, you cannot modify Date/DateTime objects. You must create a new one
with the values you want:
Class: Date (Ruby 2.0.0)
I reached till this point:
require 'date'
text_t = '2:12:03 PM ET'
dt = DateTime.parse(text_t, '%I:%M %p %Z')
# => #<DateTime: 2013-07-03T14:12:03+00:00
((2456477j,51123s,0n),+0s,2299161j)>
dt.to_s
# => "2013-07-03T14:12:03+00:00"
Date._strptime(dt.to_s,'%Y-%m-%dT%H:%M:%S%z')
# => {:year=>2013,
# :mon=>7,
# :mday=>3,
# :hour=>14,
# :min=>12,
# :sec=>3,
# :zone=>"+00:00",
# :offset=>0}
Any way to convert this Hash object back to Date object? If that can be
done,I think problem will be solved.
···
On 07/02/2013 11:23 AM, Love U Ruby wrote:
--
Posted via http://www.ruby-forum.com/\ .
It does seem odd that 30 hours after 2013-07-03T14:12:03+00:00 is 2013-01-07T03:44:12+00:00, and you do seem to be doing a lot of work to add 30 hours - looking a the code there is a lot of "noise".
If I look at the documentation for DateTime then I see I can use + to add a fractional number of days, Class: Date (Ruby 2.0.0) , so:
ratdog:tmp mike$ pry
[1] pry(main)> require 'date'
=> false
[2] pry(main)> dt = DateTime.parse('2:12:03 PM ET', '%I:%M %p %Z')
=> #<DateTime: 2013-07-02T14:12:03+00:00 ((2456476j,51123s,0n),+0s,2299161j)>
[3] pry(main)> dt + (30.0 / 24) # days
=> #<DateTime: 2013-07-03T20:12:03+00:00 ((2456477j,72723s,0n),+0s,2299161j)>
Hope this helps,
Mike
···
On 2013-07-02, at 3:41 PM, Love U Ruby <lists@ruby-forum.com> wrote:
finally I came up with as below :
require 'date'
text_t = '2:12:03 PM ET'
dt = DateTime.parse(text_t, '%I:%M %p %Z')
# => #<DateTime: 2013-07-03T14:12:03+00:00
((2456477j,51123s,0n),+0s,2299161j)>
dt.to_s
# => "2013-07-03T14:12:03+00:00" # !> invalid offset is ignored
dh = Date._strptime(dt.to_s,'%Y-%m-%dT%H:%M:%S%z')
# => {:year=>2013,
# :mon=>7,
# :mday=>3,
# :hour=>14,
# :min=>12,
# :sec=>3,
# :zone=>"+00:00",
# :offset=>0}
dh[:hour] += 30 # => 44
dh
# => {:year=>2013,
# :mon=>7,
# :mday=>3,
# :hour=>44,
# :min=>12,
# :sec=>3,
# :zone=>"+00:00",
# :offset=>0}
dh.values[0..-2].join(" ")
# => "2013 7 3 44 12 3 +00:00"
DateTime.ordinal(*dh.values[0..-3])
# => #<DateTime: 2013-01-07T03:44:12+00:00
((2456300j,13452s,0n),+0s,2299161j)>
Please suggest me if any wrong calculation has been made by me? Please
Thanks
--
Posted via http://www.ruby-forum.com/\ .
--
Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/
The "`Stok' disclaimers" apply.
Both approaches make me nervous. The first because your parsing a date where the time is 44:00 (clearly an invalid date), which at best I believe is undocumented behavior, and more likely is exploiting a bug that may be fixed at a later date.
The is Mike's is probably fine, but makes me worry about rounding errors for some values. Instead I'd do the math using Time, which uses seconds instead of fractions of days:
irb(main):006:0> now = DateTime.now.to_time
=> 2013-07-02 22:09:19 -0600
irb(main):007:0> hours = 2
=> 2
irb(main):008:0> minutes = 5
=> 5
irb(main):010:0> now + (hours * 60 * 60) + minutes * 60
=> 2013-07-03 00:14:19 -0600
Ideally the Stdlib would have a TimeSpan class which could be added and subtracted from DateTime, or be the result of subtracting 2 DateTimes. Would be a good idea for a gem.
···
On 7/2/2013 4:33 PM, Mike Stok wrote:
On 2013-07-02, at 3:41 PM, Love U Ruby <lists@ruby-forum.com> wrote:
finally I came up with as below :
require 'date'
text_t = '2:12:03 PM ET'
dt = DateTime.parse(text_t, '%I:%M %p %Z')
# => #<DateTime: 2013-07-03T14:12:03+00:00
((2456477j,51123s,0n),+0s,2299161j)>
dt.to_s
# => "2013-07-03T14:12:03+00:00" # !> invalid offset is ignored
dh = Date._strptime(dt.to_s,'%Y-%m-%dT%H:%M:%S%z')
# => {:year=>2013,
# :mon=>7,
# :mday=>3,
# :hour=>14,
# :min=>12,
# :sec=>3,
# :zone=>"+00:00",
# :offset=>0}
dh[:hour] += 30 # => 44
dh
# => {:year=>2013,
# :mon=>7,
# :mday=>3,
# :hour=>44,
# :min=>12,
# :sec=>3,
# :zone=>"+00:00",
# :offset=>0}
dh.values[0..-2].join(" ")
# => "2013 7 3 44 12 3 +00:00"
DateTime.ordinal(*dh.values[0..-3])
# => #<DateTime: 2013-01-07T03:44:12+00:00
((2456300j,13452s,0n),+0s,2299161j)>
ratdog:tmp mike$ pry
[1] pry(main)> require 'date'
=> false
[2] pry(main)> dt = DateTime.parse('2:12:03 PM ET', '%I:%M %p %Z')
=> #<DateTime: 2013-07-02T14:12:03+00:00 ((2456476j,51123s,0n),+0s,2299161j)>
[3] pry(main)> dt + (30.0 / 24) # days
=> #<DateTime: 2013-07-03T20:12:03+00:00 ((2456477j,72723s,0n),+0s,2299161j)>