Skip to content

Instantly share code, notes, and snippets.

@porteusconf
Last active October 6, 2020 04:51
Show Gist options
  • Save porteusconf/af95b8595c697a614169e3dfd9680010 to your computer and use it in GitHub Desktop.
Save porteusconf/af95b8595c697a614169e3dfd9680010 to your computer and use it in GitHub Desktop.
howto manually set macos system date hwclock using file date or "YYYY-MM-DD hh:mm:ss" converted to required "+%m%d%H%M%Y"
### /usr/local/bin/datefix.sh
echo "DEMO: HowTo Change macos system date to date of /var/log/daily.out or manually"
echo "If dead bat or hwclock wrong, should work in single-user mode/recovery"
set -x
ls -l /var/log/daily.out
date -r /var/log/daily.out
date -r /var/log/daily.out "+%m%d%H%M%Y.%S"
date -ur /var/log/daily.out
date -ur /var/log/daily.out +"%Y-%m-%d %H:%M:%S"
date -ur /var/log/daily.out "+%m%d%H%M%Y.%S"
echo 'sudo date -u $(date -ur /var/log/daily.out "+%m%d%H%M%Y.%S") # or manually:'
## 2020-10-31 23:59:58 = 2 seconds before midnight on year 2020 month 10. and day 31
echo 'sudo date -u $(date -ju -f "%Y-%m-%d %T" "2020-10-31 23:59:58" "+%m%d%H%M%Y") '
## Once date is almost correct, and network is up, set time exactly from internet
echo 'sudo sntp -sS time.apple.com' # 10.13 and newer
echo 'sudo ntpdate -u time.apple.com' # 10.12 and earlier
echo 'date -u ; date # Should now be correct date in UTC, and in local time zone'
@porteusconf
Copy link
Author

Hopefully using -u to set date in UTC avoids dealing with timezone issues? In any case, be warned this is not thoroughly tested if at all, but did try to use output of date to debug date input conversions as shown at: https://stackoverflow.com/questions/38348963/how-to-convert-date-string-to-epoch-timestamp-with-the-os-x-bsd-date-command

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment