#!/bin/bash
#
sudo systemsetup -setusingnetworktime off #关闭网络时间同步
#echo "请输入您的时间,时间格式为 2023-01-01 01:01" #echo "Please enter your time"
#read time
time=$(/usr/bin/osascript -e 'display dialog "请输入日期,格式为 年-月-日 时:分:秒 例如 2026-12-8 13:30:29" default answer "" with icon note buttons { "Continue"} default button "Continue"
set result1 to result
set the text_data to text returned of the result
set the button_data to button returned of the result1
if button_data = "Cancel" then
return ""
end if
return text_data')
if [ -z "$time" ];then
exit 0;
fi
echo "时间 是 $time" # echo "time is $time"
month=`echo "${time}" | cut -f2 -d-`
if [ `echo ${month} | wc -c` = 2 ];then month="0${month}" ; fi
day=`echo "${time}" | cut -f3 -d- | cut -f1 -d' '`
if [ `echo ${day} | wc -c` = 2 ];then day="0${day}" ; fi
hour=`echo "${time}" | cut -f3 -d- | cut -f2 -d' ' | cut -f1 -d:`
if [ `echo ${hour} | wc -c` = 2 ];then hour="0${hour}" ; fi
minute=`echo "${time}" | cut -f3 -d- | cut -f2 -d' ' | cut -f2 -d:`
if [ `echo ${minute} | wc -c` = 2 ];then minute="0${minute}" ; fi
year=`echo "${time}" | cut -f1 -d-`
second=`echo "${time}" | cut -f3 -d:`
if [ `echo ${second} | wc -c` = 2 ];then second="0${second}" ; fi
#echo "${month}${day}${hour}${minute}${year}"
sudo date "${month}${day}${hour}${minute}${year}.${second}"
#echo "时间修改成功,是 `date` "
if [ $? = 0 ]; then
(/usr/bin/osascript -e 'display dialog "日期修改成功"')
else
(/usr/bin/osascript -e 'display dialog "日期修改失败,可能是日期格式不对!"')
fi