Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set time zone offset in Ruby

Tags:

time

ruby

The default time zone offset in Ruby is apparently -0800. I want to set mine to -0500. How do I do this?

like image 991
Jason Swett Avatar asked Jan 16 '11 04:01

Jason Swett


People also ask

How do you change TimeZone in Ruby?

You can do: Time. now + Time. zone_offset("PST") if you: require 'time' in your ruby script. guess, should have done that before commenting.

How do you parse time in Ruby?

Ruby | DateTime parse() function DateTime#parse() : parse() is a DateTime class method which parses the given representation of date and time, and creates a DateTime object. Return: given representation of date and time, and creates a DateTime object.

What does time now return in Ruby?

The now() is an inbuilt method in Ruby returns the current time.


1 Answers

Set the TZ environment variable...

$ ruby -e 'puts Time.now' Sat Jan 15 20:49:10 -0800 2011 $ TZ=UTC ruby -e 'puts Time.now' Sun Jan 16 04:49:20 +0000 2011 

Ruby gets the time zone information from the host's operating system.

Most directly, it uses a C library API specified by C99 and Posix.

The implementation of that API is system-specific, on my Mac that means it consults /etc/localtime unless there is a TZ environment variable.

It's about the same on Linux.

like image 54
DigitalRoss Avatar answered Oct 03 '22 14:10

DigitalRoss