Force checking "Set time zone automatically using current location"

Dominic_Jaeckel
New Contributor II

Hi all,

I'm having difficulties to force this check through a terminal command. Attached a screenshot.

a04bcc2c980c4badb15d80be4bccd4b3

ba89f7af127c4769acb7e33ad9bfa625

In the end I want to push it to all computers through JAMF, but I can't seem to find the right command for it.

I found this one in several articles:
/usr/bin/defaults write /Library/Preferences/com.apple.timezone.auto Active -bool true

But it seems to be deprecated.

After that I found this one and tried it as well, but it doesn't seem to check the mark.
/usr/sbin/systemsetup -setusingnetworktime on

Therefore I'm wondering if there is an option to force it somehow?

I tried on Mojave and Catalina.

Best Regards and thank you,

19 REPLIES 19

Dylan_YYC
Contributor III

The issue that ive been running into with this is by default it seem that location services are turned off to settings so this function is disabled. Maybe its the way ive setup prestage and DEP, but ive had to log in and do that manually.

smpotter
New Contributor III

I'm currently using this and seems to be working fine... note WiFi must be active...

#!/bin/bash

## enabling location services
/usr/bin/defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd LocationServicesEnabled -int 1

uuid=$(/usr/sbin/system_profiler SPHardwareDataType | grep "Hardware UUID" | cut -c22-57)
/usr/bin/defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd.$uuid LocationServicesEnabled -int 1

## configure automatic timezone
/usr/bin/defaults write /Library/Preferences/com.apple.timezone.auto Active -bool YES
/usr/bin/defaults write /private/var/db/timed/Library/Preferences/com.apple.timed.plist TMAutomaticTimeOnlyEnabled -bool YES
/usr/bin/defaults write /private/var/db/timed/Library/Preferences/com.apple.timed.plist TMAutomaticTimeZoneEnabled -bool YES
/usr/sbin/systemsetup -setusingnetworktime on
/usr/sbin/systemsetup -gettimezone
/usr/sbin/systemsetup -getnetworktimeserver

larry_barrett
Valued Contributor

Immediately added this as a Self Service policy. Great stuff guys!

Dominic_Jaeckel
New Contributor II

Hi,

I tried it on Catalina and it doesn't work.
Any ideas?

78075effc784427bbbc9067327150a14

larry_barrett
Valued Contributor

It works in Catalina. You could post the contents of your autotimezone.sh but the above code works just fine with a script and policy. In my example, I made it into a Self Service item, but you should be able to use other triggers. I have not tested on Mojave yet.

8101b23bb77a415691481f208559b8b5

96fa96625eb74a479eb24dd0a285780d

33cac30c533247e5bb41fbe60f735a52

cef6242f7012403c953fa97923533967

tommillard
New Contributor II

I can confirm this is no longer working in Catalina. It will do everything except for check the box: Set time zone automatically using current location.

This thread HERE talks about how it hasn't been working since 10.13.

larry_barrett
Valued Contributor

100% works on 10.15.4. We are on Prem Jamf Pro.

3d9d93533ad84f029167b43254d4deb1

dan-snelson
Valued Contributor II

@Dominic.Jaeckel In Terminal, if you first elevate to root and then execute your modified version of @larry_barrett's script, do you get different results than using sudo with your account?

andrew_nicholas
Valued Contributor

@larry_barrett I think @tommillard is referencing the set time by Location rather than set network time. That being said, the above script does appear to work but a restart was required for it to be recognized on my test device.

larry_barrett
Valued Contributor

@smpotter's script does require WIFI to be on. The autotimezone.sh script referenced above doesn't base your time on location, its a static location. Here's the JAMF code he's referencing:

