mmkw43
Posts: 659
Joined: Tue Dec 24, 2013 6:18 pm

US timezone expert -- timedatectl and PI

Mon Nov 16, 2020 3:15 pm

Seems to me rather than the huge amount of timezones listed, if you gave someone the choice of just these cities to set timezone using timeatectl, wouldn't you have all the timezones covered, 365 days a year? New York -- Chicago -- Denver -- Los Angeles -- Anchorage -- Honolulu And the PI's ntp synchronized clock would automatically take care of daylihjt savings time correct?
THANKS

cleverca22
Posts: 2488
Joined: Sat Aug 18, 2012 2:33 pm

Re: US timezone expert -- timedatectl and PI

Mon Nov 16, 2020 3:26 pm

Code: Select all

[clever@amd-nixos:~]$ tzselect 
Please identify a location so that time zone rules can be set correctly.
Please select a continent, ocean, "coord", or "TZ".
 1) Africa
 2) Americas
 3) Antarctica
 4) Asia
 5) Atlantic Ocean
 6) Australia
 7) Europe
 8) Indian Ocean
 9) Pacific Ocean
10) coord - I want to use geographical coordinates.
11) TZ - I want to specify the timezone using the Posix TZ format.
#? 2
Please select a country whose clocks agree with yours.
 1) Anguilla              19) Dominican Republic    37) Peru
 2) Antigua & Barbuda     20) Ecuador               38) Puerto Rico
 3) Argentina             21) El Salvador           39) St Barthelemy
 4) Aruba                 22) French Guiana         40) St Kitts & Nevis
 5) Bahamas               23) Greenland             41) St Lucia
 6) Barbados              24) Grenada               42) St Maarten (Dutch)
 7) Belize                25) Guadeloupe            43) St Martin (French)
 8) Bolivia               26) Guatemala             44) St Pierre & Miquelon
 9) Brazil                27) Guyana                45) St Vincent
10) Canada                28) Haiti                 46) Suriname
11) Caribbean NL          29) Honduras              47) Trinidad & Tobago
12) Cayman Islands        30) Jamaica               48) Turks & Caicos Is
13) Chile                 31) Martinique            49) United States
14) Colombia              32) Mexico                50) Uruguay
15) Costa Rica            33) Montserrat            51) Venezuela
16) Cuba                  34) Nicaragua             52) Virgin Islands (UK)
17) Curaçao               35) Panama                53) Virgin Islands (US)
18) Dominica              36) Paraguay
#? 10
Please select one of the following timezones.
 1) Newfoundland; Labrador (southeast)  15) Central - NU (Resolute)
 2) Atlantic - NS (most areas); PE      16) Central - NU (central)
 3) Atlantic - NS (Cape Breton)         17) CST - SK (most areas)
 4) Atlantic - New Brunswick            18) CST - SK (midwest)
 5) Atlantic - Labrador (most areas)    19) Mountain - AB; BC (E); SK (W)
 6) AST - QC (Lower North Shore)        20) Mountain - NU (west)
 7) Eastern - ON, QC (most areas)       21) Mountain - NT (central)
 8) Eastern - ON, QC (no DST 1967-73)   22) Mountain - NT (west)
 9) Eastern - ON (Thunder Bay)          23) MST - BC (Creston)
10) Eastern - NU (most east areas)      24) MST - BC (Dawson Cr, Ft St John)
11) Eastern - NU (Pangnirtung)          25) MST - BC (Ft Nelson)
12) EST - ON (Atikokan); NU (Coral H)   26) Pacific - BC (most areas)
13) Central - ON (west); Manitoba       27) Pacific - Yukon (south)
14) Central - ON (Rainy R, Ft Frances)  28) Pacific - Yukon (north)
#? 4

The following information has been given:

        Canada
        Atlantic - New Brunswick

Therefore TZ='America/Moncton' will be used.
Selected time is now:   Mon Nov 16 11:25:41 AST 2020.
Universal Time is now:  Mon Nov 16 15:25:41 UTC 2020.
Is the above information OK?
1) Yes
2) No
#? 1

You can make this change permanent for yourself by appending the line
        TZ='America/Moncton'; export TZ
to the file '.profile' in your home directory; then log out and log in again.

Here is that TZ value again, this time on standard output so that you
can use the /run/current-system/sw/bin/tzselect command in shell scripts:
America/Moncton
using the tzselect binary, you can reduce it from a single massive list, to 3 short lists, and its already designed to interface nicely with scripts

W. H. Heydt
Posts: 14048
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: US timezone expert -- timedatectl and PI

Mon Nov 16, 2020 4:09 pm

mmkw43 wrote:
Mon Nov 16, 2020 3:15 pm
Seems to me rather than the huge amount of timezones listed, if you gave someone the choice of just these cities to set timezone using timeatectl, wouldn't you have all the timezones covered, 365 days a year? New York -- Chicago -- Denver -- Los Angeles -- Anchorage -- Honolulu And the PI's ntp synchronized clock would automatically take care of daylihjt savings time correct?
THANKS
Not sufficient.

Arizona doesn't do DST at all, so add "Phoenix" to your list. One of the Midwestern states (Indiana? Illinois?) has part of the state that doesn't do DST. California passed a ballot proposition a few years ago to (shudder!) go to DST full time. Fortunately, there has been no further legislative action on that (it would have to pass both the state legislature and the US Congress to be implemented).

mmkw43
Posts: 659
Joined: Tue Dec 24, 2013 6:18 pm

Re: US timezone expert -- timedatectl and PI

Mon Nov 16, 2020 4:32 pm

Ok thanks WH. I'll add Indianapolis and Phoenix to it and do some searching for a comprehensive "short list". It's for a python script and don't want to have a lot of choices in a pull down menu I want to do. I could just do an entry box and have them type it in to be on the safe side but prefer the easy drop down menu. Thanks much.

mmkw43
Posts: 659
Joined: Tue Dec 24, 2013 6:18 pm

Re: US timezone expert -- timedatectl and PI

Mon Nov 16, 2020 4:34 pm

Yeah and I hate DST but let me ask, if I get the timezone choices right, the PI's clock will take care of the back and forth of DST?

mmkw43
Posts: 659
Joined: Tue Dec 24, 2013 6:18 pm

Re: US timezone expert -- timedatectl and PI

Mon Nov 16, 2020 4:36 pm

and thanks clever -- I'll delve into that as well. what I really wanted to do is bring up raspi-config in a python script using subprocess.call but can't get it done. keeps crashing.

User avatar
DougieLawson
Posts: 40579
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: US timezone expert -- timedatectl and PI

Mon Nov 16, 2020 8:39 pm

mmkw43 wrote:
Mon Nov 16, 2020 4:34 pm
Yeah and I hate DST but let me ask, if I get the timezone choices right, the PI's clock will take care of the back and forth of DST?
For the places that do daylight saving it will automagically change the local clock on the appropriate weekends in the spring and autumn.
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

W. H. Heydt
Posts: 14048
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: US timezone expert -- timedatectl and PI

Mon Nov 16, 2020 9:27 pm

mmkw43 wrote:
Mon Nov 16, 2020 4:34 pm
Yeah and I hate DST but let me ask, if I get the timezone choices right, the PI's clock will take care of the back and forth of DST?
Yes. At least...all of mine do.

Kaadk
Posts: 46
Joined: Fri Aug 28, 2020 3:39 pm

Re: US timezone expert -- timedatectl and PI

Mon Nov 16, 2020 10:11 pm

mmkw43 wrote:
Mon Nov 16, 2020 4:32 pm
Ok thanks WH. I'll add Indianapolis and Phoenix to it and do some searching for a comprehensive "short list". It's for a python script and don't want to have a lot of choices in a pull down menu I want to do. I could just do an entry box and have them type it in to be on the safe side but prefer the easy drop down menu. Thanks much.
Instead of location based, I wonder if you can just ask them their time zone (Eastern, Mountain, Central, etc...) and whether or not they need to adjust for Daylight savings. You know, kind of how things used to work way back in the 80's, 90's, early 00's. That way, you don't have the person in Buffalo trying to figure out if they should pick Toronto, Detroit or NYC since they all basically mean the same thing.

User avatar
scruss
Posts: 3652
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: US timezone expert -- timedatectl and PI

Mon Nov 16, 2020 11:00 pm

Why limit it? There will be edge cases you can never expect to know about. Like Newfoundland, half an hour east of Atlantic time, itself an hour east of Eastern time.

If you're network connected, there are reasonable ways to geolocate timezone from network connection
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

User avatar
DougieLawson
Posts: 40579
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: US timezone expert -- timedatectl and PI

Mon Nov 16, 2020 11:04 pm

You only have 13 zones to work with.

Code: Select all

pi@discovery:/usr/share/zoneinfo/US $ ls
Alaska    Arizona  Eastern       Hawaii          Michigan  Pacific      Samoa
Aleutian  Central  East-Indiana  Indiana-Starke  Mountain  Pacific-New
pi@discovery:/usr/share/zoneinfo/US $
There's more in /usr/share/zoneinfo/America but a large number of them aren't within the 50 states.

I'd have thought the tzdata folks would have built a TZ file for each state capital (but they didn't). It's all a bit higgledy-piggledy. I guess that wouldn't work when states like TN have different zones in the east and west of the state.
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

User avatar
DougieLawson
Posts: 40579
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: US timezone expert -- timedatectl and PI

Mon Nov 16, 2020 11:09 pm

scruss wrote:
Mon Nov 16, 2020 11:00 pm
Why limit it? There will be edge cases you can never expect to know about. Like Newfoundland, half an hour east of Atlantic time, itself an hour east of Eastern time.

If you're network connected, there are reasonable ways to geolocate timezone from network connection.
This entirely ugly PHP program does Geolocation from an IPv4 address.
The OpenLayers stuff is only there so I can plot an address on a map. You will need an API key from https://ipgeolocation.io/pricing.html.

Code: Select all

<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
<script src="../../OpenLayers/OpenLayers.js"></script>

</head>
<body>

<?php

    function get_geolocation($apiKey, $ip, $lang = "en", $fields = "*", $excludes = "") {
        $url = "https://api.ipgeolocation.io/ipgeo?apiKey=".$apiKey."&ip=".$ip."&lang=".$lang."&fields=".$fields."&excludes=".$excludes;
        $cURL = curl_init();

        curl_setopt($cURL, CURLOPT_URL, $url);
        curl_setopt($cURL, CURLOPT_HTTPGET, true);
        curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Accept: application/json'
        ));
        return curl_exec($cURL);
    }


// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["IP"])) {
    $nameErr = "IP is required";
  } else {
    $IP = test_input($_POST["IP"]);
    // check if name only contains letters and whitespace
  }

}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>

<h2>Geoip </h2>
<p><span class="error">* required field</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  IP address: <input type="text" name="IP" value="<?php echo $name;?>">
  <span class="error">* <?php echo $nameErr;?></span>
  <input type="submit" name="submit" value="Submit">
</form>

<?php
    echo "<h2>Your Input:</h2>";
    echo $IP;

    echo "<h2>Geoip output</h2>";
    $apiKey = "<redacted API key>";
    $location = get_geolocation($apiKey, $IP);
    $geo = json_decode($location, true);

    echo "<table>";
    echo "<tr><td>IP address:</td><td>".$geo["ip"]."</td></tr>";
    echo "<tr><td>Hostname:</td><td>".$geo["hostname"]."</td></tr>";
    echo "<tr><td>Continent code:</td><td>".$geo["continent_code"]."</td></tr>";
    echo "<tr><td>Continent name:</td><td>".$geo["continent_name"]."</td></tr>";
    echo "<tr><td>Country code[2]:</td><td>".$geo["country_code2"]."</td></tr>";
    echo "<tr><td>Country code[3]:</td><td>".$geo["country_code3"]."</td></tr>";
    echo "<tr><td>Country name:</td><td>".$geo["country_name"]."</td></tr>";
    echo "<tr><td>Country capital:</td><td>".$geo["country_capital"]."</td></tr>";
    echo "<tr><td>State/province:</td><td>".$geo["state_prov"]."</td></tr>";
    echo "<tr><td>District:</td><td>".$geo["district"]."</td></tr>";
    echo "<tr><td>City:</td><td>".$geo["city"]."</td></tr>";
    echo "<tr><td>Zipcode:</td><td>".$geo["zipcode"]."</td></tr>";
    echo "<tr><td>Latitude:</td><td>".$geo["latitude"]."</td></tr>";
    echo "<tr><td>Longitude:</td><td>".$geo["longitude"]."</td></tr>";
