How to get local timezone name from Ruby in the same way as Java?

I need to find out the time zone name of the local machine from Ruby (from the list of values โ€‹โ€‹of TZInfo :: Timezone.all_identifiers). I need this so that I can correctly set the time zone of the Oracle database session so that I can correctly handle the timestamp with the time zone values โ€‹โ€‹in the database.

I would like to get the same value as in Java, which can be obtained using java.util.TimeZone.getDefault (). getID () (which on my computer returns "Europe / Riga"). As I understood in the JDK sources, there are special C functions that define this default timezone.

Therefore, when using JRuby, I can call this Java method. But I need a solution that I can use with MRI.

I tried using Time.now.zone, but it returns different results (in my case โ€œEETโ€ or โ€œEESTโ€) depending on whether daylight saving time is on or not. Therefore, I really need to enter the previously mentioned location format (for example, "Europe / Riga"), which correctly determines the dates when daylight saving time occurs.

Any suggestions?

+4
source share
1 answer

JRuby returns EET / CDT values โ€‹โ€‹for compatibility, since C-Ruby does the same. But, since JRuby can easily call the Java level, you can simply call it tat:

require 'java' id = java.util.TimeZone.getDefault().getID() 

or, in more Ruby-like syntax:

 require 'java' id = java.util.TimeZone.get_default.get_id 

Alternatively, you can use JodaTime:

 require 'java' id = org.joda.time.DateTimeZone.getDefault.getID 
+2
source

All Articles