Get min and max value from this array of hashes - ruby

I have an array that contains a hash in each row containing created_at and a value. How do I get the min and max from the array for the value fields?
The array is called - channels_counts_for_history_graph
and
channels_counts_for_history_graph.max[1]
Gives me the max date rather than the max value?
[[Sun, 30 Dec 2018 15:03:55 UTC +00:00, 4305],
[Sun, 30 Dec 2018 15:05:42 UTC +00:00, 4305],
[Mon, 31 Dec 2018 09:24:06 UTC +00:00, 4306],
[Sat, 05 Jan 2019 09:04:50 UTC +00:00, 4308],
[Tue, 01 Jan 2019 11:26:04 UTC +00:00, 4306],
[Wed, 02 Jan 2019 17:24:19 UTC +00:00, 4305]]
Any help appreciated.
Thanks

I suggest using Enumerable#minmax_by to get the min and the max value in just one method call:
array = [['Sun, 30 Dec 2018 15:03:55 UTC +00:00', 4305],['Sun, 30 Dec 2018 15:05:42 UTC +00:00', 4305],['Mon, 31 Dec 2018 09:24:06 UTC +00:00', 4306],['Sat, 05 Jan 2019 09:04:50 UTC +00:00', 4308],['Tue, 01 Jan 2019 11:26:04 UTC +00:00', 4306],['Wed, 02 Jan 2019 17:24:19 UTC +00:00', 4305]]
array.minmax_by(&:last)
#=> [["Sun, 30 Dec 2018 15:03:55 UTC +00:00", 4305], ["Sat, 05 Jan 2019 09:04:50 UTC +00:00", 4308]]

By default when you sort an array sorts by the first element first.
You can reverse the array for the purposes of the sort.
channel_counts_for_history_graph.map(&:reverse).max[0]

I may guess that this is what you were asking for:
[{ created_at: Date.new(2017, 1, 1) }, { created_at: Date.new(2019, 1, 1) }, { created_at: Date.new(2018, 1, 1) }]
.minmax_by { |value| value[:created_at] }

Related

How to check if a date value inside an hash is bigger than a reference date

I'm writing a validation and I have an hash with this structure
elements.map{ |e| [e.id,e.coverable.published_at] }.to_h
=> {305=>Fri, 17 Apr 2020 15:23:00 CEST +02:00,
306=>Fri, 17 Apr 2020 13:00:00 CEST +02:00,
307=>Fri, 17 Apr 2020 09:20:00 CEST +02:00,
308=>Fri, 17 Apr 2020 12:59:00 CEST +02:00,
309=>Fri, 17 Apr 2020 11:39:00 CEST +02:00}
I have a reference date...
published_at
=> Mon, 04 May 2020 23:51:00 CEST +02:00
I have to check if any of the element has a published_at datetime value bigger than my published_at.
Is there a short way to do that?
Try something like this
elements.any? { |e| e.coverable.published_at > your_published_at }
In case you need the element which passes the condition use find
element = elements.find { |e| e.coverable.published_at > your_published_at }
# if element is not nil such element is present

Sort a collection with a property type Date value

I have a collection as:
content [Collection]
[0] [Content]
creationDate Thu Aug 22 11:50:37 GMT 2019
[1] [Content]
creationDate Thu Aug 22 11:45:37 GMT 2019
[2] [Content]
creationDate Thu Aug 22 11:54:37 GMT 2019
How can I sort this collection by date value?
i.e.
content [Collection]
[0] [Content]
creationDate Thu Aug 22 11:45:37 GMT 2019
[1] [Content]
creationDate Thu Aug 22 11:50:37 GMT 2019
[2] [Content]
creationDate Thu Aug 22 11:54:37 GMT 2019
Create a DateComparator for sorting.
class DateComparator implements Comparator<Date> {
public int compare(Date d1, Date d2) {
if (d1.before(d2)) {
return -1;
} else if (d1.after(d2)) {
return 1;
} else {
return 0;
}
}
}
and call content.sort(new DateComparator())

Sort a hash with key as month date with many values for the same month

