I want to have the current time without the seconds.
I use this: int(round(time.time() * 1000)) to get the current time, I get this as result:1589835912648
But when I transform (using datetime.fromtimestamp(int(1589835912648/1000)) ) it to something readable, I get this: datetime.datetime(2020, 5, 18, 23, 5, 12)
which is normal. But I want to get rid of the seconds!
like datetime.fromtimestamp(int(1589746140000/1000)) which gives: datetime.datetime(2020, 5, 17, 22, 9)
So, it gives the time without the seconds.
How can I do this?
I already tried round(int(round(time.time() * 1000)),-4) but that also gives me the seconds
Hope this is clear and that someone can help me
If you are fine with using something other than datetime, here:
import time
print(time.strftime("%H:%m"))
This will print the time in hours and minutes.
Hope this helps!
datetime.datetime is good for this because you can access each field easily by name:
>>> from datetime import datetime
>>> now = datetime.now()
>>> now.hour, now.minute
(14, 17)
or you can turn it into a string with strftime:
>>> datetime.now().strftime("%H:%M")
'14:17'
from datetime import datetime
current_time = datetime.now()
hour = current_time.hour
minute = current_time.minute
print(f"{hour}:{minute}")
Related
I am subtracting two dates in python 2.7 and getting wrong result in seconds. Apparently difference between these dates is more than 24h which is 86400s. But I am getting 44705s, why is that and how to fix it ?
>>> date1
datetime.datetime(2017, 10, 22, 11, 41, 28)
>>> date2
datetime.datetime(2017, 10, 20, 23, 16, 23)
>>> (date1-date2).seconds
44705
Calling .seconds will only give you the seconds component of the timedelta object, which only takes into account seconds, minutes, and hours (see docs for more detail). If you want the entire timedelta in seconds, call total_seconds.
>>> (date1 - date2).total_seconds()
131105.0
date1-date2 is datetime.timedelta(1, 44705). You're only looking at the seconds portion. Look at the days portion too.
I have a unix timestamp in seconds (such as 1294778181) that I can convert to UTC using
from datetime import datetime
datetime.utcfromtimestamp(unix_timestamp)
Problem is, I would like to get the corresponding time in 'US/Eastern' (considering any DST) and I cannot use pytz and other utilities.
Only datetime is available to me.
Is that possible?
Thanks!
Easiest, but not supersmart solution is using timedelta
import datetime
>>> now = datetime.datetime.utcnow()
US/Eastern is 5 hours behind UTC, so let's just create thouse five hours as a timedelta object and make it negative, so that when reading back our code we can see that the offset is -5 and that there's no magic to deciding when to add and when to subtract timezone offset
>>> eastern_offset = -(datetime.timedelta(hours=5))
>>> eastern = now + eastern_offset
>>> now
datetime.datetime(2016, 8, 26, 20, 7, 12, 375841)
>>> eastern
datetime.datetime(2016, 8, 26, 15, 7, 12, 375841)
If we wanted to fix DST, we'd run the datetime through smoething like this (not entirely accurate, timezones are not my expertise (googling a bit now it changes each year, yuck))
if now.month > 2 and now.month < 12:
if (now.month == 3 and now.day > 12) or (now.month == 11 and now.day < 5):
eastern.offset(datetime.timedelta(hours=5))
You could go even into more detail, add hours, find out how exactly it changes each year... I'm not going to go through all that :)
I have a time which is 13:11:06 and i want to -GMT (i.e -0530). I can minus it by simply doing -5 by splitting the string taking the first digit (convert to int) and then minus it and then re-join. But then i get it in a format which is 8:11:06 which is not right as it should be 08:11:06, secondly its a lengthy process. Is there a easy way to get my time in -GMT format (08:11:06)
This is what i did to get -GMT time after getting the datetime
timesplithour = int(timesplit[1]) + -5
timesplitminute = timesplit[2]
timesplitseconds = timesplit[3]
print timesplithour
print timesplitminute
print timesplitseconds
print timesplithour + ":" + timesplitminute + ":" + timesplitseconds
You could use Python's datatime library to help you as follows:
import datetime
my_time = "13:11:06"
new_time = datetime.datetime.strptime("2016 " + my_time, "%Y %H:%M:%S") - datetime.timedelta(hours=5, minutes=30)
print new_time.strftime("%H:%M:%S")
This would print:
07:41:06
First it converts your string into a datetime object. It then creates a timedelta object allowing you to subtract 5 hours 30 minutes from the datetime object. Finally it uses strftime to format the resulting datetime into a string in the same format.
Use the datetime module:
from datetime import datetime, timedelta
dt = datetime.strptime('13:11:06', '%H:%M:%S')
time_gmt = (dt - timedelta(hours=5, minutes=30)).time()
print(time_gmt.hour)
print(time_gmt.minute)
print(time_gmt.second)
s = time_gmt.strftime('%H:%M:%S')
print(s)
Output
7
41
6
07:41:06
Note that this subtracts 5 hours and 30 minutes as initially mentioned in the question. If you really only want to subtract 5 hours, use timedelta(hours=5).
You can use datetimes timedelta.
print datetime.datetime.today()
>>> datetime.datetime(2016, 3, 3, 10, 45, 6, 270711)
print datetime.datetime.today() - datetime.timedelta(days=3)
>>> datetime.datetime(2016, 2, 29, 10, 45, 8, 559073)
This way you can subtract easily
Assuming the time is a datetime instance
import datetime as dt
t = datetime(2015,12,31,13,11,06)
#t.time() # gives time object. ie no date information
offset = dt.timedelta(hours=5,minutes=30) # or hours=5.5
t -= offset
t.strftime(("%H:%M:%S") # output as your desired string
#'18:41:06'
If the object is datetime and you don't care about DST, the simplest thing you can do is,
In [1]: from datetime import datetime
In [2]: curr = datetime.now()
In [3]: curr
Out[3]: datetime.datetime(2016, 3, 3, 9, 57, 31, 302231)
In [4]: curr.utcnow()
Out[4]: datetime.datetime(2016, 3, 3, 8, 57, 57, 286956)
datetime.utcnow()
This call is returning an incorrect datetime, delayed from UTC/GMT by 1 hour (check in: http://www.worldtimeserver.com/current_time_in_UTC.asp).
Is it working like it should be?
For example, it's returning, right now:
2015-02-17 23:58:44.761000.
Current UTC time is: 00:58, not 23:58
I know I'm five years late, but I had the same problem tonight. In my experience, the solution to the problem was to use the aware UTC datetime:
utc_dt_aware = datetime.datetime.now(datetime.timezone.utc)
If you google "utcnow() wrong" this is the first result you get, so I thought it would be good to answer anyway.
datetime.utcnow() uses OS provided values.
datetime.utcnow() uses gettimeofday(2) or time.time() on Python 2 (and gmtime(3) to convert the result into broken-down time).
time.time() uses gettimeofday(2), ftime(3), time(2). Newer CPython versions may use clock_gettime(2), GetSystemTimeAsFileTime().
You could check the self-consistency as follows:
#!/usr/bin/env python
import time
from datetime import datetime, timedelta
print(datetime.utcnow())
print(datetime(1970, 1, 1) + timedelta(seconds=time.time()))
print(datetime(*time.gmtime()[:6]))
Here's (non-tested) code that calls GetSystemTimeAsFileTime() on Windows based on CPython source:
#!/usr/bin/env python
import ctypes.wintypes
from datetime import datetime, timedelta
def utcnow_microseconds():
system_time = ctypes.wintypes.FILETIME()
ctypes.windll.kernel32.GetSystemTimeAsFileTime(ctypes.byref(system_time))
large = (system_time.dwHighDateTime << 32) + system_time.dwLowDateTime
return large // 10 - 11644473600000000
print(datetime(1970, 1, 1) + timedelta(microseconds=utcnow_microseconds()))
Here's code that calls clock_gettime() on Python 2.
Problem only occurs with utc time (Python3).
e.g. System time:
$ date
Wed Jul 15 10:44:26 BST 2015
Python time correct when using datetime.now():
>>> datetime.now()
datetime.datetime(2015, 7, 15, 10, 44, 30, 775840)
...But incorrect by one hour when using datetime.utcnow():
>>> datetime.utcnow()
datetime.datetime(2015, 7, 15, 9, 44, 32, 599823)
UTC's problem is it doesn't know my timezone.
You have to tell it, with the help of a timezone module called pytz:
>>> import pytz
>>> mytz = pytz.timezone('Europe/London')
>>> pytz.utc.localize(datetime.utcnow(), is_dst=None).astimezone(mytz)
datetime.datetime(2015, 7, 15, 11, 3, 43, 688681, tzinfo=<DstTzInfo 'Europe/London' BST+1:00:00 DST>)
References:
pytz - Converting UTC and timezone to local time
https://opensourcehacker.com/2008/06/30/relativity-of-time-shortcomings-in-python-datetime-and-workaround/
http://sweemengs-tech-world.blogspot.co.uk/2010/05/get-correct-datetime-value-for-python.html
http://bugs.python.org/issue5094)
I know I am tremendously late in replying to this.
I have tried doing this recently and therefore I suggest using datetime.now() instead of datetime.utcnow(). For my simple application that works fine.
I am storing all my times in UTC and my system is set to UTC (though I am in EST).
I have dates stored as:
Wed, 20 Feb 2013 03:51:39 +0000
However, I would like to select information based off today for EST, so I am attempting to:
Get current time as UTC and change to EST
datetime.utcnow().replace(tzinfo=tz.tzutc()).astimezone(tz.gettz('America/New_York'))
2013-02-19 23:17:20.560898-05:00
Next I want to get the start time for the EST day (2013-02-19 00:00:00.000000-05:00) and the end time (2013-02-19 23:59:59.99999-05:00)
Once I have those values, I'd like to convert back to UTC, so I have a high and low value I can clamp by that's correct my EST (my timezone).
If this isn't the best way to do this, or I'm missing something (does seem overly complicated to me) please help me see the light!
TIA
Update per answer:
d1 = datetime.utcnow().replace(tzinfo=tz.tzutc()).astimezone(tz.gettz('America/New_York'))
print d1.strftime("%m %d %Y") ; d2 = d1.replace(day=d1.day + 1) ; print d2.strftime("%m %d %Y")
That will give me
02 20 2013
02 21 2013
Which is correct. I now need to generate the full EST time from that and then convert to UTC. This I cannot figure out. Actually, I probably want to convert to UTC epoch timestamp when complete because that will make my database operations pretty easy (<, >, ==, etc).
The first step of getting current time as UTC and converting it to EST seems a bit pointless. Do you use that time for anything?
Other than that it seems rather straighforward. You want to get the start and end of a day EST in UTC, so you create them and convert them to UTC. That's not so complicated. :-)
You might want to look at your matching routines though, so that you can use the start of today as the lower value, and the start of tomorrow as the higher, so you don't have to deal with that 23:59:59.9999 time.
Update:
From my original understanding of your question, this is what you want to do:
First you want to get the current date as it is in UTC (so at 11pm EST the 12st, you want the 22nd, as it is the 22nd in UTC then.
>>> from datetime import datetime
>>> today = datetime.utcnow().date()
>>> today
datetime.date(2013, 2, 21)
Secondly you want 00:00:00 of that day in UTC, as start for a search.
>>> from dateutil import tz
>>> start = datetime(today.year, today.month, today.day, tzinfo=tz.tzutc())
datetime.datetime(2013, 2, 21, 0, 0, tzinfo=tzutc())
Except that you want to know what that time is in New York:
>>> from dateutil import tz
>>> est = tz.gettz('America/New_York')
>>> start = start.astimezone(est)
>>> start
datetime.datetime(2013, 2, 20, 19, 0, tzinfo=tzfile('/usr/share/zoneinfo/America/New_York'))
And you also want tomorrow as the end:
>>> from datetime import timedelta
>>> end = start + timedelta(1)
>>> end
datetime.datetime(2013, 2, 21, 19, 0, tzinfo=tzfile('/usr/share/zoneinfo/America/New_York'))
Summary:
today = datetime.utcnow().date()
start = datetime(today.year, today.month, today.day, tzinfo=tz.tzutc()).astimezone(est)
end = start + timedelta(1)
use datetime pytz will solve your problem.
def get_start_and_end():
tz = pytz.timezone('Asia/Shanghai')
today = datetime.now(tz=tz)
start = today.replace(hour=0, minute=0, second=0, microsecond=0)
end = start + timedelta(1)
return start, end
The question is old but maybe this helps:
import datetime
end_of_today = datetime.datetime.combine(datetime.datetime.today(), datetime.time(23, 59, 59, 999999))
I would definitely give Delorean a look, to solve your problem would follow a few steps.
You first need to parse your string. Excellent use the Delorean parse method.
>>> from delorean import parse
>>> d = parse("Wed, 20 Feb 2013 03:51:39 +0000")
>>> d
Delorean(datetime=2013-02-20 03:51:39+00:00, timezone=UTC)
Once you have the datetime that you parsed in a Delorean object you simply convert to EST
>>> d = d.shift('US/Eastern')
>>> d
Delorean(datetime=2013-02-19 22:51:39-05:00, timezone=US/Eastern)
Albeit pointless. You never use it for anything in your question, but super easy with Delorean.
Then you get the time now in EST
from delorean import Delorean
>>> d1 = Delorean(timezone="US/Eastern")
>>> d1
Delorean(datetime=2013-02-21 00:35:56.405256-05:00, timezone=US/Eastern)
Now for the truncation step.
>>> d.truncate('day')
Delorean(datetime=2013-02-21 00:00:00-05:00, timezone=US/Eastern)
do the simple shift as above to UTC.
Now get the end of day.
d = d.next_day(1) # move to the next day
Then to shift back one second. Something that the library needs I will be updating this. Simply get the datetime from the Delorean example by asking for it with datetime attribute.
d.datetime - timedelta(seconds=1)
datetime.datetime(2013, 2, 21, 23, 59, 59, tzinfo=<DstTzInfo 'US/Eastern' EST-1 day, 19:00:00 STD>)
Goodluck, but this library should simply your dealing with datetime operations :)
This is only a partial answer, because the rest has been covered well. I struggled with this for a while, as some technologies have inclusive searches, and I don't want to include any data from the first microsecond of the next day.
My solution for finding the end of day time quickly and correctly is this:
reference_time.replace(hour=0, minute=0, second=0, microsecond=0) + timedelta(days=1,microseconds=-1)
How about this
import datetime
datetime.datetime.combine(datetime.date.today(), datetime.time(00, 00, 00))
datetime.datetime.combine(datetime.date.today(), datetime.time(23, 59, 59))
Just ran into this, here's the simplest option I found:
from delorean import Delorean
today_d = Delorean()
sod_dt = today_d.start_of_day
eod_dt = today_d.end_of_day
sod_d = Delorean(sod_dt)
eod_d = Delorean(eod_dt)
sod_e = sod_d.epoch
eod_e = eod_d.epoch
to confirm:
In [69]: eod_e - sod_e
Out[69]: 86399.99999904633
close enough for most people
Here's a nice solution if you already use Arrow.
import arrow
now = arrow.now('US/Eastern')
start = now.floor('day')
end = now.ceil('day')
# Use the "datetime" property to access the actual datetime
print(start.datetime)
With python3.8, we can use as follow:
from datetime import datetime, timedelta, time
from pytz import timezone
today = datetime.utcnow()
start_of_day = datetime.combine(today, time.min).astimezone(tz=timezone('America/New_York'))
end_of_day = datetime.combine(today, time.max).astimezone(tz=timezone('America/New_York'))
To get the min and max time of the day, you could use datetime.time that gives you the earliest and latest representalbe time:
time.min --> datetime.time(0, 0)
time.max --> datetime.time(23, 59, 59, 999999)
and then you can combine this with datetime.now() or the specific date you want as below to get the desired result
from datetime import datetime, time
tmp = datetime.combine(datetime.now(), time.max)
print(tmp) --> 2022-09-27 23:59:59.999999
from datetime import datetime, time
tmp = datetime.combine(datetime.now(), time.max)
print(tmp) --> 2022-09-27 00:00:00
Simplest solution for getting the start of a day, though it might produce a phantom microsecond in unlucky floating point imprecision cases:
import datetime as dt
d = dt.datetime.now()
d = d - dt.timedelta(seconds=d.timestamp() % dt.timedelta(days=1).total_seconds())