Athena - Convert String based timestamp to ISO time - amazon-web-services

I have a timestamp column that has a value like this Fri, 12 Mar 2021 14:00:02:270
I want to convert it to timestamp format to use any timestamp-related functions.
Expected output:
2021-03-12 14:00:02
I tried this, but seems its not the right syntax.
cast(date_parse(recordtime,'%a, %d %b %Y %T:%i:%S:')as TIMESTAMP )

From the documentation, the error seems to be at the end of the query, because %T is the format Time, 24-hour (hh:mm:ss), so you don't need to specify %i and %S after that.
This one works:
SELECT cast(date_parse('Fri, 12 Mar 2021 14:00:02:270', '%a, %d %b %Y %T:%f') as timestamp)
You have to add %f at the end to handle the millisecond after your Time format.

Related

Read Javascript date input in Django

I have a calendar/datepicker done in Javascript that creates an input of the following format "Thu Mar 29 2018"
On submit, I want to read the date in a Django date field, but it fails.
What solutions do I have ?
Use datetime.strptime
In your case
from datetime import datetime
datetime_format = "%a %b %d %Y"
date_object = datetime.strptime("Thu Mar 29 2018", datetime_format)

How to print the current time in the format Day, Date Month Year HH:MM:SS?

How to print the current time in the format Day, Date Month Year HH:MM:SS
Mon, 28 Aug 2017 15:37:01 .
And then, convert this timestamp to epoch seconds & vice-versa.
datetime module does all the job
>>> import datetime
>>> datetime.datetime.now().strftime("%a, %d %B %Y %H:%M:%S")
'Tue, 29 August 2017 03:04:21'

How can I correctly convert this?

I tried a couple of times converting this date format
Wed, 02 April 2015 15:50:53 SAST
to this format
YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]
but with no luck so far.
Is there any better way to this, that I might have missed?
Here's what I attempted:
date = Wed, 02 April 2015 15:50:53 SAST
splitter = date.split(" ")
joiner = " ".join(splitter[1:len(splitter)-1])
date = datetime.datetime.strptime(joiner,"%d %b %Y %H:%M:%S")
date = datetime.datetime.strftime(date,"%A, %b %d %Y %H:%M:%S %z")
When I'm saving it to the db, I'm receiving this error:
[Wed, 02 April 2015 15:50:53 SAST for that value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]
Have a look at strptime (str --> time) and strftime (date --> str).
EDIT:
You are trying to save a string to a DateTimeField. Just remove the string conversion (strftime).

Convert gmtime() to 4 byte hex

I have the time in this format:
Fri, 19 Dec 2014 03:55:24
and I want to convert it to a 4 byte hexadecimal value. My question is similar to this one: question, but the difference is that I have a different format, and I use the gmtime() function because I want the date since the Epoch. This is what I tried so far (by trying to break the code from the answer of the similar question into smaller parts):
ut = time.strftime("%a, %d %b %Y %H:%M:%S ", time.gmtime())
time.strptime(ut, '%a, %d %b %Y %H:%M:%S')
But I get the error:
ValueError: uncoverted data remains:
Could you please help me?
I know that it is a simple question, but I cannot see what is the problem.
Remove the trailing space in the format string used in
ut = time.strftime("%a, %d %b %Y %H:%M:%S ", time.gmtime())
The line should read:
ut = time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())
Without the space after %S, it runs on my side!
There are several steps:
Parse rfc 5322 time string into an object that represent the broken-down time
import email.utils
time_tuple = email.utils.parsedate("Fri, 19 Dec 2014 03:55:24")
Convert the broken-down time into "seconds since Epoch" number
import calendar
timestamp = calendar.timegm(time_tuple) # assume input time is in UTC
Print the number in hex format
print('%08X' % timestamp)
# -> 5493A1AC

parse string as date-time

Let's say I have the following string:
Sat, 14 Sep 2013 22:44:49 +0000
how would I turn it into the following format for Django models?
YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]
Thank you very much.
You can use datetime to achieve what you are looking for
from datetime import datetime
date_string = "Sat, 14 Sep 2013 22:44:49 +0000"
initial_format = "%a, %w %b %Y %H:%M:%S %z"
final_format = "%Y-%m-%d %H:%M:%S %z"
datetime.strptime(date_string, initial_format).strftime(final_format)
You can construct the appropriate formats here
Alternatively, for django models, you could just send datetime object as a parameter, and django would convert it to the appropriate format for you.