I'm making a program that calculates minutes and hours into seconds but it won't print, would appreciate any help
enter_hours = int(input("Please enter number of hours: "))
enter_minutes = int(input("Please enter number of minutes: "))
def CalculateSeconds():
hours = enter_hours * 3600
minutes = enter_minutes * 60
return(hours, minutes)
Figured it out
def CalculateSeconds(enter_hours, enter_minutes):
hours = enter_hours * 3600
minutes = enter_minutes * 60
return(hours, minutes)
seconds = CalculateSeconds(enter_hours=int(input("Enter first number: ")),
enter_minutes=int(input("Enter second number: ")))
print(seconds)
Modified Question.
My fitness app will calculate the number of calories burned based on a calculated value for each second. I have a timer that will allow the app to pick back up should it. I can't get the running sum to continue calculating when the app goes into the background. I tried to place the running sum inside of a DispatchQueue but not getting the sum as expected. Any guidance is greatly appreciated.
Here's the code I have placed inside the function that updates the timer.
//MARK: - Update Timer Label
func updateTimerLabel() {
interval = -Int(timerStartDate.timeIntervalSinceNow)
time = interval
let hours = interval / 3600
let minutes = interval / 60 % 60
let seconds = interval % 60
print("Current interval = \(interval)")
timerLabel.text = String(format:"%02i:%02i:%02i", hours, minutes, seconds)
DispatchQueue.global(qos: .background).async {
if self.activityArray[self.currentArrayRow].2 <= 4.5 {
self.cps = self.activityArray[self.currentArrayRow].2 * Double(self.user.userWeightInKilo) / 3600
self.runningCPS = self.runningCPS + self.cps
print("MET \(self.activityArray[self.currentArrayRow].2) <= 4.5 * KG (\(Double(self.user.userWeightInKilo))) * HR (\(Double(self.user.userHeartRate))) / MaxHR (\(Double(self.user.maxHeartRate)) * interval \(Double(self.interval)) / 3600. Gives a cps 0f \(self.cps) and a runningCPS of \(self.runningCPS) ")
} else {
self.cps = self.activityArray[self.currentArrayRow].2 * Double(self.user.userWeightInKilo) * Double(self.user.userHeartRate) / Double(self.user.maxHeartRate) / 3600
self.runningCPS = self.runningCPS + self.cps
print("MET \(self.activityArray[self.currentArrayRow].2) > 4.5 * KG (\(Double(self.user.userWeightInKilo))) * HR (\(Double(self.user.userHeartRate))) / MaxHR (\(Double(self.user.maxHeartRate)) * interval \(Double(self.interval)) / 3600. Gives a cps 0f \(self.cps) and a runningCPS of \(self.runningCPS) ")
}
}
activeLabel.text = String(format: "%0.1f", runningCPS) + " Calories Burned"
}
I have Hours / Minutes / Seconds that I would like converted into an integer
7 Hours 30 Minutes 5 Seconds
You could use split():
import Foundation
func getSecondsFromString(timeString: String) -> (Int) {
let timeParts = timeString.replacingOccurrences(of: "[^0-9]", with: " ", options: [.regularExpression])
.split(separator: " ")
.map{Int($0)!}
return timeParts[0] * 3600 + timeParts[1] * 60 + timeParts[2]
}
print(getSecondsFromString(timeString: "7 Hours 30 Minutes 5 Seconds"))
Output:
27005
If you have the time difference between two dates in seconds using timeIntervalSince then you can directly convert this into hours as a double by doing
let hours: Double = elapsedTime / 3600 // 7.501388...
In your examples this is 7.5 which you can then multiply with the hourly rate. If you for some reason only want to use full hours you can either round to the nearest full hour
let fullHours = round(hours) // 8.0
or if you want to truncate minutes and keep the hour then you can do a integer division from the start
let hours: Int = elapsedTime / 3600 // 7
I have the following function:
function timestamp(duration)
local hours = duration / 3600
local minutes = duration % 3600 / 60
local seconds = duration % 60
return string.format("%02d:%02d:%02.03f", hours, minutes, seconds)
end
when the duration is 4.404 sec it returns 00:00:4.404
what is am looking for is 00:00:04.404
It should be:
string.format("%02d:%02d:%06.3f", hours, minutes, seconds)
Field width contains all characters of the number, including point and fraction.
This question already has answers here:
How to convert milliseconds into human readable form?
(22 answers)
Closed 6 years ago.
I need to convert an milliseconds into Days, Hours, Minutes Second.
ex: 5 Days, 4 hours, 13 minutes, 1 second.
Thanks
if you don't want to do the calculation by yourself, you could go for such a solution.
I, however, know that is a kinda costly solution, so you need to be aware of potential performance issues in runtime – depending on how frequently you intend to invoke this.
NSTimeInterval _timeInSeconds = 123456789.123; // or any other interval...;
NSCalendar *_calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
NSCalendarUnit _units = NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *_components = [_calendar components:_units fromDate:[NSDate date] toDate:[NSDate dateWithTimeIntervalSinceNow:_timeInSeconds] options:kNilOptions];
NSLog(#"%ld Days, %ld Hours, %ld Minutes, %ld Seconds", _components.day, _components.hour, _components.minute, _components.second);
You can write your own function like this:
import UIKit
let miliseconds: Int = 24 * 3600 * 1000 + 3700 * 1000
// 1 day and 1 hour 1 minute 40 seconds
func convertTime(miliseconds: Int) -> String {
var seconds: Int = 0
var minutes: Int = 0
var hours: Int = 0
var days: Int = 0
var secondsTemp: Int = 0
var minutesTemp: Int = 0
var hoursTemp: Int = 0
if miliseconds < 1000 {
return ""
} else if miliseconds < 1000 * 60 {
seconds = miliseconds / 1000
return "\(seconds) seconds"
} else if miliseconds < 1000 * 60 * 60 {
secondsTemp = miliseconds / 1000
minutes = secondsTemp / 60
seconds = (miliseconds - minutes * 60 * 1000) / 1000
return "\(minutes) minutes, \(seconds) seconds"
} else if miliseconds < 1000 * 60 * 60 * 24 {
minutesTemp = miliseconds / 1000 / 60
hours = minutesTemp / 60
minutes = (miliseconds - hours * 60 * 60 * 1000) / 1000 / 60
seconds = (miliseconds - hours * 60 * 60 * 1000 - minutes * 60 * 1000) / 1000
return "\(hours) hours, \(minutes) minutes, \(seconds) seconds"
} else {
hoursTemp = miliseconds / 1000 / 60 / 60
days = hoursTemp / 24
hours = (miliseconds - days * 24 * 60 * 60 * 1000) / 1000 / 60 / 60
minutes = (miliseconds - days * 24 * 60 * 60 * 1000 - hours * 60 * 60 * 1000) / 1000 / 60
seconds = (miliseconds - days * 24 * 60 * 60 * 1000 - hours * 60 * 60 * 1000 - minutes * 60 * 1000) / 1000
return "\(days) days, \(hours) hours, \(minutes) minutes, \(seconds) seconds"
}
}
convertTime(miliseconds)
//result is "1 days, 1 hours, 1 minutes, 40 seconds"
try this
NSTimeInterval time = <timein ms>;
NSInteger days = time / (24 * 60 * 60);
NSInteger hours = (time / (60 * 60)) - (24 * days);
NSInteger minutes =(time / 60) - (24 * 60 * days) - (hours * 60);
NSInteger seconds = (lroundf(time) % 60);
I have write the easy code to do this in both Objective-c and Swift:
Swift
var milliseconds : double_t = 568569600;
milliseconds = floor(milliseconds/1000);
let seconds : double_t = fmod(milliseconds, 60);
let minutes : double_t = fmod((milliseconds / 60) , 60);
let hours : double_t = fmod((milliseconds / (60*60)), 60);
let days : double_t = fmod(milliseconds / ((60*60)*24), 24);
NSLog("seconds : %.f minutes : %.f hours : %.f days : %.f", seconds, minutes, hours, days);
Output - seconds : 9 minutes : 56 hours : 38 days : 7
Objective
double milliseconds = 568569600;
milliseconds = milliseconds/1000;
float seconds = fmod(milliseconds, 60);
float minutes = fmod((milliseconds / 60) , 60);
float hours = fmod((milliseconds / (60*60)), 60);
float days = fmod(milliseconds / ((60*60)*24), 24);
NSLog(#"seconds : %.f minutes : %.f hours : %.f days : %.f ", seconds, minutes, hours, days);
Output - seconds : 10 minutes : 56 hours : 38 days : 7