Swift - Hours/Minutes/Seconds to Integer conversion - ios

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

Related

Won't print calculations for hours and minutes to seconds

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)

Trying to create timeAgo function for news feed, but it just displays "0 seconds ago"

I am trying to create a newsfeed that shows something like "3 minutes ago" or whatever the current time is from when they posted. However, very time I post to the newsfeed it just says 0 seconds ago. Could someone please tell me where my code is messing up.
func timeAgoDisplay() -> String {
let secondsAgo = Int(Date().timeIntervalSinceNow)
if secondsAgo < 60 {
return "\(secondsAgo) seconds ago"
} else if secondsAgo < 60 * 60 {
return "\(secondsAgo / 60) minutes ago"
} else if secondsAgo < 60 * 60 * 24 {
return "\(secondsAgo / 60 / 60) hours ago"
}
return "\(secondsAgo / 60 / 60 / 24) days ago"
}
Since Date() returns the current date, you need to specify an input value with the type Date for the function and use the timeIntervalSinceNow property of that value.
Also, you need to multiply the value you're getting with -1, since the value will be negative if the input date is from the past.
func timeAgoDisplay(_ date: Date) -> String {
let secondsAgo = Int(date.timeIntervalSinceNow) * -1
[...]
}

lua number format not working

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.

How to customize my timer display to show only minutes and seconds?

My Timer is displaying Minutes and Hours, but once it gets to 60 minutes it restarts from 0 Minute.
Should I get rid of the modulo ( % 60 ) for minutes.
I would like my timer to display for ex: 80:45 ( basically not stopping at 60 min once it reaches 1 hour)
var min = 0
var sec = 0
func stringFromTimeInterval(interval: NSTimeInterval) -> String {
let interval = Int(interval)
let seconds = interval % 60
let minutes = (interval / 60) % 60
//let hours = (interval / 3600)// I don't need the hours
return String(format: "%02d:%02d",minutes, seconds)
}
% 60 means that it will spit out a minutes value that is the remainder when divided by 60(minutes). This is most probably because for time in the form hh:mm, you want it to go from 5:59 to 6:00, not 5:60. So changing the following line will give you what you seek.
let minutes = (interval / 60) % 60 -> let minutes = interval / 60

Converting MPH to minute miles

I'm attempting to convert MPH into minute miles. I'm currently running code to do this by doing 60 / the miles per hour which gives me the result in minute miles.
For example 60/8mph = 7.5
However the answer I get I need to convert into minutes and seconds so that I would have 7 minutes 30 seconds. Is there a way I can get the numbers after the decimal point so I can multiply it by 60 to convert it to seconds, then add it back to the minutes.
You can use remainder,
double remainder = fmod(a_double, another_double);
should include <math.h>
Well, I don't know whether there is an existing class that handles this, but to answer your specific question, the fractional part of the decimal (mantissa?) would be:
((60 % 8) / 8.0f)
You can multiply that by 60.
Do it in seconds...
3600/8 = 450
450/60 = 7 remainder 30
= 7:30
It's pretty simple, you're on the right path actually.
What you need to do is:
Get Minutes
Get Seconds
Convert seconds from int to real time (0.5 to 30, etc..)
Add seconds to minutes
Get minutes by casting it to an Integer:
int minutes = 60/8;
Get seconds by using the remainder:
float seconds = 60%8;
Convert seconds to real time:
int realSeconds = seconds * 60;
Now get result back by adding both:
int totalSeconds = minuts + realSeconds;
Here's a little function that does it (typed directly to browser, probably won't compile)
#include <math.h>
int getMinuteMiles(float mph){
int minutes = 60/mph;
double seconds = fmod(60, mph);
int realSeconds = seconds * 60;
return minutes+realSeconds;
}

Resources