?>
<tr><td>&nbsp;</td><td>
<div id="Map" style="height:270px;width:270px"></div>
<script>
<?php
    echo "var lat            = ".$geo["latitude"].";";
    echo "var lon            = ".$geo["longitude"].";";
?>
    var zoom           = 11;
    var fromProjection = new OpenLayers.Projection("EPSG:4326");   // Transform from WGS 1984
    var toProjection   = new OpenLayers.Projection("EPSG:900913"); // to Spherical Mercator Projection
    var position       = new OpenLayers.LonLat(lon, lat).transform( fromProjection, toProjection);

    map = new OpenLayers.Map("Map");
    var mapnik         = new OpenLayers.Layer.OSM();
    map.addLayer(mapnik);

    var markers = new OpenLayers.Layer.Markers( "Markers" );
    map.addLayer(markers);
    markers.addMarker(new OpenLayers.Marker(position));

    map.setCenter(position, zoom);
</script>
</td></tr>
<?php
    echo "<tr><td>Is EU:</td><td>".$geo["is_eu"]."</td></tr>";
    echo "<tr><td>International dialling code:</td><td>".$geo["calling_code"]."</td></tr>";
    echo "<tr><td>Internet TLD:</td><td>".$geo["country_tld"]."</td></tr>";
    echo "<tr><td>Languages:</td><td>".$geo["languages"]."</td></tr>";
    echo "<tr><td>Flag:</td><td><img src=".$geo["country_flag"]."></img></td></tr>";
    echo "<tr><td>Internet Service Provider:</td><td>".$geo["isp"]."</td></tr>";
    echo "<tr><td>Connection type:</td><td>".$geo["connection_type"]."</td></tr>";
    echo "<tr><td>Organisation:</td><td>".$geo["organization"]."</td></tr>";
    echo "<tr><td>Geo name ID:</td><td>".$geo["geoname_id"]."</td></tr>";
    echo "<tr><td>Currency code:</td><td>".$geo["currency"]["code"]."</td></tr>";
    echo "<tr><td>Currency name:</td><td>".$geo["currency"]["name"]."</td></tr>";
    echo "<tr><td>Currency symbol:</td><td>".$geo["currency"]["symbol"]."</td></tr>";
    echo "<tr><td>Time zone name:</td><td>".$geo["time_zone"]["name"]."</td></tr>";
    echo "<tr><td>Time zone offset:</td><td>".$geo["time_zone"]["offset"]."</td></tr>";
    echo "<tr><td>Current time:</td><td>".$geo["time_zone"]["current_time"]."</td></tr>";
    echo "<tr><td>Current unix time:</td><td>".$geo["time_zone"]["current_time_unix"]."</td></tr>";
    echo "<tr><td>Is daylight saving:</td><td>".$geo["time_zone"]["is_dst"]."</td></tr>";
    echo "<tr><td>Daylight offset</td><td>".$geo["time_zone"]["dst_savings"]."</td></tr>";
    echo "</table>";

?>

</body>
</html>
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Eirikur
Posts: 95
Joined: Sun Sep 09, 2018 9:43 pm

Re: US timezone expert -- timedatectl and PI

Tue Nov 17, 2020 4:59 am

geoip technology is pretty common, especially in installers. That's how the Pi setup guesses your time zone. I don't know if there are any fully free services.

plugwash
Forum Moderator
Forum Moderator
Posts: 3723
Joined: Wed Dec 28, 2011 11:45 pm

Re: US timezone expert -- timedatectl and PI

Tue Nov 17, 2020 4:48 pm

The rule used by the TZ database is that areas within a country are grouped together if their clocks have been consistent since 1970.

I guess with a bit of effort one could write a program to work out which zones were consistent with each other given a more recent cutoff date.

Return to “General discussion”