#!/bin/sh
####################################################################################################
#
# Copyright (c) 2010, JAMF Software, LLC.  All rights reserved.
#
#       Redistribution and use in source and binary forms, with or without
#       modification, are permitted provided that the following conditions are met:
#               * Redistributions of source code must retain the above copyright
#                 notice, this list of conditions and the following disclaimer.
#               * Redistributions in binary form must reproduce the above copyright
#                 notice, this list of conditions and the following disclaimer in the
#                 documentation and/or other materials provided with the distribution.
#               * Neither the name of the JAMF Software, LLC nor the
#                 names of its contributors may be used to endorse or promote products
#                 derived from this software without specific prior written permission.
#
#       THIS SOFTWARE IS PROVIDED BY JAMF SOFTWARE, LLC "AS IS" AND ANY
#       EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
#       WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
#       DISCLAIMED. IN NO EVENT SHALL JAMF SOFTWARE, LLC BE LIABLE FOR ANY
#       DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
#       (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
#       LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#       ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#       (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#       SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
####################################################################################################
#
# SUPPORT FOR THIS PROGRAM
#
#       This program is distributed "as is" by JAMF Software, LLC's Resource Kit team. For more
#       information or support for the Resource Kit, please utilize the following resources:
#
#               http://list.jamfsoftware.com/mailman/listinfo/resourcekit
#
#               http://www.jamfsoftware.com/support/resource-kit
#
#       Please reference our SLA for information regarding support of this application:
#
#               http://www.jamfsoftware.com/support/resource-kit-sla
#
####################################################################################################
#
# ABOUT THIS PROGRAM
#
# NAME
#   setTimeZone.sh -- Set the time zone
#
# SYNOPSIS
#   sudo setTimeZone.sh
#   sudo setTimeZone.sh <mountPoint> <computerName> <currentUsername> <timeZone>
#
#   If the $timeZone parameter is specified (parameter 4), this is the time sone that will be set.
#
#   If no parameter is specified for parameter 4, the hardcoded value in the script will be used.
#
# DESCRIPTION
#   This script sets the system time zone as reflected in the Date & Time preference pane with the
#   System Preferences application.  It has been designed to work on Mac OS X 10.3 and higher.
#
#   A list of supported time zone entries can be found by running the command:
#
#       For Mac OS X 10.5 or later:
#
#           /usr/sbin/systemsetup -listtimezones
#
#       For Mac OS X 10.4 or earlier:
#
#           /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Support/systemsetup -listtimezones
#
#   The system time zone will be set according to the value specified in the paramter $timeZone.
#   It can be used with a hardcoded value in the script, or read in as a parameter.  Since the
#   Casper Suite defines the first three parameters as (1) Mount Point, (2) Computer Name and
#   (3) username, we are using the forth parameter ($4) as the passable parameter.  If no parameter
#   is passed, then the hardcoded value will be used.
#
####################################################################################################
#
# HISTORY
#
#   Version: 1.0
#
#   - Created by Nick Amundsen on August 5th, 2008
#
#   Version: 2.0
#
#   - Updated by Brock Walters October 28 2014
#
####################################################################################################
#
# DEFINE VARIABLES & READ IN PARAMETERS
#
####################################################################################################
#
# A HARDCODED VALUE FOR "timeZone" CAN BE SET BELOW.
#
# A list of accepted time zone values can be generated using the following command in Terminal:
#
#       sudo systemsetup -listtimezones
#
# Delete the double quotes and replace with the desired time zone name, e.g. timeZone=Pacific/Honolulu
# If this script is to be deployed via policy using the JSS leave the next line as is.
#
####################################################################################################

timeZone=""

####################################################################################################
#
# SCRIPT CONTENTS - DO NOT MODIFY BELOW THIS LINE
#
####################################################################################################

# CHECK TO SEE IF A VALUE WAS PASSED IN PARAMETER 4 AND, IF SO, ASSIGN TO "timeZone"

if [ "$4" != "" ] && [ "$timeZone" == "" ]
then
    timeZone=$4
fi

osx=$(/usr/bin/defaults read /System/Library/CoreServices/SystemVersion ProductVersion)
maj=$(/usr/bin/defaults read /System/Library/CoreServices/SystemVersion ProductVersion | awk '{print substr($1,1,2)}')
ref=$(/usr/bin/defaults read /System/Library/CoreServices/SystemVersion ProductVersion | awk '{print substr($1,4,2)}')

