Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Time#getlocal

time.getlocal → new_time
time.getlocal(utc_offset) → new_time
time.getlocal(timezone) → new_time

Returns a new Time object representing time in local time (using the local time zone in effect for this process).

If utc_offset is given, it is used instead of the local time. utc_offset can be given as a human-readable string (eg. "+09:00") or as a number of seconds (eg. 32400).

Example
t = Time.utc(2000,1,1,20,15,1)  #=> 2000-01-01 20:15:01 UTC
t.utc?                          #=> true

l = t.getlocal                  #=> 2000-01-01 14:15:01 -0600
l.utc?                          #=> false
t == l                          #=> true

j = t.getlocal("+09:00")        #=> 2000-01-02 05:15:01 +0900
j.utc?                          #=> false
t == j                          #=> true

k = t.getlocal(9*60*60)         #=> 2000-01-02 05:15:01 +0900
k.utc?                          #=> false
t == k                          #=> true
See also #getgm #getutc #friday? #gm also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1