Convert current time to UNIX

Hi,
I need script to get every day current date only with years,month,day and convert it to UNIX format. I dont need hours and minutes, but if it impossible it must be midnight, be like (2022-05-23 00:00:00)
import time
from datetime import datetime

current date and time\

shortDate = datetime.today().strftime(’%Y-%m-%d’)
print("Current Date Time is ", shortDate)
a=time.mktime(datetime.datetime.strptime(shortDate, “%Y-%m-%d”).timetuple())
print(a)
Maybe can somebody help me?

This bit of info may help you.

Did you know that 1h of ‘Unix time’ = 0.041666666668 ?

So, to increment any Unix time stamp by 24hrs, simply time_stamp += 24*0.041666666668 (where time_stamp is some arbitrary user defined variable).

My bad: that does not work outside of a spreadsheet.


This will get your script to work:

import datetime
import time

shortDate = datetime.date.today().strftime('%Y-%m-%d')
print("Current Date Time is ", shortDate)
a=time.mktime(datetime.datetime.strptime(shortDate, "%Y-%m-%d").timetuple())
print(a)

Thanks for explaining.

No worries; I hope it was of some use.

That formula is one that I use in my spreadsheets. I’ve not in fact tried to use that with Python, but I see no reason for it not to be valid.

Working date and times is quite a complex topic, more so if you want to account for time zones and the shifts that happen with Spring and Fall.

Recommended viewing:

By Rob via Discussions on Python.org at 23May2022 16:18:

This bit of info may help you.
Did you know that 1h of ‘Unix time’ = 0.041666666668 ?

It doesn’t help me, unless this is some crude unexplained joke where
that number is approximately 1/24.

So, to increment any Unix time stamp by 24hrs, simply time_stamp += 24*0.041666666668 (where time_stamp is some arbitrary user defined
variable).

This is just… nonsense. UNIX timestamps are in seconds, so a day is 24*3600. Except when it isn’t like when they do a leap second in a year.

Fiddling with dates, datetimes and UNIX timestamps requires you to know
what they all are, and exactly what you’re trying to achieve: are you
working with calendar dates? Or UNIX timestamps? Because the former is
not a direct consistent mulitple of the latter.

Grumpily,
Cameron Simpson cs@cskk.id.au, who has spent a lot of time thinking
about times in the last couple of months

Yeah, it’s a spreadsheet thing and I should have checked this before I posted – my bad.

Date / time zero in a spreadsheet = 30/12/1999 00:00:00 so zero +0. 0.041666666668 = 30/12/1999 01:00:00

By Rob via Discussions on Python.org at 24May2022 03:04:

Yeah, it’s a spreadsheet thing and I should have checked this before I posted – my bad.

Date / time zero in a spreadsheet = 30/12/1999 00:00:00 so zero +0.
0.041666666668 = 30/12/1999 01:00:00

Then the implication is that the spreadsheet times are days (+ fractions
of a day). But you asked the OP to advance a day by adding
24*0.041666666668, which is … approximately 1 !! Just a very elaborate
way to spell it, and using an unexplained “magic number”.

Have I mentioned my hatred of spreadsheets? So opaque!

And your “in a spreadsheet” example above may well be dependent on
exactly which vendor made the spreadsheet software.

Now, computing in days + a fraction is a reasonable way to work in
calendar based time, because you can add whole numbers of days to the
days part and get the right day. But adding a computed fraction to the
time part is more fuzzy. When that crosses a leap second it could leave
you on the same calendar day if that day was a whole second longer than
a normal day. So provided you’re adding days to days you’re ok. And
provided you’re adding fractions to fractions you’re usually ok
(provided the overflow is comfortably large).

But all this is why the OP, and all of us, need to know exactly what
we’re working in. Calendar days plus a distinct HH:MM:SS part?
Calendar days? UNIX timestamps (seconds since the start of 1 january
1970 UTC) - just seconds from a reference point? There are more choices,
but these are the common kinds of options.

Just saying you’re working with “times” is too vague, because the kind
of arithmetic involved depends on the kind of units you’re using.

Cheers,
Cameron Simpson cs@cskk.id.au