Android Question Finding a way to get device's timezone ID

toby

Well-Known Member
Licensed User
Longtime User
DateTime.TimeZoneOffset returns either -5 or -4 (Daylight saving Time) for New York and -6 or -5 (Daylight saving Time) for Chicago. As you can see, I could get -5 for both cities depending on whether Daylight Saving time is in effect and I have to handle the tricky Daylight Saving time related issue myself.

One solution is to use Timezone ID (such as "America/New_York" and "America/Chicago"). In my php code, I need to call date_default_timezone_set() function which expects a Timezone ID, i.e.,
B4X:
date_default_timezone_set("America/New_York");

I'm wondering what are some easy ways in B4A to get the phone device's Timezone ID.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I can use the daylight saving time function to find out if it's in effect
What do you need it for? As I wrote, if you are using it for time calculations then you are most probably doing something wrong.

You can get the id string with:
B4X:
Dim TimeZone As JavaObject
TimeZone = TimeZone.InitializeStatic("java.util.TimeZone").RunMethod("getDefault", Null)
Log(TimeZone.RunMethod("getID", Null))
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
In my app, I can't rely on the device's date and time because user can change it manually. So I'm getting the information from a ntp time server with a httpjob which passes the Timezone ID to the php script :
B4X:
            Dim j As HttpJob
            j.Initialize("", Me)
            j.Download2("https://samplesite.com/ntpTime.php",Array As String("timezone_id", "America/Toronto")) 'get datetime from ntp server


php script ntpTime.php:
B4X:
<?php
$timezone_id = $_GET["timezone_id"]; //"America/Toronto"
error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", 1);

date_default_timezone_set($timezone_id); //this is the only place Timezone ID is used

/* Query a time server (C) 1999-09-29, Ralf D. Kloth (QRQ.software) <ralf at qrq.de> */
function query_time_server ($timeserver, $socket)
{
    $fp = fsockopen($timeserver,$socket,$err,$errstr,5);
        # parameters: server, socket, error code, error text, timeout
    if($fp)
    {
        fputs($fp, "\n");
        $timevalue = fread($fp, 49);
        fclose($fp); # close the connection
    }
    else
    {
        $timevalue = " ";
    }

    $ret = array();
    $ret[] = $timevalue;
    $ret[] = $err;     # error code
    $ret[] = $errstr;  # error text
    return($ret);
} # function query_time_server


$timeserver ="ntp.pads.ufrj.br"; //"time1.google.com"; // "time.chu.nrc.ca"; //"ntp.pads.ufrj.br";
$timercvd = query_time_server($timeserver, 37);

//if no error from query_time_server
if(!$timercvd[1])
{
    $timevalue = bin2hex($timercvd[0]);
    $timevalue = abs(HexDec('7fffffff') - HexDec($timevalue) - HexDec('7fffffff'));
    $tmestamp = $timevalue - 2208988800; # convert to UNIX epoch time stamp
    $datum = date("Y-m-d (D) H:i:s",$tmestamp - date("Z",$tmestamp)); /* incl time zone offset */
    $doy = (date("z",$tmestamp)+1);

   // echo "Time check from time server ",$timeserver," : [<font color=\"red\">",$timevalue,"</font>]";
    //echo " (seconds since 1900-01-01 00:00.00).<br>\n";
    //echo "The current date and universal time is ",$datum," UTC. ";
    //echo "It is day ",$doy," of this year.<br>\n";
    //echo "The unix epoch time stamp is $tmestamp.<br>\n";


    echo date("d/m/Y H:i:s", $tmestamp);

}
else
{
    echo "Unfortunately, the time server $timeserver could not be reached at this time. ";
    echo "$timercvd[1] $timercvd[2].<br>\n";
}
?>
 
Upvote 0
Top