I am a beginner to Python so I do not know a lot of terms or anything really. Can I ask on how to convert minutes to hours and minutes
EX: 75 minutes ->0 days, 1 hour, 15 minutes
print("Welcome to the Scheduler!")
print("What is your name?")
name = input()
print("How many chocolates are there in the order?")
chocolates = input()
print("How many chocolate cakes are there in the order?")
chocolate_cakes = input()
print("How many chocolate ice creams are in the order?")
chocolate_ice_creams = input()
total_time = float(chocolates) + float(chocolate_cakes) + float(chocolate_ice_creams)
print("Total Time:")
print("How many minutes do you have before the order is due?")
minutes = input()
extra_time = float(minutes) - float(total_time)
print("Your extra time for this order is", extra_time)
time = extra_time // 60
print("Thank you,", name)
Well if you're given an input in minutes that is greater than equal to 1440 minutes, then you have at least a day. So to handle this (and the other aspects of time) we can use modulus (%).
days = 0
hours = 0
mins = 0
time = given_number_of_minutes
days = time / 1440
leftover_minutes = time % 1440
hours = leftover_minutes / 60
mins = time - (days*1440) - (hours*60)
print(str(days) + " days, " + str(hours) + " hours, " + str(mins) + " mins. ")
This should work.
# Python Program to Convert seconds
# into hours, minutes and seconds
def convert(seconds):
seconds = seconds % (24 * 3600)
hour = seconds // 3600
seconds %= 3600
minutes = seconds // 60
seconds %= 60
return "%d:%02d:%02d" % (hour, minutes, seconds)
# Driver program
n = 12345
print(convert(n))
method = a
import datetime
str(datetime.timedelta(seconds=666))
'0:11:06'
method = b
def convert(seconds):
seconds = seconds % (24 * 3600)
hour = seconds // 3600
seconds %= 3600
minutes = seconds // 60
seconds %= 60
return "%d:%02d:%02d" % (hour, minutes, seconds)
from datetime import datetime
day = minutes = hours = 0
day = datetime.now ()
day = day.day
minutes = datetime.now ()
minutes = minutes.minute
hours = datetime.now ()
hours = hours.hour
print ("It's day" + str (day) + "and it's" + str (minutes) + "minutes and" + str (hours) + "hours.")
This code above does not work I started to make my own version of how this code could help you remember I am an amateur is true
from datetime import datetime
day = minutes = hours = 0
time = datetime.now().minute
days = time / 1440
leftover_minutes = time % 1440
hours = leftover_minutes / 60
mins = time - (days*1440) - (hours*60)
print(str(days) + " days, " + str(hours) + " hours, " + str(mins) + " mins. ")
You actually have to round down values in order to get integers.
import math
def transform_minutes(total_minutes):
days = math.floor(total_minutes / (24*60))
leftover_minutes = total_minutes % (24*60)
hours = math.floor(leftover_minutes / 60)
mins = total_minutes - (days*1440) - (hours*60)
#out format = "days-hours:minutes:seconds"
out = '{}-{}:{}:00'.format(days, hours, mins)
return out
n = int (input())
day = int (n // 1440)
hours = int (n % 1440) // 60
mins = int (n % 1440) % 60
print(day)
print(hours)
print(mins)
Related
I have 3 variables (ADG,DR,TC) that I want to count for every single time 45 mins and 2.5 hrs pass, respectively for each 3 variable. I have this code to create a 24 hrs timer and I need to add these variables (and their counts) into while when_to_stop > 0: loop. Any recommendations? Thank you!
import time
while True:
uin = input(">>")
try:
when_to_stop = abs(int(uin))
except KeyboardInterrupt:
break
except:
print("Not a number!")
while when_to_stop > 0:
m, s = divmod(when_to_stop, 60)
h, m = divmod(m, 60)
time_left = str(h).zfill(2) + ":" + str(m).zfill(2) + ":" + str(s).zfill(2)
print(time_left + "\r", end="" )
time.sleep(0.001)
when_to_stop -= 1
# ADG - Auto Droplet Generator (45 mins)
# DR - Droplet Reader (time same as TC)
# TC - Ther mal cycler (2.5 hrs)
"""
Added an elapsed seconds counter.
When the amount of elapsed seconds is evenly divisible by (45 * 60 seconds), 45 minutes have passed, and the ADG variable is incremented by one each time this happens.
When the amount of elapsed seconds is evenly divisible by (150 * 60 seconds), 2.5 hours have passed, and the TC variable is incremented by one each time this happens. The DR variable acts the same way (time same as TC) so it has the same value as the TC variable.
import time
elapsed = 0
ADG = 0
DR = 0
TC = 0
while True:
uin = input(">>")
try:
when_to_stop = abs(int(uin))
except KeyboardInterrupt:
break
except:
print("Not a number!")
while when_to_stop > 0:
m, s = divmod(when_to_stop, 60)
h, m = divmod(m, 60)
time_left = str(h).zfill(2) + ":" + str(m).zfill(2) + ":" + str(s).zfill(2)
print(time_left + "\r", end="" )
time.sleep(0.001)
when_to_stop -= 1
elapsed += 1
#Auto Droplet Generator (45 mins)
if (elapsed % (45*60) == 0):
ADG += 1
# DR - Droplet Reader (time same as TC)
if (elapsed % (150*60) == 0):
DR += 1
# TC - Ther mal cycler (2.5 hrs)
if (elapsed % (150*60) == 0):
TC += 1
I'm looking for a concise way to get a Ruby Time object representing the top of the next minute (and hour/day/month/year, if possible). I want this to work in a pure Ruby environment, so the Rails function Time.change or similar doesn't fit the bill.
At first this seems simple - just add 1 to Time.now, but there are edge cases where if, for example, you try to instantiate a Time object with Time.now.min + 1 when the current minute is 59, you get an ArgumentError: min out of range. This goes for hour, day, and month as well.
I have some lengthy code that does the job. It's ugly, but I'm just experimenting:
def add_minute
now = Time.local
year = now.year
month = now.month
day = now.day
hour = now.hour
min = now.min
if now.min == 59
if now.hour == 23
if now.day == Date.civil(now.year, now.month, -1).day
if month == 12
year = year + 1
month = 1
day = 1
hour = 0
min = 0
else
month = now.month + 1
day = 1
hour = 0
min = 0
end
else
day = now.day + 1
hour = 0
min = 0
end
else
hour = now.hour + 1
min = 0
end
else
min = now.min + 1
end
Time.local year, month, day, hour, min, 0
end
This seems absurdly verbose for what seems like it should be a simple or built-in task, but I haven't found a native Ruby solution. Does one exist?
You could convert the Time object to UNIX epoch time (seconds since 1970) using #to_i, add 60 s, and then convert back to a Time object.
time_unix = Time.now.to_i
time_unix_one_min_later = time_unix + 60
time_one_min_later = t = Time.at(time_unix_one_min_later)
time_one_min_later_rounded_down = Time.new(t.year, t.month, t.day, t.hour, t.min)
EDIT: Even shorter - you can just add integer seconds to Time.now directly:
time_one_min_later = t = Time.now + 60
time_one_min_later_rounded_down = Time.new(t.year, t.month, t.day, t.hour, t.min)
EDIT 2: One-liner - just subtract Time.now.sec:
time_one_min_later_rounded_down = Time.now + 60 - Time.now.sec
Other option, given one second to midnight:
require 'time'
now = Time.strptime('2018-12-31 23:59:59', '%Y-%m-%d %H:%M:%S')
Within one minute:
Time.at(now + 60) #=> 2019-01-01 00:00:59 +0100
Time.at(now + 60 - now.sec) #=> 2019-01-01 00:00:00 +0100
You get: # HAPPY NEW YEAR!
Ruby has built in methods for adding months (>>) and days (+). A year is 12 months, and an hour is 1/24th of a day.
require 'date'
def add_time(time, year: 0 ,month: 0, day: 0, hour: 0, minute: 0)
time >>= 12*year
time >>= month
time += day
time += Rational(hour,24) # or (hour/24.0) if you dislike rationals
time += Rational(minute, 24*60) # (minute/24.0*60) if you dislike rationals
end
p t = DateTime.now
p add_time(t, year: 1, minute: 30)
Not that clean without ActiveSupport:
new_date = (DateTime.now + 1.to_f / (60*24))
DateTime.new(new_date.year, new_date.month, new_date.day, new_date.hour, new_date.minute)
We can make this calculation easier to understand by getting the current number of seconds we are through the day. (optional)
DateTime.current.to_i gives us the number of seconds since 1970
DateTime.current.to_i - DateTime.current.beginning_of_day.to_i gives us the number of seconds since the start of the day.
(((number_of_seconds_through_the_day + 60)/60) * 60) gives us the number of seconds we will be at when the next minute starts
Then we subtract the two to give us the number of seconds until the top of the next minute.
If we want the exact time at start of the next minute then we can do:
DateTime.current + seconds_until_start_of_the_next_minute.seconds
def seconds_until_start_of_the_next_minute
number_of_seconds_through_the_day = DateTime.current.to_i - DateTime.current.beginning_of_day.to_i
number_of_seconds_through_the_day_at_next_minute = (((number_of_seconds_through_the_day + 60)/60) * 60)
seconds_until_next_minute_starts = number_of_seconds_through_the_day_at_next_minute - number_of_seconds_through_the_day
return seconds_until_next_minute_starts
end
How do you convert a float, say 13.5, to the corresponding 24-hour time %H:%M:%S?
(13.5 would be 13:30:00, 8.25 would be 8:15:00)
I'm still figuring the Time class...it confuses me
sec = (13.5 * 3600).to_i
min, sec = sec.divmod(60)
hour, min = min.divmod(60)
"%02d:%02d:%02d" % [hour, min, sec] # => "13:30:00"
#Time of your with two digit in minutes like 5 to 50
time_val="13.50"
#split by "."
split_val=time_val.split('.')
#get a hours
hour_val=split_val[0]
#get a minutes
min_val= (100*60)/split_val[1].to_i
if(min_val == 60)
hour_val +=1
min_val=0
end
#it's concat a hours and minutes then it's final result
final_time= hour_val.to_s + ":" + min_val + ":00"
I have an hour selection drop down 0-23 and minutes selection drop down 0-59 for Start time and End time respectively (so four controls).
I'm looking for an algorithm to calculate time difference using these four values.
Since they're not stored in fancy date/time selection controls, I don't think I can use any standard date/time manipulation functions.
How do I calculate the difference between the two times?
This pseudo-code gives you the algorithm to work out the difference in minutes. It assumes that, if the start time is after the end time, the start time was actually on the previous day.
const MINS_PER_HR = 60, MINS_PER_DAY = 1440
startx = starthour * MINS_PER_HR + startminute
endx = endhour * MINS_PER_HR + endminute
duration = endx - startx
if duration < 0:
duration = duration + MINS_PER_DAY
The startx and endx values are the number of minutes since midnight.
This is basically doing:
Get number of minutes from start of day for start time.
Get number of minutes from start of day for end time.
Subtract the former from the latter.
If result is negative, add number of minutes in a day.
Don't be so sure though that you can't use date/time manipulation functions. You may find that you could easily construct a date/time and calculate differences with something like:
DateTime startx = new DateTime (1, 1, 2010, starthour, startminute, 0);
DateTime endx = new DateTime (1, 1, 2010, endhour , endminute , 0);
Integer duration = DateTime.DiffSecs(endx, startx) / 60;
if (duration < 0)
duration = duration + 1440;
although it's probably not needed for your simple scenario. I'd stick with the pseudo-code I gave above unless you find yourself doing some trickier date/time manipulation.
If you then want to turn the duration (in minutes) into hours and minutes:
durHours = int(duration / 60)
durMinutes = duration % 60 // could also use duration - (durHours * 60)
This will compute duration in minutes including the year as factor
//* Assumptions:
Date is in Julian Format
startx = starthour * 60 + startminute
endx = endhour * 60 + endminute
duration = endx - startx
if duration <= 0:
duration = duration + 1440
end-if
if currday > prevday
duration = duration + ((currday-preday) - 1 * 1440)
end-if
First you need to check to see if the end time is greater than or equal to the start time to prevent any problems. To do this you first check to see if the End_Time_Hour is greater than Start_Time_Hour. If they're equal you would instead check to see if End_Time_Min is greater than or equal to Start_Time_Min.
Next you would subtract Start_Time_Hour from End_Time_Hour. Then you would subtract Start_Time_Min from End_Time_Min. If the difference of the minutes is less than 0 you would decrement the hour difference by one and add the minute difference to 60 (or 59, test that). Concat these two together and you should be all set.
$start_time_hr = 5;
$start_time_mi = 50;
$end_time_hr = 8;
$end_time_mi = 30;
$diff = (($end_time_hr*60)+$end_time_mi) - (($start_time_hr*60)+$start_time_mi);
$diff_hr = (int)($diff / 60);
$diff_mi = (int)($diff) - ($diff_hr*60);
echo $diff_hr . ':' . $diff_mi;
simple equation should help:
mindiff = 60 + endtime.min - starttime.min
hrdiff = ((mindiff/60) - 1) + endtime.hr - starttime.hr
This gives you the duration in hours and minutes
h1 = "hora1"
m1 "min1"
h2 "hora2"
m2 = "min2"
if ( m1 > m2)
{
h3 = (h2 - h1) - 1;
}
else
{
h3 = h2 - h1;
}
m1 = 60 - m1;
if (m1 + m2 >= 60)
{
m3 = 60 - (m1 + m2);
} else if (m3 < 0)
{
m3 = m3 * -1;
}
else
{
m3 = m1 + m2;
}
System.out.println("duration:" + h3 + "h" + m3 + "min");
If you have a function that returns the number of days since some start date (e.g. dayssince1900) you can just convert both dates to seconds since that start date, do the ABS(d1-d2) then convert the seconds back to whatever format you want e.g. HHHH:MM:SS
Simple e.g.
SecondsSince1900(d)
{
return dayssince1900(d)*86400
+hours(d)*3600
+minutes(d)*60
+seconds(d);
}
diff = ABS(SecondsSince1900(d1)-SecondsSince1900(d2))
return format(diff DIV 3600)+':'+format((diff DIV 60) MOD 60)+':'+format(diff MOD 60);
Hum: Not that simple if you have to take into account the leap seconds astronomers are keen to put in from time to time.
Someone does 20 Hours 42 Minutes & 16 Seconds in one shift totaling 74536 seconds.
How do I get the hours from number of seconds the person has done for that shift?
20 * 60 * 60 = 72000
42 * 60 = 2520
16 = 16
+ -----
Total = 74536
____________________________
Total % 60 = Seconds (16)
Total % ? = Minutes (42)
Total % ? = Hours (20)
Tried 84600 already; turns out when a number is lower the modulus, it really is not very helpful, and something I am going to have to catch should someone only sign in for a few seconds ...
You need to use both modulus and division:
t = seconds_in_shift;
secs = t % 60;
t /= 60;
mins = t % 60;
t /= 60;
hour = t;
Or:
secs = ttime % 60;
mins = (ttime / 60) % 60;
hour = ttime / 3600;
One other option uses the div() function from Standard C (<stdlib.h>):
div_t v1 = div(ttime, 60);
div_t v2 = div(v1.quot, 60);
After that, v1.rem contains the seconds; v2.rem contains the minutes, and v2.quot contains the hours based on the number of seconds originally in ttime.
Based on Jonathan's reply, I believe the accurate answer should be this…
$total_time = 61000;
$sec = $total_time % 60;
$total_time = ($total_time - $sec) / 60;
$min = $total_time % 60;
$hour = ($total_time - $min) / 60;
echo "$hour hours, $min minutes and $sec seconds";
But if your server has PHP7 installed, you can use the intdiv() function. An integer division in which the remainder is discarded.
// $hour = ($total_time - $min) / 60; old code but still works
$hour = intdiv($total_time, 60); // PHP7 and above code