Time.zone.at different from Time.zone in Ruby On Rails 3 - ruby-on-rails-3

Here is what I am doing in console:
1.9.2p290 :018 > Time.zone = 'Brasilia'
=> "Brasilia"
1.9.2p290 :020 > Time.zone.now
=> Fri, 17 Aug 2012 13:08:06 BRT -03:00
1.9.2p290 :021 > Event.last.start_time
=> 2000-01-01 03:45:00 UTC
1.9.2p290 :022 > Time.zone.at(event.start_time)
=> Sat, 01 Jan 2000 01:45:00 BRST -02:00
Why is the last sentence returning a different timezone?

Related

how do you extract a variable that appears multiple times in a table only once

I'm trying to extract the name of space organisations from a table but the closest i can get is the amount of times it appears next to the name of the organisation but i just want the name of the organisation not the amount of times it is named in the table.
if you can help me please leave a comment on my google colab.
https://colab.research.google.com/drive/1m4zI4YGguQ5aWdDVyc7Bdpr-78KHdxhR?usp=sharing
What I get:
variable number
organisation
time of launch
0
SpaceX
Fri Aug 07, 2020 05:12 UTC
1
CASC
Thu Aug 06, 2020 04:01 UTC
2
SpaceX
Tue Aug 04, 2020 23:57 UTC
3
Roscosmos
Thu Jul 30, 2020 21:25 UTC
4
ULA
Thu Jul 30, 2020 11:50 UTC
...
...
...
4319
US Navy
Wed Feb 05, 1958 07:33 UTC
4320
AMBA
Sat Feb 01, 1958 03:48 UTC
4321
US Navy
Fri Dec 06, 1957 16:44 UTC
4322
RVSN USSR
Sun Nov 03, 1957 02:30 UTC
4323
RVSN USSR
Fri Oct 04, 1957 19:28 UTC
etc
etc
etc
What I want:
organisation
RVSN USSR
Arianespace
CASC
General Dynamics
NASA
VKS RF
US Air Force
ULA
Boeing
Martin Marietta
etc

How to find missing dates AND missing period in sql table within a given range?

Suppose there exist a table called:
RandomPriceSummary , which has the date ranging from Wed Oct 01 2022 00:00:00 GMT+0100 to Wed Oct 03 2022 00:00:00 GMT+0100, and period ranging from 1-3 and cost as shown below:
date
period
cost
Wed Oct 01 2022 00:00:00 GMT+0100 (British Summer Time)
1
10
Wed Oct 01 2022 00:00:00 GMT+0100 (British Summer Time)
2
20
Wed Oct 01 2022 00:00:00 GMT+0100 (British Summer Time)
3
10
Wed Oct 03 2022 00:00:00 GMT+0100 (British Summer Time)
1
20
Wed Oct 03 2022 00:00:00 GMT+0100 (British Summer Time)
2
20
In the above table, how can we check all of the missing dates and missing periods?
For example, we need a query WHERE SETTLEMENT_DATE BETWEEN TIMESTAMP '10-01-2022' AND TIMESTAMP '10-03-2022' which has a missing period ranging from 1-3.
So the expected answer should return something along the lines of :
missing_date
missing_period
Wed Oct 02 2022 00:00:00 GMT+0100 (British Summer Time)
1
Wed Oct 02 2022 00:00:00 GMT+0100 (British Summer Time)
2
Wed Oct 02 2022 00:00:00 GMT+0100 (British Summer Time)
3
Wed Oct 03 2022 00:00:00 GMT+0100 (British Summer Time)
3
We can use the following calendar table left anti-join approach:
SELECT d.dt, p.period
FROM (SELECT date_trunc('day', dd)::date AS dt
FROM generate_series(
'2022-01-01'::timestamp,
'2022-12-31'::timestamp,
'1 day'::interval) dd
) d
CROSS JOIN (SELECT 1 AS period UNION ALL SELECT 2 UNION ALL SELECT 3) p
LEFT JOIN RandomPriceSummary t
ON t.date::date = d.dt AND t.period = p.perio
WHERE d.dt BETWEEN '2022-10-01'::date AND '2022-10-03'::date AND
t.date IS NULL
ORDER BY d.dt, p.period;

Convert a Particular Dataframe Column Into Customized Date Or Time

As you can see a Date & Time Column are being saved in this CSV File. Now what problem is that the date & time are in format of something like - 30-1-2022 & 20:08:00
But i want it to look something like 30th Jan 22 and 8:08 PM
Any code for that ?
import requests
import pandas as pd
from datetime import datetime
from datetime import date
currentd = date.today()
s = requests.Session()
headers = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'}
url = 'https://www.nseindia.com/'
step = s.get(url,headers=headers)
today = datetime.now().strftime('%d-%m-%Y')
api_url = f'https://www.nseindia.com/api/corporate-announcements?index=equities&from_date={today}&to_date={today}'
resp = s.get(api_url,headers=headers).json()
result = pd.DataFrame(resp)
result.drop(['difference', 'dt','exchdisstime','csvName','old_new','orgid','seq_id','sm_isin','bflag','symbol','sort_date'], axis = 1, inplace = True)
result.rename(columns = {'an_dt':'DateandTime', 'attchmntFile':'Source','attchmntText':'Topic','desc':'Type','smIndustry':'Sector','sm_name':'Company Name'}, inplace = True)
result[['Date','Time']] = result.DateandTime.str.split(expand=True)
result.drop(['DateandTime'], axis = 1, inplace = True)
result.to_csv( ( str(currentd.day) +'-'+str(currentd.month) +'-'+'CA.csv'), index=True)
print('Saved the CSV File')
Try creating a temporary column:
result['Full_date']=pd.to_datetime(result['Date']+' '+result['Time'])
Then format 'Date' and 'Time'
result['Date']=result['Full_date'].dt.strftime('%b %d, %Y')
result['Time']=result['Full_date'].dt.strftime('%R' '%p')
Try this:
# Remove comment if needed
# import locale
# locale.setlocale(locale.LC_TIME, 'C')
# https://stackoverflow.com/a/16671271
def ord(n):
return str(n)+("th" if 4<=n%100<=20 else {1:"st",2:"nd",3:"rd"}.get(n%10, "th"))
result['Date'] = pd.to_datetime(result['Date'], format='%d-%b-%Y')
result['Date'] = result['Date'].dt.day.map(ord) + result['Date'].dt.strftime(' %b %Y')
result['Time'] = pd.to_datetime(result['Time']).dt.strftime('%-H:%M %p')
# Now you can export
Output:
>>> result[['Date', 'Time']]
Date Time
0 30th Jan 2022 21:07 PM
1 30th Jan 2022 20:57 PM
2 30th Jan 2022 19:40 PM
3 30th Jan 2022 18:55 PM
4 30th Jan 2022 18:53 PM
5 30th Jan 2022 18:09 PM
6 30th Jan 2022 17:44 PM
7 30th Jan 2022 16:01 PM
8 30th Jan 2022 15:21 PM
9 30th Jan 2022 15:16 PM
10 30th Jan 2022 15:10 PM
11 30th Jan 2022 15:06 PM
12 30th Jan 2022 14:29 PM
13 30th Jan 2022 14:15 PM
14 30th Jan 2022 13:41 PM
15 30th Jan 2022 12:20 PM
16 30th Jan 2022 12:09 PM
17 30th Jan 2022 12:07 PM
18 30th Jan 2022 10:58 AM
19 30th Jan 2022 10:42 AM
20 30th Jan 2022 10:40 AM
21 30th Jan 2022 10:39 AM
22 30th Jan 2022 10:06 AM
23 30th Jan 2022 9:39 AM
24 30th Jan 2022 9:36 AM
25 30th Jan 2022 9:25 AM
26 30th Jan 2022 8:43 AM
27 30th Jan 2022 1:00 AM
28 30th Jan 2022 0:59 AM
29 30th Jan 2022 0:13 AM

presto sql: select the data that before or after a datetime

