Skip to main content
Question

Force checking "Set time zone automatically using current location"


Forum|alt.badge.img+4

Hi all,

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

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

Forum|alt.badge.img+12
  • Valued Contributor
  • 191 replies
  • February 10, 2020

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.


Forum|alt.badge.img+7
  • New Contributor
  • 24 replies
  • February 10, 2020

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

1#!/bin/bash
2
3## enabling location services
4/usr/bin/defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd LocationServicesEnabled -int 1
5
6uuid=$(/usr/sbin/system_profiler SPHardwareDataType | grep "Hardware UUID" | cut -c22-57)
7/usr/bin/defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd.$uuid LocationServicesEnabled -int 1
8
9## configure automatic timezone
10/usr/bin/defaults write /Library/Preferences/com.apple.timezone.auto Active -bool YES
11/usr/bin/defaults write /private/var/db/timed/Library/Preferences/com.apple.timed.plist TMAutomaticTimeOnlyEnabled -bool YES
12/usr/bin/defaults write /private/var/db/timed/Library/Preferences/com.apple.timed.plist TMAutomaticTimeZoneEnabled -bool YES
13/usr/sbin/systemsetup -setusingnetworktime on
14/usr/sbin/systemsetup -gettimezone
15/usr/sbin/systemsetup -getnetworktimeserver

Forum|alt.badge.img+13
  • Contributor
  • 341 replies
  • February 11, 2020

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


Forum|alt.badge.img+4
  • Author
  • New Contributor
  • 2 replies
  • February 12, 2020

Hi,

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


Forum|alt.badge.img+13
  • Contributor
  • 341 replies
  • February 12, 2020

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.


Forum|alt.badge.img+1
  • New Contributor
  • 3 replies
  • April 24, 2020

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.


Forum|alt.badge.img+13

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


dan-snelson
Forum|alt.badge.img+28
  • Honored Contributor
  • 632 replies
  • April 24, 2020

@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?


Forum|alt.badge.img+13

@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.


Forum|alt.badge.img+13

@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:

1#!/bin/sh
2####################################################################################################
3#
4# Copyright (c) 2010, JAMF Software, LLC. All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions are met:
8# * Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10# * Redistributions in binary form must reproduce the above copyright
11# notice, this list of conditions and the following disclaimer in the
12# documentation and/or other materials provided with the distribution.
13# * Neither the name of the JAMF Software, LLC nor the
14# names of its contributors may be used to endorse or promote products
15# derived from this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY JAMF SOFTWARE, LLC "AS IS" AND ANY
18# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20# DISCLAIMED. IN NO EVENT SHALL JAMF SOFTWARE, LLC BE LIABLE FOR ANY
21# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27#
28####################################################################################################
29#
30# SUPPORT FOR THIS PROGRAM
31#
32# This program is distributed "as is" by JAMF Software, LLC's Resource Kit team. For more
33# information or support for the Resource Kit, please utilize the following resources:
34#
35# http://list.jamfsoftware.com/mailman/listinfo/resourcekit
36#
37# http://www.jamfsoftware.com/support/resource-kit
38#
39# Please reference our SLA for information regarding support of this application:
40#
41# http://www.jamfsoftware.com/support/resource-kit-sla
42#
43####################################################################################################
44#
45# ABOUT THIS PROGRAM
46#
47# NAME
48# setTimeZone.sh -- Set the time zone
49#
50# SYNOPSIS
51# sudo setTimeZone.sh
52# sudo setTimeZone.sh <mountPoint> <computerName> <currentUsername> <timeZone>
53#
54# If the $timeZone parameter is specified (parameter 4), this is the time sone that will be set.
55#
56# If no parameter is specified for parameter 4, the hardcoded value in the script will be used.
57#
58# DESCRIPTION
59# This script sets the system time zone as reflected in the Date & Time preference pane with the
60# System Preferences application. It has been designed to work on Mac OS X 10.3 and higher.
61#
62# A list of supported time zone entries can be found by running the command:
63#
64# For Mac OS X 10.5 or later:
65#
66# /usr/sbin/systemsetup -listtimezones
67#
68# For Mac OS X 10.4 or earlier:
69#
70# /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Support/systemsetup -listtimezones
71#
72# The system time zone will be set according to the value specified in the paramter $timeZone.
73# It can be used with a hardcoded value in the script, or read in as a parameter. Since the
74# Casper Suite defines the first three parameters as (1) Mount Point, (2) Computer Name and
75# (3) username, we are using the forth parameter ($4) as the passable parameter. If no parameter
76# is passed, then the hardcoded value will be used.
77#
78####################################################################################################
79#
80# HISTORY
81#
82# Version: 1.0
83#
84# - Created by Nick Amundsen on August 5th, 2008
85#
86# Version: 2.0
87#
88# - Updated by Brock Walters October 28 2014
89#
90####################################################################################################
91#
92# DEFINE VARIABLES & READ IN PARAMETERS
93#
94####################################################################################################
95#
96# A HARDCODED VALUE FOR "timeZone" CAN BE SET BELOW.
97#
98# A list of accepted time zone values can be generated using the following command in Terminal:
99#
100# sudo systemsetup -listtimezones
101#
102# Delete the double quotes and replace with the desired time zone name, e.g. timeZone=Pacific/Honolulu
103# If this script is to be deployed via policy using the JSS leave the next line as is.
104#
105####################################################################################################
106
107timeZone=""
108
109####################################################################################################
110#
111# SCRIPT CONTENTS - DO NOT MODIFY BELOW THIS LINE
112#
113####################################################################################################
114
115# CHECK TO SEE IF A VALUE WAS PASSED IN PARAMETER 4 AND, IF SO, ASSIGN TO "timeZone"
116
117if [ "$4" != "" ] && [ "$timeZone" == "" ]
118then
119 timeZone=$4
120fi
121
122osx=$(/usr/bin/defaults read /System/Library/CoreServices/SystemVersion ProductVersion)
123maj=$(/usr/bin/defaults read /System/Library/CoreServices/SystemVersion ProductVersion | awk '{print substr($1,1,2)}')
124ref=$(/usr/bin/defaults read /System/Library/CoreServices/SystemVersion ProductVersion | awk '{print substr($1,4,2)}')
125
126if [ $maj -gt 10 ]
127then
128 echo
129 echo "Check OS string format & OS X systemsetup utility for script compatibility with OS X version $osx"
130 echo
131 exit
132fi
133
134if [ "$timeZone" != "" ]
135then
136 if [ $ref -lt 5 ]
137 then
138 echo
139 echo "Setting time zone for OS X $osx..."
140 /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Support/systemsetup -settimezone "$timeZone"
141 /usr/bin/killall SystemUIServer
142 echo "Refreshing the clock in the Menu Bar..."
143 echo
144 else
145 echo
146 echo "Setting time zone for OS X $osx..."
147 /usr/sbin/systemsetup -settimezone "$timeZone"
148 /usr/bin/killall SystemUIServer
149 echo "Refreshing the clock in the Menu Bar..."
150 echo
151 fi
152else
153 echo
154 echo "Error: The timeZone variable is not populated. Press the return key "
155 echo "to generate a list of valid time zones. Select & copy the desired "
156 echo "time zone from the list & paste into the script on the line reading "
157 echo
158 echo " timeZone="" "
159 echo
160 read -p ""
161 /usr/sbin/systemsetup listtimezones
162 exit
163fi

Forum|alt.badge.img+1
  • New Contributor
  • 3 replies
  • April 24, 2020

@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.


macbrun
Forum|alt.badge.img+7
  • Contributor
  • 22 replies
  • April 24, 2020

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.


Forum|alt.badge.img+1
  • New Contributor
  • 3 replies
  • April 24, 2020

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


Forum|alt.badge.img+13

I am not on a T2 mac :)


Forum|alt.badge.img+4

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


Forum|alt.badge.img+13
  • Valued Contributor
  • 95 replies
  • June 15, 2020

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


Forum|alt.badge.img+13
  • Honored Contributor
  • 550 replies
  • June 16, 2020

@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

1#!/bin/sh
2#######################################################################################################################
3# #
4# This script is activated by a launchagent to set the correct time on an Apple device #
5# Based on NetworktimeSwitcher Script by mm2270 adapated for macOS Mojave #
6# #
7#######################################################################################################################
8ping -c 5 ntp.example.com > /dev/null
9casTest=$?
10
11if [[ $casTest == 0 ]]; then
12 ntpServer=`/usr/sbin/systemsetup -getnetworktimeserver | awk '{print $4}'`
13 if [[ $ntpServer != "ntp.example.com" ]]; then
14 /usr/sbin/systemsetup -setusingnetworktime off
15 sudo sntp -sS ntp.example.com
16 /usr/sbin/systemsetup -setusingnetworktime on
17 else
18 exit 0
19 fi
20else
21ping -c 5 ntp1.example.com > /dev/null
22casTest=$?
23
24if [[ $casTest == 0 ]]; then
25 ntpServer=`/usr/sbin/systemsetup -getnetworktimeserver | awk '{print $4}'`
26 if [[ $ntpServer != "ntp1.example.com" ]]; then
27 /usr/sbin/systemsetup -setusingnetworktime off
28 sudo sntp -sS ntp1.example.com
29 /usr/sbin/systemsetup -setusingnetworktime on
30 else
31 exit 0
32 fi
33else
34 ntpServer=`/usr/sbin/systemsetup -getnetworktimeserver | awk '{print $4}'`
35 if [[ $ntpServer != "time.apple.com" ]]; then
36 /usr/sbin/systemsetup -setusingnetworktime off
37 sudo sntp -sS time.apple.com
38 exit 0
39 fi
40fi
41fi

Forum|alt.badge.img+8
  • New Contributor
  • 12 replies
  • May 19, 2022

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
Forum|alt.badge.img+28
  • Honored Contributor
  • 632 replies
  • May 19, 2022
mario11 wrote:

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.


The timing of your post is quite serendipitous, @mario11 

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.


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings