»
 

Go Back   ResellerRatings Store Ratings > ResellerRatings Forums > Tech Support

Reply
 
LinkBack Thread Tools Display Modes
Old 10-30-2003, 05:57 PM   #1 (permalink)
Registered User
 
Join Date: Oct 2001
Location: TOO close to Wash DC
Posts: 7,956
vass0922 is on a distinguished road
Ruby - Datediff??

I'm trying to find the number of minutes between two different times

Now the catch is, when you go across from monday to tuesday or whatever

I can do the math easy enough from within a day.. but soon as you go across a new day the math gets messed up

tl = Time.now.localtime
tl.day tl.hour tl.min

This will get me the individual numbers.. but not sure how to get what I need

In vbscript there is a DateDiff function that could do it, is there something similiar in Ruby?
I've googled around but not finding much.

vass0922 is offline   Reply With Quote
Old 10-30-2003, 06:18 PM   #2 (permalink)
Registered User
 
SpookyEddy's Avatar
 
Join Date: Oct 2001
Location: UK
Posts: 3,125
SpookyEddy is on a distinguished road
Would something as simple as this work?...
Code:
#!/usr/bin/ruby

time1 = Time.now.localtime
sleep(5)
time2 = Time.now.localtime

difftime = time2 - time1

puts "We slept for: " + difftime.to_s + " seconds."
No idea if that will do your day change for you, but worth a shot.

Regards

ed
SpookyEddy is offline   Reply With Quote
Old 10-30-2003, 06:32 PM   #3 (permalink)
Registered User
 
Join Date: Oct 2001
Location: TOO close to Wash DC
Posts: 7,956
vass0922 is on a distinguished road
Yes that will do what I want, the problem is I'll be putting the time into a text file, and then later the script will run again and the first variable will be the previous time read from the text file

If you syswrite Time.now.localtime it will put the full string "Thu Oct 30 20:23:09 Eastern Standard Time 2003"
... or I can use Time.now.localtime.to_i and convert it to an int.. but how do you convert that to a time object?

I guess I could try something like


time1 = Time.now.localtime.to_i
sleep(5)
time2 = Time.now.localtime.to_i

then divide that number by 60 to get the minutes...

I do believe that does the day as well, as Time.now.localtime uses the full time string

That may work!
knew it had to be easy, I just gotta learn to think in Ruby
__________________
<< Insert exceedingly large and overly verbose message of how 1337 you are here including full specs of every vehicle you've ever driven and PC you've owned >>

Last edited by vass0922; 10-30-2003 at 06:38 PM.
vass0922 is offline   Reply With Quote
Old 10-30-2003, 06:33 PM   #4 (permalink)
Registered User
 
SpookyEddy's Avatar
 
Join Date: Oct 2001
Location: UK
Posts: 3,125
SpookyEddy is on a distinguished road
I just checked, it does the day as well.
SpookyEddy is offline   Reply With Quote
Old 10-31-2003, 06:35 AM   #5 (permalink)
Registered User
 
SpookyEddy's Avatar
 
Join Date: Oct 2001
Location: UK
Posts: 3,125
SpookyEddy is on a distinguished road
On a related note, if I ever want to log to a file or anything similar I tend to just use the YAML module. That way I can record my log entries in a way that I can easily pass back into my code later but is still human readable.

To give you an idea what I am rambling about heres some sketchy code.
Code:
#!/usr/bin/ruby

require 'yaml'

def log(log_entry)
    log_time = Time.now.localtime
    log_hash = { log_time => log_entry }

    File.open("log.txt", "a") do
        |file|
        file.puts log_hash.to_yaml
    end
end

log("foo")
sleep(2)
log("bar")
Resulting in a log.txt that looks like so...
Code:
ed@Slacker:~/ruby$ cat log.txt 
--- 
2003-10-31 15:57:12.436763 +00:00: foo
--- 
2003-10-31 15:57:14.437038 +00:00: bar
Just an idea anyway

<edit>
I can't even see my own post as its been eaten.
/me shakes fist at the stupid cache system.
</edit>

Later

ed

Last edited by SpookyEddy; 10-31-2003 at 12:04 PM.
SpookyEddy is offline   Reply With Quote
Old 10-31-2003, 09:27 AM   #6 (permalink)
Registered User
 
Join Date: Oct 2001
Location: TOO close to Wash DC
Posts: 7,956
vass0922 is on a distinguished road
Interesting
I will take a better look at it later... thinking I'm going back to bed

Was checking it out on CPAN, that maybe useful indeed

Thanks again!!

--- edit ---
No worries, I can see the post and the edit
__________________
<< Insert exceedingly large and overly verbose message of how 1337 you are here including full specs of every vehicle you've ever driven and PC you've owned >>
vass0922 is offline   Reply With Quote
Old 10-31-2003, 12:16 PM   #7 (permalink)
Registered User
 
SpookyEddy's Avatar
 
Join Date: Oct 2001
Location: UK
Posts: 3,125
SpookyEddy is on a distinguished road
no problem.

It started me thinking about writing a Log class based on the same idea but with additional methods like append, rotate, validate etc for general logging and log maintenance. Hmm or perhaps a sub class of File or something similar.

I have plenty of beer so who knows, I may even write some code this evening

Regards

ed
SpookyEddy is offline   Reply With Quote
Old 10-31-2003, 09:30 PM   #8 (permalink)
Banned
 
Join Date: Sep 2003
Posts: 35
pedantic is on a distinguished road
http://jakarta.apache.org/log4j/docs/
pedantic is offline   Reply With Quote
Reply




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Most Active Discussions

Recent Discussions

All times are GMT -6. The time now is 11:03 PM.