Python Forum
[SOLVED] Epoch timestamp without milliseconds?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Epoch timestamp without milliseconds?
#1
Hello,

What's a good way to get an Epoch timestamp without the milliseconds?

for row in cur.execute("SELECT id,created,title FROM content"):
	#2004-08-18 19:11:13
	formated_date = datetime.strptime(row["created"],"%Y-%m-%d %H:%M:%S")

	epoch = datetime.timestamp(formated_date)
	#BAD epoch = datetime.datetime.timestamp(formated_date).replace(microsecond = 0)
	
	#2004-08-20 13:15:42 1093000542.0 Some title
	print(row["created"], epoch, row["title"])
Thank you.
Reply
#2
I don't see milliseconds in your result. The 1093000542.0 is the number of seconds since the epoch.
Reply
#3
Maybe it's not miliseconds.

I'd like to get rid of the trailing ".0"
Reply
#4
There is no trailing 0. That is an artifact of printing a float. If you don't want ".0" to be printed, specify a format for printing. Or you could convert epoch to an int.
Reply
#5
That was easy :-) I thought it was timestamp() itself.

epoch = int(datetime.timestamp(formated_date))
print(row["created"], epoch, row["title"])
Thank you.
Reply
#6
If you don't know, either look it up or ask Python.
from datetime import datetime

epoch = datetime.now().timestamp()
print("epoch is a", type(epoch))
help(datetime.timestamp)
Output:
epoch is a <class 'float'> Help on method_descriptor: timestamp(...) Return POSIX timestamp as float.
ndc85430 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Alternative to regex to extract date from whole timestamp? Winfried 6 1,832 Nov-16-2022, 01:49 PM
Last Post: carecavoador
  error in timestamp Led_Zeppelin 3 3,201 Jun-15-2022, 08:28 PM
Last Post: deanhystad
  error in timestamp Led_Zeppelin 0 1,006 Jun-10-2022, 07:59 PM
Last Post: Led_Zeppelin
  |SOLVED] Glob JPGs, read EXIF, update file timestamp? Winfried 5 2,470 Oct-21-2021, 03:29 AM
Last Post: buran
  Stumped by my own code (ratio & epoch-time calculation). MvGulik 2 2,119 Dec-30-2020, 12:04 AM
Last Post: MvGulik
Thumbs Up Convert ActiveDirectory timestamp into regular one. Arrow (solved) SpongeB0B 2 1,927 Nov-02-2020, 08:34 AM
Last Post: bowlofred
  Writing a function to calculate time range in Unix Epoch Time t4keheart 2 2,987 Jul-15-2020, 01:55 AM
Last Post: t4keheart
  The following script works but I need to take it one step further to milliseconds. yeto 1 2,007 Jul-19-2019, 04:15 AM
Last Post: micseydel
  Parse Binary Data File and convert Epoch Time drdevereaux 1 3,159 May-16-2019, 01:56 AM
Last Post: Larz60+
  Timestamp is undefined ErnestTBass 7 7,927 Feb-16-2019, 08:27 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020