Created
November 13, 2014 17:48
-
-
Save numist/f34cb150e337a8b948d9 to your computer and use it in GitHub Desktop.
Get the system time zone in Olson format using Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_local_timezone_str | |
| # Yes, this is actually a shell script… | |
| olsontz = `if [ -f /etc/timezone ]; then | |
| cat /etc/timezone | |
| elif [ -h /etc/localtime ]; then | |
| readlink /etc/localtime | sed "s/\\/usr\\/share\\/zoneinfo\\///" | |
| else | |
| checksum=\`md5sum /etc/localtime | cut -d' ' -f1\` | |
| find /usr/share/zoneinfo/ -type f -exec md5sum {} \\; | grep "^$checksum" | sed "s/.*\\/usr\\/share\\/zoneinfo\\///" | head -n 1 | |
| fi`.chomp | |
| # …and it almost certainly won't work with Windows or weird *nixes | |
| throw "Olson time zone could not be determined" if olsontz.nil? || olsontz.empty? | |
| return olsontz | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When using Rails, things only get worse—Rails gets the time zone from its configuration, so it's not useful for fetching the system's time zone.
Adding to the dissonance, Rails cleverly extends
Time: