Subtract time

98 views
Skip to first unread message

Khan

unread,
Aug 28, 2012, 10:18:46 AM8/28/12
to rubyonra...@googlegroups.com
Hey everyone

            i am trying to subtract two different times.i am getting right when i have two fixed times.
here is my code


def hour_min(last,first)
   
    difference = last - first
    seconds    =  difference % 60
    difference = (difference - seconds) / 60
    minutes    =  difference % 60
    difference = (difference - minutes) / 60
    hours      =  difference % 24

    return hours,minutes
   
  end
if last =9:30 PM and first = 10:52 i get after subtraction 10 hours : 38 mins but the problem is with Time.now
if last = Time.now and  first = 10:30 am it is give improper output



this is my view
<% hour_min =  hour_min(Time.now.to_i,cin.first.to_i)%>
 <%="#{hour_min[0]}Hour : #{hour_min[1]}Min"%>

My problem is if i have two fixed timings i am getting correct but when i have to calculate with Time.now i am getting wrong answer can any one help me out



Cheers


Colin Law

unread,
Aug 28, 2012, 10:35:04 AM8/28/12
to rubyonra...@googlegroups.com
On 28 August 2012 15:18, Khan <emailtosa...@gmail.com> wrote:
> Hey everyone
>
> i am trying to subtract two different times.i am getting right
> when i have two fixed times.
> here is my code
>
>
> def hour_min(last,first)
>
> difference = last - first
> seconds = difference % 60
> difference = (difference - seconds) / 60
> minutes = difference % 60
> difference = (difference - minutes) / 60
> hours = difference % 24
>
> return hours,minutes
>
> end
> if last =9:30 PM and first = 10:52 i get after subtraction 10 hours : 38
> mins but the problem is with Time.now
> if last = Time.now and first = 10:30 am it is give improper output
>
>
>
> this is my view
> <% hour_min = hour_min(Time.now.to_i,cin.first.to_i)%>

You should not need to call to_i, just pass in the times, the
difference of two times is the difference in seconds.

> <%="#{hour_min[0]}Hour : #{hour_min[1]}Min"%>
>
> My problem is if i have two fixed timings i am getting correct but when i
> have to calculate with Time.now i am getting wrong answer can any one help
> me out

Have a look at the Rails Guide on debugging which will show you
techniques that can be used to debug the code. I would start just by
running the console (rails c) and try calling your method with various
values and see what happens. You can use puts in the method to print
intermediate results and see what is happening.

What results are you getting? Is it a number of hours out? If so
then I guess it is a time zone issue.

Colin

sameena Khan

unread,
Aug 28, 2012, 11:40:47 AM8/28/12
to rubyonra...@googlegroups.com
@colin if i have two timings9:30 PM and  10:52 i got correct output here it is  10 hours : 38mins
 and if i give Time.now as one input and 10:30 AM as another input i got 2 hours : mins


i dont think it is timezone issue because if there are two fixed inputs and if i subtract them with my code i get the correct output the only issue is with Time.now as a input i guess. i wanna calculate the working hours with mins of an employee. As i explained before if the employee checkin and checkout time is there i can able to calculate it and if i wanna show right from his checkin until he checkout every min of his working hours and mins i have to go for Time.now as another input.

On Tue, Aug 28, 2012 at 8:05 PM, Colin Law <cla...@googlemail.com> wrote:
On 28 August 2012 15:18, Khan <emailtosa...@gmail.com> wrote:
> Hey everyone
>
>             i am trying to subtract two different times.i am getting right
> when i have two fixed times.
> here is my code
>
>
> def hour_min(last,first)
>
>     difference = last - first
>     seconds    =  difference % 60
>     difference = (difference - seconds) / 60
>     minutes    =  difference % 60
>     difference = (difference - minutes) / 60
>     hours      =  difference % 24
>
>     return hours,minutes
>
>   end
> if last =9:30 PM and first = 10:52 i get after subtraction 10 hours : 38
> mins but the problem is with Time.now
> if last = Time.now and  first = 10:30 am it is give improper output
>
>
>
> this is my view
> <% hour_min =  hour_min(Time.now.to_i,cin.first.to_i)%>

You should not need to call to_i, just pass in the times, the
difference of two times is the difference in seconds.

If i don't call with to_i the output will be with decimal(i.e., 3.o hours ) to convert them i went with to_i

>  <%="#{hour_min[0]}Hour : #{hour_min[1]}Min"%>
>
> My problem is if i have two fixed timings i am getting correct but when i
> have to calculate with Time.now i am getting wrong answer can any one help
> me out

Have a look at the Rails Guide on debugging which will show you
techniques that can be used to debug the code.  I would start just by
running the console (rails c) and try calling your method with various
values and see what happens.  You can use puts in the method to print
intermediate results and see what is happening.

What results are you getting?  Is it a number of hours out?  If so
then I guess it is a time zone issue.

Colin

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.

Colin Law

unread,
Aug 28, 2012, 11:58:43 AM8/28/12
to rubyonra...@googlegroups.com
On 28 August 2012 16:40, sameena Khan <emailtosa...@gmail.com> wrote:
> @colin if i have two timings9:30 PM and 10:52 i got correct output here it
> is 10 hours : 38mins
> and if i give Time.now as one input and 10:30 AM as another input i got 2
> hours : mins

The best thing is to put in some diagnostic code and work out what is
happening. Remember Time.now is not a time of day it is a date and
time. Hopefully your other times are also full date and time objects.

Colin

keerthi

unread,
Aug 29, 2012, 2:48:19 AM8/29/12
to rubyonra...@googlegroups.com
@colin here i found in console

>> @bio = Biometric.find(36457)
=> #<Biometric id: 36457, rfid: 0, userid: 3, date_time: "2012-08-29 09:30:00", checkin_checkout: nil, forget: nil, reason: nil, entered_by: nil, created_at: "2012-08-29 09:30:00", updated_at: "2012-08-29 09:30:00", RealCheckIn: nil, MACHINENO: nil>
>> @first = @bio.date_time.to_i
=> 1346232600
>> @now = Time.now
=> Wed Aug 29 12:13:46 +0530 2012
>> @time_now = @now.to_i
=> 1346222626
>> @time_now - @first
=> -9974

@first = @bio.date_time
=> Wed, 29 Aug 2012 09:30:00 UTC +00:00
with out converting to to_i

>> @now = Time.now
=> Wed Aug 29 12:16:48 +0530 2012
with out converting to_i

as i found was my date from database is in UTC format and when it comes to Time.now its not UTC


how can i solve it

Cheers

Colin Law

unread,
Aug 29, 2012, 3:49:28 AM8/29/12
to rubyonra...@googlegroups.com
On 29 August 2012 07:48, keerthi <emailtosa...@gmail.com> wrote:
> @colin here i found in console
>
>>> @bio = Biometric.find(36457)
> => #<Biometric id: 36457, rfid: 0, userid: 3, date_time: "2012-08-29
> 09:30:00", checkin_checkout: nil, forget: nil, reason: nil, entered_by: nil,
> created_at: "2012-08-29 09:30:00", updated_at: "2012-08-29 09:30:00",
> RealCheckIn: nil, MACHINENO: nil>
>>> @first = @bio.date_time.to_i
> => 1346232600
>>> @now = Time.now
> => Wed Aug 29 12:13:46 +0530 2012
>>> @time_now = @now.to_i
> => 1346222626
>>> @time_now - @first
> => -9974
>
> @first = @bio.date_time
> => Wed, 29 Aug 2012 09:30:00 UTC +00:00
> with out converting to to_i
>
>>> @now = Time.now
> => Wed Aug 29 12:16:48 +0530 2012
> with out converting to_i
>
> as i found was my date from database is in UTC format and when it comes to
> Time.now its not UTC
>
>
> how can i solve it

First a slight correction. The time in the database is not in UTC
/format/ it is just in UTC. UTC is not a time format it is a time
zone. Assuming that the column type is datetime (look in db/schema.db
to check) it should not be a problem. When you subtract the times it
should allow for the different time zones and give you the correct
difference in seconds, provided that the time in the database is
correct for UTC. If that is not working for you please give an
example of it not working.

If it makes it easier for you to understand what is going on you can
convert Time.now to utc by using Time.now.utc. That will not change
the result when you subtract the utc time in the database of course,
as it is still the same time, just represented in a different time
zone.

Colin

Swati Verma

unread,
Aug 29, 2012, 5:06:36 AM8/29/12
to rubyonra...@googlegroups.com
You can do

(Time.now - Time.mktime(*ParseDate.parsedate((t.created_at).to_s)))

Swati Verma

unread,
Aug 29, 2012, 5:22:33 AM8/29/12
to rubyonra...@googlegroups.com, cla...@googlemail.com
You can do

(Time.now - Time.mktime(*ParseDate.parsedate((t.created_at).to_s))) 

keerthi

unread,
Aug 29, 2012, 6:01:49 AM8/29/12
to rubyonra...@googlegroups.com
@swathi thanks for your reply but, i did not get ur trying to say. what is that Time.mktime and y u want o convert it to string

keerthi

unread,
Aug 29, 2012, 6:18:55 AM8/29/12
to rubyonra...@googlegroups.com
@swathi thanks i got what i need

Cheers

On Wed, Aug 29, 2012 at 2:36 PM, Swati Verma <er.swat...@gmail.com> wrote:

keerthi

unread,
Aug 29, 2012, 6:22:57 AM8/29/12
to rubyonra...@googlegroups.com
i got the answer what i am expecting but can u explain me

(Time.now - Time.mktime(*ParseDate.parsedate((t.created_at).to_s)))
what exactly this line does" Time.mktime(*ParseDate.parsedate((t.created_at).to_s)) "



On Wed, Aug 29, 2012 at 2:36 PM, Swati Verma <er.swat...@gmail.com> wrote:

Colin Law

unread,
Aug 29, 2012, 7:13:32 AM8/29/12
to rubyonra...@googlegroups.com
On 29 August 2012 10:06, Swati Verma <er.swat...@gmail.com> wrote:
> You can do
>
> (Time.now - Time.mktime(*ParseDate.parsedate((t.created_at).to_s)))

But why would you do that?

Colin
Reply all
Reply to author
Forward
0 new messages