There is table called t1, and there are columns id, created_at, text, for example, as following table:
id created text
1 Thu Jun 30 01:00:57 +0000 2016 I like this movie1
2 Thu Jun 30 02:59:57 +0000 2016 I like this movie2
3 Thu Jun 30 03:49:57 +0000 2016 I like this movie3
4 Thu Jun 30 04:59:50 +0000 2016 I like this movie4
5 Thu Jun 30 05:39:57 +0000 2016 I like this movie5
6 Thu Jun 30 06:39:57 +0000 2016 I like this movie6
7 Thu Jun 30 06:29:57 +0000 2016 I like this movie6
8 Thu Jun 30 07:09:57 +0000 2016 I like this movie7
9 Thu Jun 30 07:39:57 +0000 2016 I like this movie8
10 Thu Jun 30 08:39:57 +0000 2016 I like this movie9
11 Thu Jun 30 09:39:57 +0000 2016 I like this movie10
12 Thu Jun 30 10:29:57 +0000 2016 I like this movie11
13 Thu Jun 30 11:29:57 +0000 2016 I like this movie12
12 Thu Jun 30 12:29:57 +0000 2016 I like this movie13
I want to select data separated by hour time.
For example, I want to select all the data that hour is less or equal 06, then I want to select the data that hour is more than 07. Since the data of column is datetime form: Thu Jun 30 12:29:57 +0000 2016, I don't know how to deal with this. Thanks for your help!
The sql is presto(presto sql):
select id, created, text from t1 where created_at <= 6
You could use datepart for this if you are using mssql:
select
id,
created,
text
from
t1
where
datepart(hour, created) <= 6
References:
DATEPART (Transact-SQL)
I done it, use the hour(datestamp)can solve it.
select id, created, text from t1 where hour(created_at) <= 6

Modifying date to enable search in mysql DateTime format

I have an UI, in which we which we select a date range, and then perform a query to check orders within that range.
So the valid formats accepted are
a) 2013-04-01 17:00:00 - 2013-04-16 18:00:00
b) 2013-04-01 17:00:00 - 2013-04-16
c) 2013-04-01 - 2013-04-16
I split it in ruby to give me start_date and end_date. I don't have to touch the start time, as it ready for the sql query.
But the end_date has to be modified because to perform a ranged query created_at BETWEEN '2013-04-01 17:00:00' AND '2013-04-16' does not give me the results of 16th which should be included in the result set. (As it compares to 2013-04-16 00:00:00)
So this is the working solution, I came up with
end_date = Time.parse(end_date).strftime("%T")=="00:00:00" ? (end_date.to_s) + " 23:59:59" : end_date.to_s
Is there a better way to do this as the above looks quite confusing? (Looking or 1-2 line answers, not more)
You could use the DateTime.parse(str) method:
date_str = "2013-04-16"
date = DateTime.parse(date_str)
#=> Tue, 16 Apr 2013 00:00:00 +0000
date_time_str = "2013-04-16 18:00:00"
date = DateTime.parse(date_time_str)
#=> Tue, 16 Apr 2013 18:00:00 +0000
And then you could test the .hour (or .minute) and set the end_date to end_of_day if no time was selected:
end_date = (date.hour == 0 && date.minute == 0) ? date.end_of_day : date
Little improvement: You could test if the parsed date is equal to the beginning of this date (no hours/minutes/seconds):
date = DateTime.parse("2013-12-12")
#=> Thu, 12 Dec 2013 00:00:00 +0000
date.beginning_of_day
#=> Thu, 12 Dec 2013 00:00:00 +0000
end_date = (date.beginning_of_day == date) ? date.end_of_day : date
#=> Thu, 12 Dec 2013 23:59:59 +0000
To solve the problem when user enters a DateTime like 2013-04-16 00:00:00, you can use .include? to check if the string contains the '00:00:00' part:
date_str = "2013-04-16 00:00:00"
date = DateTime.parse(date_str)
end_date = date if date_str.include?('00:00:00') # means the user explicitly wants the time at 00:00 and 00 second
end_date ||= (date.beginning_of_day == date) ? date.end_of_day : date # set end_date if end_date.nil?