I work with decimal times in Lua and make arithmetical operations on them.
For example 124500+5=124505 (12:45:05)
What formula can avoid 60 digits problem?
124459+5=124504 (not 124464)
How can I resolve it?
You are mixing formation with calculation. The best way is to transform your time "string" in a real number:
12:45:05 -> 12 * 60 * 60 + 45 * 60 + 05 = 45905
The function could look like this:
function time_to_number(t)
return (math.floor(t / 10000) * 60 * 60) + ((math.floor(t / 100) % 100) * 60) + (t % 100)
-- you can also use % 10000 if the hours are limited to two digits
end
Now you can calculate on the seconds.
To format the value back you can use this function
function time_split(t)
local hour = math.floor(t / 3600)
local min = math.floor((t % 3600) / 60)
local sec = (t % 3600) % 60
return hour, min, sec
end
I have used many brackets for readability, which are not all required.
When I run this code in Corona on Windows 7 it instantly crashes. It works fine in ZeroBrane. Any ideas why?
--Stopwatch--
local startTime
function start()
startTime = os.time()
--Start the stop watch--
end
function secondsEllapsed()
--Return the number of seconds since the stop watch was started--
return os.time() - startTime
end
start()
while true do
-- Get the time ellapsed and convert it to hours, minutes and seconds
ellapsed = secondsEllapsed()
hours = math.floor(ellapsed / 3600)
minutes = math.floor((ellapsed - (hours * 3600)) / 60)
seconds = math.floor((ellapsed - (hours * 3600) - (minutes * 60)))
-- Print the time ellapsed to the command line
print(hours .. 'h', minutes .. 'm', seconds .. 's')
end
It crashes probably because you run infinity loop.
Below is an example of a countdown timer for Corona SDK written in LUA.
How would I add days, months and years to this?
local function updateTime()
-- decrement the number of seconds
secondsLeft = secondsLeft - 1
-- time is tracked in seconds. We need to convert it to minutes and seconds
local minutes = math.floor( secondsLeft / 60 )
local seconds = secondsLeft % 60
-- make it a string using string format.
local timeDisplay = string.format( "%02d:%02d", minutes, seconds )
clockText.text = timeDisplay
end
Days (and hours) would be trivial, but what about months and years? Since you have no timestamp telling us of how many seconds left to what, it's hard knowing exactly how many months, depending on the length of the months (28, 29, 30 or 31 days). The same with years if we consider leap years as well. In any case, here's something that might be sufficient:
local SECONDS_IN_HOUR = 60 * 60
local SECONDS_IN_DAY = 24 * SECONDS_IN_HOUR
local SECONDS_IN_MONTH = 30 * SECONDS_IN_DAY -- assuming an average of 30 days per month
local SECONDS_IN_YEAR = 365 * SECONDS_IN_DAY
local years = math.floor((secondsLeft / SECONDS_IN_YEAR) % 365)
local months = math.floor((secondsLeft / SECONDS_IN_MONTH) % 12)
local days = math.floor((secondsLeft / SECONDS_IN_DAY) % 30)
local hours = math.floor((secondsLeft / SECONDS_IN_HOUR) % 24)
local minutes = math.floor((secondsLeft / 60) % 60)
local seconds = secondsLeft % 60
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
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