I create a hash with months as keys and timelaps as values
biens_delai[bien_date.mon] = b.delai
I get this result without month parsing
{Wed, 18 Jan 2017=>3.0, Sat, 25 Feb 2017=>2.0, Fri, 17 Mar 2017=>3.0, Sat, 25 Mar 2017=>5.0, Tue, 18 Apr 2017=>2.0, Thu, 29 Jun 2017=>2.0}
In March i have 2 values but when i parse by month i get the most high value and i want a addition of 2 values for March not the most high
{1=>3.0, 2=>2.0, 3=>5.0, 4=>2.0, 6=>2.0}
That's not the high value which you are getting, the values are getting overwritten, try the following
biens_delai[bien_date.mon] = biens_delai[bien_date.mon].to_f + b.delai

Ruby: Datetime to UTC conversion

I am trying to convert the below date and time combination to UTC
from_date: "2017-06-19",from_time: "14:00"
to_date: "2017-06-19", to_time: "23:00"
Timezone: EDT
I am using below piece of code for conversion
Date.parse(dt).to_datetime + Time.parse(t).utc.seconds_since_midnight.seconds
And it gives the wrong date value for the to_date & to_time combination.
Output:
Date.parse(from_date).to_datetime +
Time.parse(from_time).utc.seconds_since_midnight.seconds
#⇒ **Mon, 19 Jun 2017 18:00:00 +0000**
Date.parse(to_date).to_datetime +
Time.parse(to_time).utc.seconds_since_midnight.seconds
#⇒ **Mon, 19 Jun 2017 03:00:00 +0000**
Above conversion should give "Tue, 20 Jun 2017 03:00:00 +0000" instead.
Below line of codes worked for me:
parsed_date = Time.zone.parse(from_date).strftime('%Y-%m-%d')
parsed_time = Time.zone.parse(from_time).strftime('%T')
Time.parse(parsed_date + ' ' + parsed_time).utc.strftime('%F %T')
require 'time'
from = Time.parse "2017-06-19 14:00 US/Eastern"
=> 2017-06-19 14:00:00 -0400
from.utc
=> 2017-06-19 18:00:00 UTC
to = Time.parse "2017-06-19 23:00 US/Eastern"
=> 2017-06-19 23:00:00 -0400
to.utc
=> 2017-06-20 03:00:00 UTC
Though you can also specify the timezone offset without using the string, doing it this way handles Daylight Savings Time.
I think this is shorter:
from_date = "2017-06-19"
from_time = "14:00"
DateTime.strptime("#{from_date}T#{from_time}ZEDT", "%Y-%m-%dT%H:%MZ%z").utc
=> Mon, 19 Jun 2017 18:00:00 +000
to_date = "2017-06-19"
to_time = "23:00"
DateTime.strptime("#{to_date}T#{to_time}ZEDT", "%Y-%m-%dT%H:%MZ%z").utc
=> Tue, 20 Jun 2017 03:00:00 +0000

DateTime set TimeZone

ubuntu 14.04
ruby 1.9.3-p484
rails 3.2.18
I have a date as a string: 06/20/2015 02:45 AM
d = DateTime.strptime('06/20/2015 02:45 AM', '%m/%d/%Y %I:%M %p').to_time
=> Sat, 20 Jun 2015 02:45:00 UTC
Current TimeZone may be different and placed in Time.zone.
I tried d.to_time.in_time_zone. It gives respectively for PDT and CDT TimeZone:
Fri, 19 Jun 2015 21:45:00 CDT -05:00
Fri, 19 Jun 2015 19:45:00 PDT -07:00
I need to get DateTime object that holds date Sat, 20 Jun 2015 02:45:00 PDT -07:00 for PDT zone or Sat, 20 Jun 2015 02:45:00 CDT -05:00 for CDT zone.
I think it would work:
zone = ActiveSupport::TimeZone.new("Central Time (US & Canada)")
d.to_time.in_time_zone.in_time_zone(zone)
or just
d.to_time.in_time_zone.in_time_zone("Central Time (US & Canada)")
Try this:
#config/application.rb
config.time_zone = 'Central Time (US & Canada)'
config.active_record.default_timezone = :local
Don't forget to restart your server.
I have found the solution. Method DateTime#offset rules:
d = DateTime.strptime('06/20/2015 02:45 AM', '%m/%d/%Y %I:%M %p')
d = d.change(offset: (Time.zone.now.utc_offset / 3600).to_s)

Resources