Convert date to string

Hi
i have seen couple of example of convert the date to string. but dont
understand how to use that in my example

remote_date=cat #{file} | grep -m 1 'Current time:' | awk {'print $3'}
remote_time =cat #{file} | grep -m 1 'Current time:' | awk {'print $4'}

puts remote_date ::::2012-11-11
puts remote_time :::: 17:57:05

current_date=date +%Y-%m-%d
current_time=date +%H:%M:%S

if remote_date.to_s == current_date then
puts “Date is same”

else

puts “Time differences found”

end
puts current_date :::2012-11-11
puts current_time :::17:57:11

Basically, want i want is :

if current time is 1 min or less then 1 min different then remote time
everything is fine, but more then 1 min difference is a problem

any help will be great.

it would be great if i can do this without installing any ruby module
(which have to install )

thanks

On Sun, Nov 11, 2012 at 10:04 AM, Ferdous ara [email protected]
wrote:

i have seen couple of example of convert the date to string. but dont
understand how to use that in my example

Why would you want to? For your use case it’s easier to compare
actual DateTime objects than strings.

Look at the docs for Date, DateTime and Time in the stdlib.

1.9.3 (main):0 > t2 = DateTime.new(2012, 11, 11, 9, 1, 2)
=> #<DateTime: 2012-11-11T09:01:02+00:00
((2456243j,32462s,0n),+0s,2299161j)>
1.9.3 (main):0 > t1 = DateTime.new(2012, 11, 11, 9, 1, 1)
=> #<DateTime: 2012-11-11T09:01:01+00:00
((2456243j,32461s,0n),+0s,2299161j)>
1.9.3 (main):0 > t2.to_time - t1.to_time
=> 1.0

Subject: Convert date to string
Date: Mon 12 Nov 12 03:04:03AM +0900

Quoting Ferdous ara ([email protected]):

Basically, want i want is :

if current time is 1 min or less then 1 min different then remote time
everything is fine, but more then 1 min difference is a problem

any help will be great.

it would be great if i can do this without installing any ruby module
(which have to install )

The time-management functions that are included in Ruby are excellent.

Just do

require ‘time’

and then

