24 hour format for Python timestamp - python

Currently I'm creating timestamps with the Python time module. I created a string using this command
timestamp = time.strftime('%l:%M:%S')
However, this prints the format in a 12 hours format. Is there a way I can do this in a 24 hours format so it would show up as 20:52:53 instead of 8:52:53?
Thanks!

Try
timestamp = time.strftime('%H:%M:%S')
Check out this link for info on Python time module
https://docs.python.org/3/library/time.html#time.strftime

Related

Timestamps in QuestDB Python time.time() Not working

ı am building a database in QuestDB.
ı set up a table and one column is timestamp.
docs about table = https://questdb.io/docs/guides/working-with-timestamps-timezones/
timestamp column is converting automaticly 1623167145123456 to '2021-06-08T16:45:45.123456Z'.
doc say :
The native timestamp format used by QuestDB is a Unix timestamp in microsecond resolution. QuestDB does not store time zone information alongside timestamp values and therefore it should be assumed that all timestamps are in UTC. The following example shows how a Unix timestamp in microseconds may be passed into a timestamp column directly
when ı try to send this column
time.time()
it's not working.
Python time.time() = 1657105707.8171313
doc input = 1623167145123456
when ı delete this blank in python time.time() and send this to timestamp column
output is :
2495-02-11T11:02:24.069445Z
what should ı do ?
Try this out int(time.time() * 1000000000)
It is working for me in this example to send open data from TFL to QuestDB

how do i get only the time in python without the date and year?

I need to get time in python and i am using time.ctime() and it returns this Thu Jul 8 15:37:26 2021
But i only need 15:37:26 and i cant figure out how to get only this and not the date and year.
I already tried using datetime where i could not figure it out either so im trying with time now.
here is a bit of code for the context:
cas = time.ctime()
cas = str(cas)
api.update_status('nyni je:'+ cas )
time.sleep(60)
Anyone know how to do it?
print(datetime.datetime.now().time().isoformat(timespec='seconds'))
import datetime
print(datetime.datetime.now().strftime("%H:%M:%S"))
imports
from datetime import datetime
code
now = datetime.now()
cas = now.strftime("%H:%M:%S")
print(cas)
You can use strftime to convert a datetime value to a string of a certain format. In your case you can use %H:%M:%S to only get the time. The same function can be used to get the date as well, you can read more here.
Take a look at the "strftime() and strptime() Format Codes" section also for how you can format it.

want to convert json timestamp into normal timestamp (CST Hrs) in python

I am downloading a json file containing timestamp using python . But the timestamp i am getting is below format
`2021-04-01T21:43:52.757Z`
Want to convert into normal timestamp (CST Hrs). I also see that the time is increased by 4 hours when i compare the report manually.
`4/1/2021 5:43:53 PM`
The above hours is 4 hrs less when i compare with json file entry. Please advise me.
You need to use python's datetime module to handle this. The Z in the string actually means time zone 0 or UTC time which is 6 hours ahead of CST not 4:
import datetime
date_object = datetime.datetime.strptime(
"2021-04-01T21:43:52.757Z", "%Y-%m-%dT%H:%M:%S.%fZ"
)
date_object = date_object - datetime.timedelta(hours=6)
print(
f"{date_object.month}/{date_object.day}/{date_object.year} {date_object.strftime('%I:%M:%S %p')}"
)
Which will give this output:
4/1/2021 03:43:52 PM
have to use an f string if you want non zero padded dates because its not available in datetime according to the docs
You can use pytz module to deal with time zones directly and not hardcode them if you want. Or if you are on python 3.9 you can use timezone objects to create timezones yourself

How to keep only the Hour:Minute:Second part of recorded time series? (How to convert %Y-%m-%d %H:%M:%S to only %H:%M:%S ?)

I have a .csv file that includes 2 columns, begin_time & end_time which contain records in this format: 2013-02-12 16:21:48 .
They start from 2013 till 2017.
My goal is to convert all of them to only 16:21:48 part, so I'll be able to map all of the datasets into a [00:00:00 - 23:59:59] range to monitor them through 24 hours of the day to check abnormal events.
So I want to remove the yy:mm:dd part of the record, and only keep the hh:mm:ss part.
These are the datasets of smart home events and I'm trying to discover the activities and the abnormal changes among them.
I have tried datetime.strftime(time_records, format="%H:%M:%S") and it returns str, but when I try to convert the str to pandas.timestamps, it brings back the yy:mm:dd
I expect the 2013-02-12 16:21:48 to be 16:21:48 in timestamp or datetime format, so I'll be able to convert them to UNIX timestamp format.
Thanks in advance
from datetime import datetime
a='2013-02-12 16:21:48'
c=datetime.strptime(a,'%Y-%m-%d %H:%M:%S').time()
print(c)
output
16:21:48

Python: Formatting time for influxdb

I want to input a date/time into influx db from python but not sure how to do this. Python code datetime.datetime.now() currently gives out a format like this : 2018-06-25 13:59:36.698000.
I know this format is accepted by influx "1529932431998" which is "2018-06-25T13:13:51.998Z" or "0x1643714427e". Any ideas how i can get python to give me 11 digit number from python?
Thanks
Looks like you need epoch time.
Try:
import datetime
import time
print( time.mktime(datetime.datetime.now().timetuple()) )
Output:
1529932959.0

Categories

Resources