if [ $maj -gt 10 ]
then
    echo
    echo "Check OS string format & OS X systemsetup utility for script compatibility with OS X version $osx"
    echo
    exit
fi

if [ "$timeZone" != "" ]
then
    if [ $ref -lt 5 ]
    then
        echo
        echo "Setting time zone for OS X $osx..."
        /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Support/systemsetup -settimezone "$timeZone"
        /usr/bin/killall SystemUIServer
        echo "Refreshing the clock in the Menu Bar..."
        echo
    else
        echo
        echo "Setting time zone for OS X $osx..."
        /usr/sbin/systemsetup -settimezone "$timeZone"
        /usr/bin/killall SystemUIServer
        echo "Refreshing the clock in the Menu Bar..."
        echo
    fi
else
    echo
    echo "Error: The timeZone variable is not populated. Press the return key "
    echo "to generate a list of valid time zones. Select & copy the desired "
    echo "time zone from the list & paste into the script on the line reading "
    echo
    echo " timeZone="" "
    echo
    read -p ""
   /usr/sbin/systemsetup listtimezones
   exit
fi

tommillard
New Contributor II

@andrew.nicholas is correct. I was speaking to the checkbox in the next one over. @larry_barrett your video shows what IS working, but the checkbox for this screenshot isn't working, which did before Catalina.

a04bcc2c980c4badb15d80be4bccd4b3

macbrun
New Contributor III

I'm probably not adding anything here but a data point, however I just tested @smpotter 's script via Self Service on a non-T2 chip MacBook Pro running macOS 10.15.4 and it worked fine (after reboot). Perhaps the T2 security chip is causing different behavior? I don't have a T2 Mac to test this on right now.

tommillard
New Contributor II

@macbrun I just tried running it and that checkbox does indeed become checked after a reboot. Thanks :) This is on a T2 mac.

larry_barrett
Valued Contributor

I am not on a T2 mac :)

Dominic_Jaeckel
New Contributor II

Hi,

sorry that I didn't reply anymore, but the script is working you just have to reboot after execution and the box is checked.

Works on Mojave and Catalina.

Best Regards,

Dominic

Stubakka
Contributor II

Hello all, I'm trying to make this an "Available offline" self service item to help with some of my older MacBook Pros lose battery power totally and the time and date reset on them to like 2010, and wrong time etc. I tested it on a system and set the date back a year and it worked however when I set it to 2010, and tired to run it from self service which was no longer connecting to the JAMF server it fails. how does the make available offline checkbox work? I thought it would cache the policy locally

Nix4Life
Valued Contributor

@Stubakka

I used the following script tied to a launch dameon that ran every 10 mins to help with the same issue you are having. It is based on a script by @mm2270. If it can reach your ntp server, it does nothing. If it can't it will default to apple to set time. It helped alot with Macbooks that were left in drawers or off network fro a bit. we saw an added benefit for users that travel in that when the time zone changed, Outlook prompted them to update their Calendars and meetings
launchd created using launchd

#!/bin/sh
#######################################################################################################################
#                                                                                                                     #
# This script is activated by a launchagent to set the correct time on an Apple device                                #
# Based on  NetworktimeSwitcher Script  by  mm2270 adapated for  macOS Mojave                                         #
#                                                                                                                     #               
#######################################################################################################################
ping -c 5 ntp.example.com > /dev/null
casTest=$?

if [[ $casTest == 0 ]]; then
    ntpServer=`/usr/sbin/systemsetup -getnetworktimeserver | awk '{print $4}'`
        if [[ $ntpServer != "ntp.example.com" ]]; then
            /usr/sbin/systemsetup -setusingnetworktime off
            sudo sntp -sS ntp.example.com
            /usr/sbin/systemsetup -setusingnetworktime on
        else
            exit 0
        fi
else
ping -c 5 ntp1.example.com > /dev/null
casTest=$?

