UTS to DateTime in Python from Last.fm API [duplicate] - python

This question already has answers here:
Converting unix timestamp string to readable date
(19 answers)
Closed 7 years ago.
The last.fm API using user.getRecentTracks method's response supplies a date in the following format:
"date": {
"#text": "11 Dec 2015, 01:41",
"uts": "1449798068"
},
What is this "uts" field and how do I convert it into datetime string for a MySql database in python? Would your suggested answer be more efficient than using datetime.datetime.strptime() method to convert the text string given?

uts looks like a timestamp. The abbreviation probably stands for UTC timestamp or Unix Timestamp. I'm not sure which, but, it simple to convert it to a datetime object
>>> from datetime import datetime
>>> dt = datetime.fromtimestamp(1449798068)
>>> print(dt)
datetime.datetime(2015, 12, 10, 20, 41, 8)
It seems the #text key has the time in a local timezone, which is five hours ahead.

Related

Re-formatting a date in python [duplicate]

This question already has answers here:
Convert String with month name to datetime
(2 answers)
Closed 6 months ago.
So I have a date in this format "August 10, 2022". I need to reformat the date in this way "2022-08-10". How do I do that in python ?.
For this, you can use datetime, specifically on this behaviour.
from datetime import datetime
a = "August 10, 2022"
b = datetime.strptime(a, '%B %d, %Y') # Converts to datetime format
c = b.strftime('%Y-%m-%d') # Converts from datetime to desired format
print(c)
# output
2022-08-10

Convert date into format %d%m%y Python [duplicate]

This question already has answers here:
datetime from string in Python, best-guessing string format
(4 answers)
How can I parse multiple (unknown) date formats in python?
(4 answers)
How can I translate dates and times from natural language to datetime? [closed]
(2 answers)
How to format date string via multiple formats in python
(5 answers)
Closed 2 years ago.
I have a problem in Python 3.9 64x bit.
In a program I am writing, I need to be able to convert any inputted date into the format %d%m%y.
For example, if the user entered 12 December 2021, the program will convert it to 121221, and if the user enters 2021 12 December, it will still convert it to 121221.
You could use pandas to_datetime and then strftime.
from pandas import to_datetime
to_datetime('12 December 2021').strftime('%d%m%y') ## returns 121221
to_datetime('2021 12 December').strftime('%d%m%y') ## returns 121221
pandas tries to infer the format when parsing the string.
Note, without specifying the datetime format for the string entered by the user there is of course ambiguity. E.g. what is meant by a the string '11/12/2021'. It could be '11 December 2021' or '12 November 2021'.
this of course is error prone if the user enters
>>> from_date="12 December 2021"
>>> import time
>>> conv=time.strptime(from_date,"%d %B %Y")
>>> time.strftime("%d/%m/%y",conv)
'121221'
>>> from_date="2021 12 December"
>>> import time
>>> conv=time.strptime(from_date,"%Y %d %B")
>>> time.strftime("%d%m%y",conv)
'121221'

How to remove seconds from Python datetime object? [duplicate]

This question already has answers here:
How to truncate the time on a datetime object?
(18 answers)
How to display locale sensitive time format without seconds in python
(4 answers)
Closed 4 years ago.
I have a python datetime object that I want to display on a website, however the time shows in the format hh:mm:ss and I want to display it in the format hh:mm.
I have tried using the replace method as per the following:
message.timestamp.replace(second='0', microsecond=0)
However this doesn't get red of the seconds it just replaces it with hh:mm:00
Use strftime() function
>>> from datetime import datetime
>>> datetime.now()
datetime.datetime(2018, 12, 27, 11, 14, 37, 137010)
>>> now = datetime.now()
>>> now.strftime("%H:%M")
'11:15'

Convert odd timestamp with offset to UNIX time in Python [duplicate]

This question already has an answer here:
Converting string with UTC offset to a datetime object [duplicate]
(1 answer)
Closed 9 years ago.
I have a timestamp string from a web log that looks like this:
10/Jun/2005:05:59:05 -0500
It like to convert it to a UNIX timestamp.
A datetime can be converted with time.mktime(datetime.timetuple())
According to the datetime docs, datetime.strptime() should convert it to a datetime:
from datetime import datetime
datetime.strptime("10/Jun/2005:05:59:05 -0500","%d/%b/%Y:%H:%M:%S %z")
At least on with Python 2.7.2 on my Mac, this results in
ValueError: 'z' is a bad directive in format '%d/%b/%Y:%H:%M:%S %z'
After reading many questions on SO about that error, I decided to try python-dateutil:
from dateutil import parser
parser.parse("10/Jun/2005:05:59:05 -0500")
That didn't work either:
ValueError: unknown string format
Now what?
You can use dateutils to make the converstion, you will need two steps:
>>> import calendar
>>> from dateutil.parser import parse
>>> d = parse('10/Jun/2005:05:59:05 -0500', fuzzy=True)
This will create a datetime object
>>> d
datetime.datetime(2005, 6, 10, 5, 59, 5, tzinfo=tzoffset(None, -18000))
And to convert it to UNIX timestamp:
>>> ts = calendar.timegm(d.utctimetuple())
>>> print ts
1118401145

Convert a ctime (like) timestamp into a tz aware datetime instance [duplicate]

This question already has answers here:
Comparing dates in Python - how to handle time zone modifiers
(3 answers)
Closed 10 years ago.
I have timestamps in the following format
"Sat Feb 06 07:00:13 -0800 2010"
What is the idiomatic/pythonic way to convert this kind of timestamp into tz aware datetime instance? ... I've fiddled with the single parts of the timestamp but looks all ugly
You can't parse time zones with datetime. But you can use the dateutil.parser module instead, as shown below. It returns a datetime.datetime object.
>>> dateutil.parser.parse('Sat Feb 06 07:00:13 -0800 2010')
datetime.datetime(2010, 2, 6, 7, 0, 13, tzinfo=tzoffset(None, -28800))
And to be fair, see this complete answer.

Categories

Resources