Forum: Ruby need only time

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Shanmu G. (Guest)
on 2008-12-10 12:01
hi all,
how to get time alone from this format
(1986, 1, 28, 17, 39, 18)
Stefan R. (Guest)
on 2008-12-10 12:49
Shanmu G. wrote:
> hi all,
> how to get time alone from this format
> (1986, 1, 28, 17, 39, 18)

Use Time.mktime

Regards
Stefan
Josef 'Jupp' Schugt (Guest)
on 2008-12-10 13:21
(Received via mailing list)
On Wed, 10 Dec 2008 10:54:29 +0100, Shanmu G.
<removed_email_address@domain.invalid> wrote:

> how to get time alone from this format
> (1986, 1, 28, 17, 39, 18)

Given the numbers I suppose that the format is as follows:

    datetime = [1986, 1, 28, 17, 39, 18] # [Y, m, d, H, M, S]; array of
integers

where Y is the year, m is the month, d the day, H the hour, M the minute
and S the second (at least this is the order of arguments provided to
Time.utc and similar functions). I am not sure what you mean by "get
time". For a string representation of the time you could use:

    Time.utc(*datetime).strftime("%H:%M:%S")

which in the given example returns

    "17:39:18"

The same could also be done without using any Time functions using

    "%02d:%02d:%0d" % datetime[-3..-1]

where %02d results in a zero-padded (hence the 0) two character (hence
the 2) representation of  the values. While in this case it is not
needed, it makes sure that that (0, 8, 15, 1, 2, 3) results in
"01:02:03" and not in "1:2:3".

In the case you start with

    datetime = Time.utc(1986, 1, 28, 17, 39, 18)

and want a numerical array of hour, minute and second you can use

    [datetime.hour, datetime.min, datetime.sec]

I hope I understood your question correctly.

Josef 'Jupp' Schugt
This topic is locked and can not be replied to.