in Ruby, conversion of float integer into %H %M %S time - ruby

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"

Related

Ruby - Get time at start of next minute

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 to convert minutes to days, hours, and minutes?

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)

how can I convert milliseconds in String format to HH:MM:SS format in Ruby in under 3 lines of code?

#scores_raw.each do |score_raw|
# below is code if time was being sent in milliseconds
hh = ((score_raw.score.to_i)/100)/3600
mm = (hh-hh.to_i)*60
ss = (mm-mm.to_i)*60
crumbs = [hh,mm,ss]
sum = crumbs.first.to_i*3600+crumbs[1].to_i*60+crumbs.last.to_i
#scores << {:secs => sum, :hms => hh.round.to_s+":"+mm.round.to_s+":"+ss.round.to_s}
#scores_hash << {:secs => sum, :hms => hh.round.to_s+":"+mm.round.to_s+":"+ss.round.to_s}
# milliseconds case end
end
That's my current code but I hate it. It's looks messy. It doesn't just look great at all. Maybe someone whose an expert in ruby could tell how to do this by chaining collects, reduces etc and making it look good?
Time class ruby provides provides at function to get time from seconds. Use this it will cure.
miliseconds = 32290928
seconds = miliseconds/1000
Time.at(seconds).strftime("%H:%M:%S")
OR to get utc time
#Get UTC Time
Time.at(seconds).utc.strftime("%H:%M:%S")
You can wrap this in a helper method:
def format_milisecs(m)
secs, milisecs = m.divmod(1000) # divmod returns [quotient, modulus]
mins, secs = secs.divmod(60)
hours, mins = mins.divmod(60)
[secs,mins,hours].map { |e| e.to_s.rjust(2,'0') }.join ':'
end
format_milisecs 10_600_00
=> "03:13:20"
Nice solution given by #Mike Woodhouse :
Use divmod :
t = 270921000
ss, ms = t.divmod(1000) #=> [270921, 0]
mm, ss = ss.divmod(60) #=> [4515, 21]
hh, mm = mm.divmod(60) #=> [75, 15]
dd, hh = hh.divmod(24) #=> [3, 3]
puts "%d days, %d hours, %d minutes and %d seconds" % [dd, hh, mm, ss]
#=> 3 days, 3 hours, 15 minutes and 21 seconds
Answer is how to convert 270921sec into days + hours + minutes + sec ? (ruby)

Ruby julian seconds

I've looked at Time, DateTime, and Date.
What's the easiest way to generate the current number of julian seconds?
Thanks!
require 'date'
secs_after_midnight = Time.now.to_i % 86400
today = Date.today
puts "Julian seconds : #{today.jd * 86400 + secs_after_midnight}"
puts "Astronomical Julian seconds : #{today.ajd.to_i * 86400 + secs_after_midnight}"
puts "Astronomical Modified Julian seconds: #{today.amjd.to_i * 86400 + secs_after_midnight}"
Output
Julian seconds : 212172639571
Astronomical Julian seconds : 212172553171
Astronomical Modified Julian seconds: 4812553171
This wikipedia page has some algorithms which can calculate the julian seconds. Whether to trust it or not is another matter but I quickly threw together a ruby-ish way from here.
class Time
def to_julian
unix = to_i
ss = unix % 60
a = (unix - ss) / 60
mm = a % 60
b = (a - mm) / 60
hh = b % 24
u = unix - ss - mm * 60 - hh * 3600
u % 86400 + 2440588
end
end
This will let you do something like
Time.now.to_julian
if you are so inclined.
Hope it helps

Algorithm needed to calculate difference between two times

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.

Resources