My rethinkdb stores data in the following format.
data = [{
'appName': "app1",
'startTime': "Mon, 14 Feb 2017 05:10:00 GMT",
'endTime': "Mon, 14 Feb 2017 05:15:00 GMT",
'status': "SUCCESS"
},
{
'appName': "app1",
'startTime': "Mon, 13 Feb 2017 05:10:00 GMT",
'endTime': "Mon, 13 Feb 2017 05:15:00 GMT",
'status': "FAILED"
},
{
'appName': "app2",
'startTime': "Mon, 13 Feb 2017 05:10:00 GMT",
'endTime': "Mon, 13 Feb 2017 05:15:00 GMT",
'status': "RUNNING"
}]
I need to fetch the latest information for all apps.
r.table('apps').group('appName').max('startTime').run()
But since my startTime is stored as a string, I can not do a max operation.
I tried updating the values in the table as follows,
r.table('apps').update({'startTimeDate': pytz.timezone('Europe/Rome').localize(datetime.strptime(r.row['startTime'], '%a, %d %b %Y %H:%M:%S GMT'))}).run()
I receive an error:
TypeError: must be string, not Bracket
How do I persist startTime and endTime as date in rethinkdb from string?
RethinkDB only supports dates formatted as ISO8601 or as a number of seconds since the UNIX epoch.
Your update query has the right idea, but it tries to use python functions inside the update, where row['startTime'] is a query fragment and not a string.
Something like this might work instead:
for app in r.table('apps').run():
date = (pytz.timezone('Europe/Rome')
.localize(datetime.strptime(app['startTime'],
'%a, %d %b %Y %H:%M:%S GMT'))
(r.table('apps')
.get(app['id'])
.update({'startTimeDate': date},
durability='soft')
).run()
r.table('apps').sync()
Related
Having issues with converting XML to JSON using pljson here is my XML
<orders>
<order>
<id>4781</id>
<customer_id>2330</customer_id>
<date_created>Tue, 22 Mar 2022 20:07:47 +0000</date_created>
<date_modified>Tue, 22 Mar 2022 20:07:56 +0000</date_modified>
<status_id>11</status_id>
<custom_status>Awaiting Fulfillment</custom_status>
</order>
</orders>
When converting it from XML to JSON, I get the JSON response why is it putting everything in brackets?
{
"row1" : "orders",
"row2" : ["order", ["id", "4781"], ["customer_id", "2330"], ["date_created", "Tue, 22 Mar 2022 20:07:47 +0000"], ["date_modified", "Tue, 22 Mar 2022 20:07:56 +0000"], ["status_id", "11"], ["custom_status", "Awaiting Fulfillment"]]
}
orders pljson_list;
l_json pljson;
orders := pljson_ml.xmlstr2json('<orders>
<order>
<id>4781</id>
<customer_id>2330</customer_id>
<date_created>Tue, 22 Mar 2022 20:07:47 +0000</date_created>
<date_modified>Tue, 22 Mar 2022 20:07:56 +0000</date_modified>
<status_id>11</status_id>
<custom_status>Awaiting Fulfillment</custom_status>
</order>
</orders>');
l_json := pljson(orders);
l_json.print;
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
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] }
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
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)