Newbie question on returning system date and time

Ruby experts:

How does one return system date and time in Ruby? (I’m having trouble finding out how to do this through online references.)

My main goal is to use current system date and time for naming log files. So, for time, inclusion of seconds would be desired.

I suppose I need to “require” the Date, Time, and perhaps Parsedate modules, but I’m not sure where to go from here.

Much obliged!

Thanks.

-Kurt Euler

Todays date -

require “date”
puts Date::today() # Output - 2002-07-09

Current time -

puts Time::now() # Output - Tue Jul 09 10:06:52 Mountain Daylight Time 2002

-Rich

···

----- Original Message -----
From: “Kurt Euler” keuler@portal.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Tuesday, July 09, 2002 9:50 AM
Subject: Newbie question on returning system date and time…

Ruby experts:

How does one return system date and time in Ruby? (I’m having trouble
finding out how to do this through online references.)

My main goal is to use current system date and time for naming log files.
So, for time, inclusion of seconds would be desired.

I suppose I need to “require” the Date, Time, and perhaps Parsedate
modules, but I’m not sure where to go from here.

Much obliged!

Thanks.

-Kurt Euler

Todays date -

require “date”
puts Date::today() # Output - 2002-07-09

Why require “date”?

Time.now.strftime(“%Y-%m-%d”) # => “2002-07-09”

Current time -

puts Time::now() # Output - Tue Jul 09 10:06:52 Mountain Daylight Time 2002

Time.now # => Tue Jul 09 12:33:53 EDT 2002

For logfile naming using timestamps, I tend to use:

filename = “my_log”
logfile = “#{filename}#{Time.now.strftime(”%Y-%m-%d-%H-%M-%S")}"

If I’m creating temp files, I like to handle multiple files per
second, so I kludged this:

filename = “/dir/tempfile”
tempfile = “#{filename}#{Time.now.strftime(”%Y-%m-%d-%H-%M-%S")}#{(60000*rand).to_i}"

There’s probably a better way to do the latter …

– Dossy

···

On 2002.07.10, Rich rich@lithinos.com wrote:


Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)