I have a hash like this:
a = {:start=>"Tue, 27 Jan 2015 13:00:00 +0000", :end=>"Tue, 27 Jan 2015 13:30:00 +0000", :title=>"2015-01-27T13:00:00+00:00 to 2015-01-27T13:30:00+00:00"}
and another hash like
b = {:start=>Tue, 27 Jan 2015 13:30:00 +0000, :end=>Tue, 27 Jan 2015 14:00:00 +0000, :title=>"2015-01-27T13:30:00+00:00 to 2015-01-27T14:00:00+00:00"}
I want to merge these two into
c = {{:start=>"Tue, 27 Jan 2015 13:00:00 +0000", :end=>"Tue, 27 Jan 2015 13:30:00 +0000", :title=>"2015-01-27T13:00:00+00:00 to 2015-01-27T13:30:00+00:00"},{:start=>Tue, 27 Jan 2015 13:30:00 +0000, :end=>Tue, 27 Jan 2015 14:00:00 +0000, title=>"2015-01-27T13:30:00+00:00 to 2015-01-27T14:00:00+00:00"}}
I tried the following:
c = a.merge(b)
and also
a.merge!(b)
This gave back only one value and
c = a+b
gave errors.
How to do this?
Its syntactically incorrect even for JSON, I believe. You want an array of hashes, by turning the outer {} into []. Better: c = [a,b].to_json
Related
I'm trying to parse this date Wed, 17 Feb 2021 13:00:00 +0100 into this 2021-02-17 13:00:00.000000000 +0100.
And I've tried using this Time.strptime(current_time.to_s, '%Q'), (where current_time it's the date above) but I get 1970-01-01 01:00:02.021 +0100
But I don't understand why I get another date, could you help me? Thanks!
I'm trying to parse this date Wed, 17 Feb 2021 13:00:00 +0100 [...]
You seem to already have an instance of Time: (or ActiveSupport::TimeWithZone which is Rails' drop-in replacement with better timezone support)
current_time = Time.current
#=> Thu, 19 May 2022 10:09:58.702560000 CEST +02:00
In this case, there's nothing to parse. You just have to format it via strftime the way you like:
current_time.strftime('%F %T.%N %z')
#=> "2022-05-19 10:09:58.702560000 +0200"
Parsing is only needed when you have a string representation that you want to turn into a Time object, e.g.: (using Rails' Time.zone.parse variant)
time_string = 'Thu, 19 May 2022 10:09:58.702560000 CEST +02:00'
time_obj = Time.zone.parse(time_string)
#=> Thu, 19 May 2022 10:09:58.702560000 CEST +02:00
time_obj.class
#=> ActiveSupport::TimeWithZone
I'm trying to parse a specific hour of a specific date. When I put the date directly as an argument, it works fine, but when I create a variable and put it in the argument it returns the current date.
Why is that?
NOTE: the variable time is 9pm and I need to parse 9pm of 12 March 2016.
datetime = DateTime.new(2016,3,12,9)
=> Sat, 12 Mar 2016 09:00:00 +0000
DateTime.parse("sat 12 march 2016 9pm")
=> Sat, 12 Mar 2016 21:00:00 +0000
DateTime.parse("datetime 9pm")
=> Mon, 14 Mar 2016 21:00:00 +0000
In your third call, you use the literal string "datetime" rather than the value of your datetime variable. You can use string interpolation to use the variable's value:
DateTime.parse("#{datetime} 9pm")
In this case, the "9pm" is ignored since it doesn't make sense added to the end of an existing date but this is why the initial attempt wasn't working. Interpolation is generally a solution for using a variable's value rather than its name.
If your goal is to change the time of an existing date, use the change method:
datetime.change(hour:21)
You can also try this
date = Date.new(2016,3,12)
DateTime.parse("#{date} 9pm")
## Output
Sat, 12 Mar 2016 21:00:00 +0000
OR
datetime = DateTime.new(2016,3,12,9)
DateTime.parse((datetime + 12.hours).to_s)
## Output
Sat, 12 Mar 2016 21:00:00 +0000
OR
DateTime.parse((datetime + 12.hours).to_s).strftime("%a, %d %b %Y %I:%M %p")
## Output
Sat, 12 Mar 2016 09:00 PM
okay, lets say we have three date ranges.
1. date1=01-20-2015..02-15-2015
2. date2 = 01-01-2015..01-31-2015
3. date3 = 02-01-2015..02-28-2015
I need a way to calculate how many days between date1 and date2 are overlapped (10 days in this case).
and how many days between date1 and date3 are overlapped (15 days in this case)?
Is there a easy way to calculate this?
If you have:
r1= (Date.new(2015, 01, 20))..(Date.new(2015, 2, 15))
r2= (Date.new(2015, 01, 1))..(Date.new(2015, 1, 31))
You can check the intersection this way:
r1.to_a & r2.to_a
=> [Tue, 20 Jan 2015,
Wed, 21 Jan 2015,
Thu, 22 Jan 2015,
Fri, 23 Jan 2015,
Sat, 24 Jan 2015,
Sun, 25 Jan 2015,
Mon, 26 Jan 2015,
Tue, 27 Jan 2015,
Wed, 28 Jan 2015,
Thu, 29 Jan 2015,
Fri, 30 Jan 2015,
Sat, 31 Jan 2015]
The cost is to convert ranges to arrays though
You can notice (r1.to_a & r2.to_a).count #=> 12, not 10 but well depends whether or not you include boundaries.
I have two DateTime objects, one in the past and one representing the current datetime. I am trying to find out how many minutes have passed between the two:
past = "Wed, 03 Jul 2013 00:59:39 UTC +00:00".to_datetime
now = "Wed, 03 Jul 2013 01:04:19 +0100".to_datetime
seconds = (now - past) #result is (-83/2160)
This is incorrect. Seconds should be 280, the number of seconds that have passed between the two times.
Subtracting two DateTimes returns the elapsed time in days.
So you can do:
past = "Wed, 03 Jul 2013 00:59:39 UTC +00:00".to_datetime
now = "Wed, 03 Jul 2013 01:04:19 +0100".to_datetime
seconds = (now - past) * 1.day
# => -3320.0
Or you could do:
seconds = (now.to_i - past.to_i)
# => -3320
※ The result is negative because of the Timezone.
past.utc
# => Wed, 03 Jul 2013 00:59:39 +0000
now.utc
# => Wed, 03 Jul 2013 00:04:19 +0000
You can see that now is actually older than past.
ActiveSupport::TimeZone.new(-4).parse("2012-08-20T14:00:00-0400")
returns
Mon, 20 Aug 2012 15:00:00 ADT -03:00
I would expect parse() to return a Time with -04:00, like Mon, 20 Aug 2012 14:00:00 EDT -04:00
How would you handle this? I think that Daylight Saving Time is messing up things here.
I've handled this problem using new_offset from DateTime.
time = "2012-08-20T14:00:00-0400"
DateTime.parse(time).new_offset("+2")
returns Mon, 20 Aug 2012 20:00:00 +0200