How to find the nearest midnights - python

I have times which are in the form of seconds since the start of the Unix epoch. For example, 1410974864 which is Wed, 17 Sep 2014 17:27:44 GMT .
How can I find the start and end of the day for a given time in this form?

datetime and pytz are a good team for that:
import datetime, pytz
epoch = 1410974864
dt_epoch = datetime.datetime.fromtimestamp(epoch, tz=pytz.UTC)
dt_midnight = dt.replace(hour=0, minute=0, second=0, microsecond=0)
So, the beginning and end of day:
day_start = dt_midnight
day_end = dt_midnight + datetime.timedelta(days=1)

You know that a day is 3600 seconds/hour * 24 hours = 86400 seconds. So just take the time you have and mod it by 86400:
seconds_through_current_day = current_time % 86400
Now you have how many seconds have elapsed since the beginning of the day so subtract this from the total time since epoch and you'll be left with the beginning of the current day
beginning_of_current_day = current_time - second_through_current_day
Lastly, just add a day's worth of seconds to get the end of the current day
end_of_current_day = beginning_of_current_day + 86400

Related

How to find the amount of minutes between the current time and a future time of the same day in python

I'm trying to make a program that tells me how long there is (in minutes) until a certain time of day in the future. I've been trying to write working code all night, but I just can't wrap my head around strftime, timedelta and datetime. I'm quite new to python, and being able to do this would be quite useful to my day-to-day life; Can someone help me out?
from datetime import datetime
def minutes_until(hour, minute):
#get the current time
now = datetime.now()
#get the current hour
current_hour = now.hour
#get the current minute
current_minute = now.minute
#get the current second
current_second = now.second
#get the current microsecond
current_microsecond = now.microsecond
#get the time until the specified hour
time_until_hour = hour - current_hour
#get the time until the specified minute
time_until_minute = minute - current_minute
#get the time until the specified second
time_until_second = 60 - current_second
#get the time until the specified microsecond
time_until_microsecond = 1000000 - current_microsecond
#get the total time until the specified time
total_time = time_until_hour * 3600 + time_until_minute * 60 + time_until_second + time_until_microsecond / 1000000
#get the total time in minutes
total_time_in_minutes = total_time / 60
#return the total time in minutes
return total_time_in_minutes
print(minutes_until(15, 0)) #time now is 2 PM, how long until 15:00 (3PM)? = 60 minutes
You can try:
from datetime import datetime
target = '2023-01-01 00:00:00'
t = datetime.strptime(target, '%Y-%m-%d %H:%M:%S')
now = datetime.now()
print(f'{(t-now).total_seconds()/60:.0f} minutes')
output: 104445 minutes

Problem with calculating how many times some date has passed midnight

I would like to ask for a help. I am a beginner when it comes to Python. I try to write a function, that sums up together two "times" and returns new_time and also how many times new_time passed midnight of "start_time"(for example 23:00 and 03:00, new_date is 02:00, and 1 day has passed )
Thank you really much in advance
from datetime import datetime, timedelta
def add_time(start_time: str, time_to_add: str):
start_time_time = datetime.strptime(start_time, "%H:%M")
add_time_time = datetime.strptime(time_to_add, "%H:%M")
new_time = start_time_time + timedelta(minutes=add_time_time.minute, hours=add_time_time.hour)
return f"New time is {new_time.strftime('%H:%M')}, XXX days after"
print(add_time("23:20", "19:20"))
Calculate the dates for start_time_time and new_time. The number of days elapsed will be the difference (in days) between these dates.
I believe there are several ways to extract just the date from a "datetime", but I have just replaced the hours and minutes to zero.
from datetime import datetime, timedelta
def add_time(start_time: str, time_to_add: str):
start_time_time = datetime.strptime(start_time, "%H:%M")
start_date = start_time_time.replace(hour=0, minute=0)
#print(start_date)
add_time_time = datetime.strptime(time_to_add, "%H:%M")
new_time = start_time_time + timedelta(minutes=add_time_time.minute, hours=add_time_time.hour)
new_date = new_time.replace(hour=0, minute=0)
#print(new_date)
days_elapsed = (new_date - start_date).days
return f"New time is {new_time.strftime('%H:%M')}, {days_elapsed} days after"
print(add_time("23:20", "19:20"))
The following code snippets demonstrates how to calculate the number of days after. You can uncomment the print statements to see what these dates actually represent.
Hope this helps.
There are many ways to do this, I done mine in such as way that it should allow you to add example of 100hours, etc. Hope this helps.
from datetime import datetime, timedelta
# Function that adds time HH:mm to a datetime object, adds set Hours and Minutes to start time and returns total days, hours, minutes and seconds passed
def add_time(start_time, time_to_add):
# Add time to start time
start_time = datetime.strptime(start_time, '%H:%M')
# Strip hours, minutes and convert to ms
time_to_add = time_to_add.split(':')
time_to_add = timedelta(hours=int(time_to_add[0]), minutes=int(time_to_add[1]))
finish_time = start_time + time_to_add
# Calculate total days, hours, minutes and seconds passed
total_days = finish_time.day - start_time.day
total_hours = finish_time.hour - start_time.hour
total_minutes = finish_time.minute - start_time.minute
total_seconds = finish_time.second - start_time.second
# Return total days, hours, minutes and seconds passed
return total_days, total_hours, total_minutes, total_seconds
# today + 23 hours + 20 minutes
days, hours, minutes, seconds = add_time("13:13", "25:00")
print(days, hours, minutes, seconds)