if [[ $casTest == 0 ]]; then
    ntpServer=`/usr/sbin/systemsetup -getnetworktimeserver | awk '{print $4}'`
        if [[ $ntpServer != "ntp1.example.com" ]]; then
             /usr/sbin/systemsetup -setusingnetworktime off
             sudo sntp -sS ntp1.example.com
             /usr/sbin/systemsetup -setusingnetworktime on
        else
            exit 0
        fi
else
    ntpServer=`/usr/sbin/systemsetup -getnetworktimeserver | awk '{print $4}'`
        if [[ $ntpServer != "time.apple.com" ]]; then
            /usr/sbin/systemsetup -setusingnetworktime off
            sudo sntp -sS time.apple.com
            exit 0
        fi
fi
fi

mario
New Contributor III

The example scripts here were no longer successfully enabling "set time zone automatically using current location setting" in my testing on latest macOS Monterey (not sure when these broke). Issue is that if you run `sudo defaults write` on preferences in `/private/var/db/timed` , macOS won't honor the settings since doing so will change the owner to root:wheel. Fix is to change ownership to _timed:_timed:

timedPrefs="/private/var/db/timed/Library/Preferences/com.apple.timed.plist"
dateTimePrefs="/private/var/db/timed/Library/Preferences/com.apple.preferences.datetime.plist"

# Set preferences.
/usr/bin/defaults write "$timedPrefs" TMAutomaticTimeZoneEnabled -bool TRUE
/usr/bin/defaults write "$dateTimePrefs" timezoneset -bool TRUE
/usr/sbin/chown "_timed:_timed" "$timedPrefs" "$dateTimePrefs"

As a bonus, the setting appears to be loaded automatically after running this script, and a restart is no longer required.

dan-snelson
Valued Contributor II

The timing of your post is quite serendipitous, @mario 

However, I ended up having to use `systemsetup` instead. 

 

timedPrefs="/private/var/db/timed/Library/Preferences/com.apple.timed.plist"
defaults read $timedPrefs
{
    NtpUseServicePort = 0;
    TMAutomaticTimeOnlyEnabled = 1;
    TMAutomaticTimeZoneEnabled = 1;
}

dateTimePrefs="/private/var/db/timed/Library/Preferences/com.apple.preferences.datetime.plist"
defaults read $dateTimePrefs
{
    timezoneset = 1;
}

ls -lah $timedPrefs
-rw-------  1 _timed  _timed   135B May 19 10:25 /private/var/db/timed/Library/Preferences/com.apple.timed.plist

ls -lah $dateTimePrefs
-rw-------  1 _timed  _timed    59B Apr 18 10:43 /private/var/db/timed/Library/Preferences/com.apple.preferences.datetime.plist

defaults delete $timedPrefs TMAutomaticTimeZoneEnabled
defaults read $timedPrefs
{
    NtpUseServicePort = 0;
    TMAutomaticTimeOnlyEnabled = 1;
}

ls -lah $timedPrefs
-rw-------  1 root  wheel   101B May 19 10:39 /private/var/db/timed/Library/Preferences/com.apple.timed.plist

/usr/sbin/chown "_timed:_timed" "$timedPrefs" "$dateTimePrefs"

ls -lah $timedPrefs
-rw-------  1 _timed  _timed   101B May 19 10:39 /private/var/db/timed/Library/Preferences/com.apple.timed.plist

 

 

After all of the above, the time was still incorrect (i.e., the Mac had been in PDT and was back in MDT, but still showed as PDT).

 

systemsetup -gettimezone
Time Zone: America/Los_Angeles

systemsetup -settimezone 'America/Denver'
2022-05-19 17:43:43.895 systemsetup[10464:153418] ### Error:-99 File:/AppleInternal/Library/BuildRoots/66382bca-8bca-11ec-aade-6613bcf0e2ee/Library/Caches/com.apple.xbs/Sources/Admin/InternetServices.m Line:379
Set TimeZone: America/Denver

systemsetup -gettimezone
Time Zone: America/Denver

date
Thu May 19 11:43:59 MDT 2022

 

 

With the error, I was surprised this actually worked (and persisted after a reboot).

The Mac is running macOS `12.3.1` and if I observed the error in `12.4`, I would have filed feedback with Apple.