t=Time::parse(remote_date+’ '+remote_time)

You will have a Time object (do ri Time to know more), on which you
can do arithmetic. Basically, if you subtract a time from another, you
will obtain the difference in seconds.

Carlo

You will have a Time object (do ri Time to know more), on which you
can do arithmetic. Basically, if you subtract a time from another, you
will obtain the difference in seconds.

Hi
you said, Substract a time from another, yes thats one i want …

So bellow works perfect

t=Time::parse(remote_date+’ ‘+remote_time)
t1=Time::parse(current_date+’ '+current_time)
puts t
puts t1

difference_in_time = (t1 - t).to_i

puts difference_in_time

thanks

Am 11.11.2012 19:57, schrieb Ferdous ara:

So bellow works perfect

t=Time::parse(remote_date+’ ‘+remote_time)
t1=Time::parse(current_date+’ '+current_time)

Simply use `t1 = Time.new’, which returns the current system time.

puts t
puts t1

difference_in_time = (t1 - t).to_i

The `to_i’ seems unnecessary to me.

On Mon, Nov 12, 2012 at 6:23 AM, [email protected] wrote:

t1=Time::parse(current_date+’ '+current_time)

I think that should be DateTime::parse, but …

Simply use `t1 = Time.new’, which returns the current system time.

The OP should be aware that parsing a time out of a string value as
we’re doing here will, in the absence of any timezone data, assume a
TZ offset of 0 (UTC/GMT).

1.9.3 (main):0 > DateTime::parse(remote_date+’ '+remote_time).to_s
=> “2012-11-11T17:57:05+00:00”
1.9.3 (main):0 >

Time.new will use the system TZ offset, e.g. -0800 for me here on the
West Coast.

1.9.3 (main):0 > Time.new
=> 2012-11-12 06:37:58 -0800

So if you’re trying to compare 2 times, make sure they’re using the
same TZ offset as well, or your results will be, erm, suspect :slight_smile:

Am 12.11.2012 15:45, schrieb Hassan S.:

On Mon, Nov 12, 2012 at 6:23 AM, [email protected] wrote:

t1=Time::parse(current_date+’ '+current_time)

I think that should be DateTime::parse, but …

The OP was using Time.parse from the `Time’ library.

Simply use `t1 = Time.new’, which returns the current system time.

The OP should be aware that parsing a time out of a string value as
we’re doing here will, in the absence of any timezone data, assume a
TZ offset of 0 (UTC/GMT).
[…]
So if you’re trying to compare 2 times, make sure they’re using the
same TZ offset as well, or your results will be, erm, suspect :slight_smile:

Generally, this is a very good point.

But for the considered example, Time.parse returns local time,
since no time zone was specified in the argument, so it should work.

On Mon, Nov 12, 2012 at 7:08 AM, [email protected] wrote:

t1=Time::parse(current_date+’ '+current_time)

I think that should be DateTime::parse, but …

The OP was using Time.parse from the `Time’ library.

Really?

1.9.3 (main):0 > Time.parse(remote_date+’ ‘+remote_time)
NoMethodError: undefined method `parse’ for Time:Class

Simply use `t1 = Time.new’, which returns the current system time.

But for the considered example, Time.parse returns local time,
since no time zone was specified in the argument, so it should work.

No, it doesn’t, and no, it won’t.

1.9.3 (main):0 > t1 = DateTime.parse(“2012-11-12 07:30”)
=> #<DateTime: 2012-11-12T07:30:00+00:00
((2456244j,27000s,0n),+0s,2299161j)>
1.9.3 (main):0 > t1.to_s
=> “2012-11-12T07:30:00+00:00”
1.9.3 (main):0 > t2 = Time.new
=> 2012-11-12 07:32:00 -0800
1.9.3 (main):0 > t2 - t1.to_time
=> 28920.165548
1.9.3 (main):0 > (t2 - t1.to_time) / 60 / 60
=> 8.033379318888889
1.9.3 (main):0 >

At least, that’s how it works for me. YMMV :slight_smile:

Subject: Re: Convert date to string
Date: Tue 13 Nov 12 12:35:55AM +0900

Quoting Hassan S. ([email protected]):

Really?

1.9.3 (main):0 > Time.parse(remote_date+’ ‘+remote_time)
NoMethodError: undefined method `parse’ for Time:Class

As I mentioned in my post, you first have to:

require ‘time’

Carlo

On Mon, Nov 12, 2012 at 7:41 AM, Carlo E. Prelz [email protected]
wrote:

As I mentioned in my post, you first have to:

require ‘time’

OK, that’s disturbing. How is it that I can do Time.new without first
requiring ‘time’ but Time.parse fails??

In any case, you’re right that Time.parse uses the system TZ in
contrast to DateTime.parse. Which seems strange, too, but…

Thanks for enlightening me :slight_smile:

Am 12.11.2012 16:35, schrieb Hassan S.:

1.9.3 (main):0 > Time.parse(remote_date+’ ‘+remote_time)
NoMethodError: undefined method `parse’ for Time:Class

Please reread the thread.
The OP used require ‘time’, exactly as I stated.

Simply use `t1 = Time.new’, which returns the current system time.

But for the considered example, Time.parse returns local time,
since no time zone was specified in the argument, so it should work.

No, it doesn’t, and no, it won’t.

Yes, it does, and yes, it will.

$ ri Time.parse
[…]

If the extracted timezone abbreviation does not match any of them, it
is ignored and the given time is regarded as a local time.

[…]

1.9.3 (main):0 > t1 = DateTime.parse(“2012-11-12 07:30”)
[…]

I was explicitly not talking about DateTime.parse!

Subject: Re: Convert date to string
Date: Tue 13 Nov 12 12:57:24AM +0900

Quoting Hassan S. ([email protected]):

OK, that’s disturbing. How is it that I can do Time.new without first
requiring ‘time’ but Time.parse fails??

Plain time handling is included in the default runtime. Time.rb adds
further functionalities:

When ‘time’ is required, Time is extended with additional methods for

parsing

and converting Times.

Carlo