How to get time difference in mins in Python

I have a time representation as string in the format:
9:00 am
8:45 pm
e.g., 12-hour, no leading hour 0, am/pm suffix
I need to get the time difference in minutes between then and now.
The time is always in the future, so if it's 09:00 right now, 9:10 am is in 10 mins time and 8:50 am is in 23h, 50m.
I've been playing with strptime but I can't seem to work out how to get my parsed time and now() in a compatible format to do arithmetic on.
something like this, indeed with strptime:
from datetime import datetime
timeString1 = "9:00 am"
timeString2 = "8:45 pm"
format = '%I:%M %p'
datetime1 = datetime.strptime(timeString1, format)
datetime2 = datetime.strptime(timeString2, format)
minutes_diff = (datetime2 - datetime1).total_seconds() / 60.0
print(f"the time difference in minutes is: {minutes_diff}")
this is a variant using datetime.
the format string FMT = "%I:%M %p" is selected according to strptime#time.strftime. and as only datetime objects support differences, i use those only (using datetime.combine).
from datetime import datetime, timedelta
FMT = "%I:%M %p"
TODAY_DATE = datetime.today().date()
def to_datetime(t_str):
return datetime.combine(TODAY_DATE, datetime.strptime(t_str, FMT).time())
NOW = to_datetime("9:00 am")
for t_str in ("9:10 am", "8:45 pm"):
t = to_datetime(t_str)
if t < NOW:
t += timedelta(days=1)
print(f"{t_str}, {(t - NOW)}, {(t - NOW).total_seconds() / 60:.0f} min")
it outputs:
9:10 am, 0:10:00, 10 min
8:45 pm, 11:45:00, 705 min
There are most likely better suited modules for that. But quick and dirty you could do:
def to_minutes(time_str)
time, suffix = time_str.split(" ")
hours, min = time.split(":")
if suffix == "pm":
hours+=12
min = hours*60+min
return min
time1 = to_minuntes("9:00 am")
time2 = to_minutes("8:45 pm")
diff = time2-time1
if diff < 0:
diff+= 24*60
# and if necessary you can do a
hours = diff //60
minutes = diff % 60
if hours > 0:
final_diff = f"{hours}h {minutes}min"
else:
final_diff = f"{minutes}min"

In Python, I want to subtract a time period from within a time period

I want to calculate hours of work during a day, and subtract lunchtime from that time. So somebody clocks in at 8:00, takes a lunch from 12:00 to 12:30, and finish at 16:00.
Lunchtime is configured in a settings table, with start-time and end-time.
So in a nutshell I want to calculate this:
endtime minus starttime = n hours:minutes of work, minus lunchtime (= 12:30 - 12:00 = 30 minutes)
How can I calculate this in Python without making this a hardcoded thing?
Help would be much appreciated
cheers
You can do it with Python datetime:
import datetime as dt
def work_time(start, end, lunch=[], format_='%H:%M'):
""" Calculate the hours worked in a day.
"""
start_dt = dt.datetime.strptime(start, format_)
end_dt = dt.datetime.strptime(end, format_)
if lunch:
lunch_start_dt = dt.datetime.strptime(lunch[0], format_)
lunch_end_dt = dt.datetime.strptime(lunch[1], format_)
lunch_duration = lunch_end_dt - lunch_start_dt
else:
lunch_duration = dt.timedelta(0)
elapsed = end_dt - start_dt - lunch_duration
hours = elapsed.seconds / 3600
return hours
>>> work_time('8:00', '16:00', lunch=['12:00', '12:30'])
7.5
The documentation for datetime provides more information on specific formatting and how to use timedeltas to perform operations on datetime and time objects.

