1

I have a python script wich runs as a continous service for long time (days, month ...). From time to time data from the serial port comes in. These values were calculated and stored in a database. Everything is ok so far.

Now I want to extend the script a bit. Therefore I am looking for a built in method from eg. datetime .., which returns the past seconds of each day as an Integer value when the data comes in. There is no need for timezone, epoche ...

For a other reason I just use someting like this "t = time.strftime('%H:%M') ". This works.

But, can I get the seconds as Int in one step ? Must I calculate it by myself ? Or have I overseen something in the docs ?

MarEng
  • 13
  • 2
  • Possible duplicate of [Number of seconds since the beginning of the day UTC timezone](http://stackoverflow.com/questions/8072740/number-of-seconds-since-the-beginning-of-the-day-utc-timezone) – MaLiN2223 May 14 '17 at 15:26
  • Leap seconds and daylight savings time (which is related to time zones) aside, *every* day has 86,400 seconds. – chepner May 14 '17 at 15:31

1 Answers1

2

This is one way to do it. I don't know if you count the modulo operator as a second step.

int(time.time()) % 86400

That gives elapsed seconds since 00h00 UTC today. You'll have to add an offset for the timezone you are in if you want it to show wall-clock time. And I suppose that might count as another step.

BoarGules
  • 16,440
  • 2
  • 27
  • 44
  • thank you for this helpfull answer. I tried this in above way and added the offset of my timezone. I got the expected value on a very short way. – MarEng May 14 '17 at 20:40
  • If you liked the solution please accept the answer. – BoarGules May 14 '17 at 21:25