Time

Hello

I#m a bit confused: time is in stdlib and core? - How do I distinguish which one to use?

=> How can I see from which module a method belongs to (only knowing the method-name), if included ?

How can I select the timezone? Time.to_s gives timezone, but the input is ignored:
Time.parse "2016-10-31 04:00 +500" +500 or +1500 ....

How can I set the the zone-information (included in the same string) ?

Time.local "2016-10-31 04:00" # ignores the time??

thanks
Opti

Hello Opti,

I did some reading, and according to this
<What's the difference between Ruby Core API and Standard Library API? - Stack Overflow; answer at StackOverflow, the
Time class in the Core Library doesn't include all the methods that the
Standard Library does.

How can I select the timezone? Time.to_s gives timezone, but the input is

ignored:
Time.parse "2016-10-31 04:00 +500" +500 or +1500 ....

Assuming by select you mean to set the timezone, first you have to require
the Time Class from the stdlib in order to use Time.parse. Then, you could
do something like the following to set the timezone:
    t = Time.parse("2016-11-11 12:00 -0500") # the last parameter, "-0500"
is the timezone.
    > 2016-11-11 12:00:00 -0500

To get the timezone, you can use the Time.zone method.
    t = Time.parse "2016-11-11 1:30 -0400"
    > 2016-11-11 1:30:00 -0400
    t.zone
    > "AST"

Have a nice one,

···

--
Antonio Santos
aj.santos128@gmail.com

On Fri, Nov 11, 2016 at 8:51 PM, Die Optimisten <inform@die-optimisten.net> wrote:

Hello

I#m a bit confused: time is in stdlib and core? - How do I distinguish
which one to use?

=> How can I see from which module a method belongs to (only knowing the
method-name), if included ?

How can I select the timezone? Time.to_s gives timezone, but the input is
ignored:
Time.parse "2016-10-31 04:00 +500" +500 or +1500 ....

How can I set the the zone-information (included in the same string) ?

Time.local "2016-10-31 04:00" # ignores the time??

thanks
Opti

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

--
Antonio Santos
AJ.Santos128@gmail.com

Hi
Yes, I tried that already -

Output:

t = Time.parse("2016-11-11 12:00 -0500")
         => 2016-11-11 18:00:00 +0100

Thats my problem...
I'm using irb RUBY 1.9 and 2.3.1

thanks
Opti

Hi,

By means of deduction, I found that when you create a new Time object, it uses your local timezone regardless of the one you input. I did find a work around, maybe you can see if it works for you.

The Time class includes a #localtime method: Class: Time (Ruby 2.3.1). Using this method, you can convert a time variable to a specific timezone. Here’s an example:

   t = Time.parse("2016-11-11 12:00 -05:00”)
   => 2016-11-11 13:00:00 -0400 # It created the variable but in my timezone.
   t = t.localtime("+01:00”) # I used your timezone to show the conversion is accurate.
   => 2016-11-11 18:00:00 +0100

So to get your desired output, you’d have to create the variable, and then change it to the desired timezone by using the #localtime method. Hope I worded it all in a way that makes sense.

Cheers,

···

--
Antonio Santos
aj.santos128@gmail.com

On Nov 11, 2016, at 10:12 PM, Die Optimisten <inform@die-optimisten.net> wrote:

Hi
Yes, I tried that already -

Output:

t = Time.parse("2016-11-11 12:00 -0500")
       => 2016-11-11 18:00:00 +0100

Thats my problem...
I'm using irb RUBY 1.9 and 2.3.1

thanks
Opti

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Hi,

Time.parse "2016-10-31 04:00 +500"
=> 2016-10-31 04:00:00 +0100 # thats the problem, the time is not converted, still ignoring the timezone

Does anyone else have the same problem? No special other modules included...

thanks
Opti

···

On 2016-11-12 03:52, Antonio Santos wrote:

Hi,

By means of deduction, I found that when you create a new Time object, it uses your local timezone regardless of the one you input. I did find a work around, maybe you can see if it works for you.

The Time class includes a #localtime method: Class: Time (Ruby 2.3.1). Using this method, you can convert a time variable to a specific timezone. Here’s an example:

   t = Time.parse("2016-11-11 12:00 -05:00”)
   => 2016-11-11 13:00:00 -0400 # It created the variable but in my timezone.
   t = t.localtime("+01:00”) # I used your timezone to show the conversion is accurate.
   => 2016-11-11 18:00:00 +0100

So to get your desired output, you’d have to create the variable, and then change it to the desired timezone by using the #localtime method. Hope I worded it all in a way that makes sense.

Cheers,

--
Antonio Santos
aj.santos128@gmail.com <mailto:aj.santos128@gmail.com>

On Nov 11, 2016, at 10:12 PM, Die Optimisten >> <inform@die-optimisten.net> wrote:

Hi
Yes, I tried that already -

Output:

t = Time.parse("2016-11-11 12:00 -0500")
       => 2016-11-11 18:00:00 +0100

Thats my problem...
I'm using irb RUBY 1.9 and 2.3.1

thanks
Opti

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Hi,

Time.parse "2016-10-31 04:00 +500"
=> 2016-10-31 04:00:00 +0100 # thats the problem, the time is not converted, still ignoring the timezone

Does anyone else have the same problem? No special other modules included…

I don’t think +500 is a valid timezone. Try +0500:

Time.parse "2016-10-31 04:00 +0500"

=> 2016-10-31 04:00:00 +0500

Peter

···

On 12 Nov 2016, at 17:56, Die Optimisten <inform@die-optimisten.net> wrote:

Indeed +500 is not a valid timezone offset in ISO 8601, but +0500, +05:00
and +05 are valid.

···

On Sat, Nov 12, 2016, 18:18 Peter W A Wood <peterwawood@gmail.com> wrote:

> On 12 Nov 2016, at 17:56, Die Optimisten <inform@die-optimisten.net> > wrote:
>
> Hi,
>
> Time.parse "2016-10-31 04:00 +500"
> => 2016-10-31 04:00:00 +0100 # thats the problem, the time is not
converted, still ignoring the timezone
>
> Does anyone else have the same problem? No special other modules
included…

I don’t think +500 is a valid timezone. Try +0500:

> Time.parse "2016-10-31 04:00 +0500"
=> 2016-10-31 04:00:00 +0500

Peter

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Oh, its the 0 ! +0500 is fine

Thanks

Hi
Its not fine that
t2 = t.localtime also changes the format of t !
that should be t.localtime! only

Please anybody could correct that (or tell why it makes sense this way)
Further it's a bid confusing to have Time, Date and DateTime... (one too much, but better to much than too little)

Opti