What was midnight yesterday as an epoch time?

I'm trying to get my head around the datetime module. I know the time now as an epoch and the time an event last happened (as an epoch time). What I need to do is figure out whether that event happened between midnight and midnight of yesterday.
t = time.time() # is now
t2 = 1234567890 # some arbitrary time from my log
24 hours ago is t - 86400, but how can I round that up and down to midnight. I'm having real trouble finding a way to get timestamps in and out of datetime or then manipulating a datetime to set the time.
In the Middle of the Night
Generating the last midnight is easy:
from datetime import datetime, time
midnight = datetime.combine(datetime.today(), time.min)
That combines today's date (you can use date() or a datetime() instance, your pick), together with time.min to form a datetime object at midnight.
Yesterday
With a timedelta() you can calculate the previous midnight:
from datetime import timedelta
yesterday_midnight = midnight - timedelta(days=1)
That Was Yesterday
Now test if your timestamp is in between these two points:
timestamp = datetime.fromtimestamp(some_timestamp_from_your_log)
if yesterday_midnight <= timestamp < midnight:
# this happened between 00:00:00 and 23:59:59 yesterday
All Together Now
Combined into one function:
from datetime import datetime, time, timedelta
def is_yesterday(timestamp):
midnight = datetime.combine(datetime.today(), time.min)
yesterday_midnight = midnight - timedelta(days=1)
return yesterday_midnight <= timestamp < midnight:
if is_yesterday(datetime.fromtimestamp(some_timestamp_from_your_log)):
# ...
Midnight at the start of today is:
midnight = (int(time.time() // 86400)) * 86400
so yesterday's midnight is:
midnight = (int(time.time() // 86400)) * 86400 - 86400
Given such a timestamp, you can use divmod to compute the number of days since the epoch (which you don't care about), and how many seconds are leftover (which you do):
days_since, remaining_seconds = divmod(t, 24*3600) # Divide by number of seconds in one day
Then, you subtract the leftover seconds from your original timestamp, which produces midnight
of the current day.
t -= remaining_seconds
Rounding up is as simple as shifting your target timestamp forward exactly one day before rounding down.
tomorrow_t = t + 24 * 3600
days_since, remaining_seconds = divmod(tomorrow_t, 24*3600)
t = tomorrow_t - remaining_seconds
To get the specific timezone's midnight timestamp:
from datetime import datetime
import pytz
TZ = "Asia/Shanghai"
datetime.now(pytz.timezone(TZ)).replace(hour=0, minute=0, second=0, microsecond=0).timestamp()
In my estimation, many date and time manipulations are easier to do, and to understand, using the arrow library. This is one of them.
Create an arbitrary date and time.
>>> import arrow
>>> arbitrary = arrow.get(2017,8,16,11,5)
Calculate midnight_yesterday: first, midnight of arbitrary as its 'day' floor; then shift this back by one day. Display the result.
>>> midnight_yesterday = arbitrary.floor('day').shift(days=-1)
>>> midnight_yesterday
<Arrow [2017-08-15T00:00:00+00:00]>
Use timestamp for the desired overall result, for Python 3.3+.
>>> midnight_yesterday.datetime.timestamp()
1502755200.0
Or use this expression for Python 2.7. (Credit: https://stackoverflow.com/a/11743262/131187 for the latter two expressions.)
>>> (midnight_yesterday-arrow.get(1970,1,1)).total_seconds()
1502755200.0
You can use this code:
import time
seconds_of_day = 24 * 60 * 60 # 86400
last_midnight = (round(time.time()) // seconds_of_day) * seconds_of_day
yesterday_last_midnight = last_midnight - seconds_of_day
import time
start_str = time.strftime( "%m/%d/%Y" ) + " 00:00:00"
end_str = time.strftime( "%m/%d/%Y ") + " 23:59:59"
start_ts = int( time.mktime( time.strptime( start_str, "%m/%d/%Y %H:%M:%S" ) ) )
end_ts = int( time.mktime( time.strptime( end_str, "%m/%d/%Y %H:%M:%S" ) ) )
print (start_ts) # timestamp today at 00:00:00
print (end_ts) # timestamp today at 23:59:59
# 1552435200
# 1552521599
Source Python get unix epoch for today’s midnight and today’s 23:59:59 (start of day, end of day)

Categories

Resources