xref: /freebsd/usr.sbin/bsdconfig/datetime/share/datetime.subr (revision 8cfe06ee4415795cc77934fb5c17bdf2c2fd3816)
1*8cfe06eeSDevin Teskeif [ ! "$_DATETIME_DATETIME_SUBR" ]; then _DATETIME_DATETIME_SUBR=1
2*8cfe06eeSDevin Teske#
3*8cfe06eeSDevin Teske# Copyright (c) 2026 Devin Teske
4*8cfe06eeSDevin Teske# All rights reserved.
5*8cfe06eeSDevin Teske#
6*8cfe06eeSDevin Teske# Redistribution and use in source and binary forms, with or without
7*8cfe06eeSDevin Teske# modification, are permitted provided that the following conditions
8*8cfe06eeSDevin Teske# are met:
9*8cfe06eeSDevin Teske# 1. Redistributions of source code must retain the above copyright
10*8cfe06eeSDevin Teske#    notice, this list of conditions and the following disclaimer.
11*8cfe06eeSDevin Teske# 2. Redistributions in binary form must reproduce the above copyright
12*8cfe06eeSDevin Teske#    notice, this list of conditions and the following disclaimer in the
13*8cfe06eeSDevin Teske#    documentation and/or other materials provided with the distribution.
14*8cfe06eeSDevin Teske#
15*8cfe06eeSDevin Teske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*8cfe06eeSDevin Teske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*8cfe06eeSDevin Teske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*8cfe06eeSDevin Teske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*8cfe06eeSDevin Teske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*8cfe06eeSDevin Teske# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*8cfe06eeSDevin Teske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*8cfe06eeSDevin Teske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*8cfe06eeSDevin Teske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*8cfe06eeSDevin Teske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*8cfe06eeSDevin Teske# SUCH DAMAGE.
26*8cfe06eeSDevin Teske#
27*8cfe06eeSDevin Teske#
28*8cfe06eeSDevin Teske############################################################ INCLUDES
29*8cfe06eeSDevin Teske
30*8cfe06eeSDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig"
31*8cfe06eeSDevin Teske. $BSDCFG_SHARE/common.subr || exit 1
32*8cfe06eeSDevin Teskef_dprintf "%s: loading includes..." datetime/datetime.subr
33*8cfe06eeSDevin Teskef_include $BSDCFG_SHARE/dialog.subr
34*8cfe06eeSDevin Teske
35*8cfe06eeSDevin TeskeBSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="085.datetime"
36*8cfe06eeSDevin Teskef_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
37*8cfe06eeSDevin Teske
38*8cfe06eeSDevin Teske############################################################ FUNCTIONS
39*8cfe06eeSDevin Teske
40*8cfe06eeSDevin Teske# f_datetime_adjkerntz
41*8cfe06eeSDevin Teske#
42*8cfe06eeSDevin Teske# Best-effort CMOS sync after changing the system clock with date(1).
43*8cfe06eeSDevin Teske#
44*8cfe06eeSDevin Teskef_datetime_adjkerntz()
45*8cfe06eeSDevin Teske{
46*8cfe06eeSDevin Teske	f_quietly adjkerntz -a
47*8cfe06eeSDevin Teske}
48*8cfe06eeSDevin Teske
49*8cfe06eeSDevin Teske# f_dialog_calendar_set_date
50*8cfe06eeSDevin Teske#
51*8cfe06eeSDevin Teske# Prompt with dialog(1) --calendar and set the system date, preserving the
52*8cfe06eeSDevin Teske# current time-of-day. Returns dialog status (Cancel/ESC) without dying.
53*8cfe06eeSDevin Teske#
54*8cfe06eeSDevin Teskef_dialog_calendar_set_date()
55*8cfe06eeSDevin Teske{
56*8cfe06eeSDevin Teske	local prompt="$msg_set_date"
57*8cfe06eeSDevin Teske	local hline="$hline_arrows_tab_enter"
58*8cfe06eeSDevin Teske	local cheight cwidth input_date ret_date retval
59*8cfe06eeSDevin Teske	local cur_tod setstr
60*8cfe06eeSDevin Teske
61*8cfe06eeSDevin Teske	f_dialog_calendar_size cheight cwidth \
62*8cfe06eeSDevin Teske		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$prompt" "$hline"
63*8cfe06eeSDevin Teske
64*8cfe06eeSDevin Teske	input_date=$( date "+%d %m %Y" )
65*8cfe06eeSDevin Teske	ret_date=$( eval $DIALOG \
66*8cfe06eeSDevin Teske		--title \"\$DIALOG_TITLE\"         \
67*8cfe06eeSDevin Teske		--backtitle \"\$DIALOG_BACKTITLE\" \
68*8cfe06eeSDevin Teske		--hline \"\$hline\"                \
69*8cfe06eeSDevin Teske		--ok-label \"\$msg_ok\"            \
70*8cfe06eeSDevin Teske		--cancel-label \"\$msg_cancel\"    \
71*8cfe06eeSDevin Teske		--calendar \"\$prompt\"            \
72*8cfe06eeSDevin Teske		$cheight $cwidth                   \
73*8cfe06eeSDevin Teske		$input_date                        \
74*8cfe06eeSDevin Teske		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
75*8cfe06eeSDevin Teske	)
76*8cfe06eeSDevin Teske	retval=$?
77*8cfe06eeSDevin Teske	f_dialog_data_sanitize ret_date
78*8cfe06eeSDevin Teske	f_dprintf "retval=%u ret_date=[%s]" $retval "$ret_date"
79*8cfe06eeSDevin Teske	[ $retval -eq $DIALOG_OK ] || return $retval
80*8cfe06eeSDevin Teske
81*8cfe06eeSDevin Teske	cur_tod=$( date "+%H:%M:%S" )
82*8cfe06eeSDevin Teske	setstr=$( date -j -f "%d/%m/%Y %T" -- \
83*8cfe06eeSDevin Teske		"$ret_date $cur_tod" "+%Y%m%d%H%M.%S" ) ||
84*8cfe06eeSDevin Teske		f_die 1 "$msg_unable_to_set_date"
85*8cfe06eeSDevin Teske
86*8cfe06eeSDevin Teske	f_eval_catch "$0" date 'date "%s"' "$setstr" ||
87*8cfe06eeSDevin Teske		f_die 1 "$msg_unable_to_set_date"
88*8cfe06eeSDevin Teske	f_datetime_adjkerntz
89*8cfe06eeSDevin Teske	f_show_msg "$msg_system_date_set" "$( date )"
90*8cfe06eeSDevin Teske	return $SUCCESS
91*8cfe06eeSDevin Teske}
92*8cfe06eeSDevin Teske
93*8cfe06eeSDevin Teske# f_dialog_timebox_set_time
94*8cfe06eeSDevin Teske#
95*8cfe06eeSDevin Teske# Prompt with dialog(1) --timebox and set the system time, preserving the
96*8cfe06eeSDevin Teske# current calendar date. Returns dialog status (Cancel/ESC) without dying.
97*8cfe06eeSDevin Teske#
98*8cfe06eeSDevin Teskef_dialog_timebox_set_time()
99*8cfe06eeSDevin Teske{
100*8cfe06eeSDevin Teske	local prompt="$msg_set_time"
101*8cfe06eeSDevin Teske	local hline="$hline_arrows_tab_enter"
102*8cfe06eeSDevin Teske	local input_time ret_time retval
103*8cfe06eeSDevin Teske	local cur_day setstr
104*8cfe06eeSDevin Teske
105*8cfe06eeSDevin Teske	#
106*8cfe06eeSDevin Teske	# bsddialog(1) --timebox wants total rows (min 7), not dialog(1)-style
107*8cfe06eeSDevin Teske	# text height. Match bsdinstall time: autosize height, width 40.
108*8cfe06eeSDevin Teske	#
109*8cfe06eeSDevin Teske	input_time=$( date "+%H %M %S" )
110*8cfe06eeSDevin Teske	ret_time=$( eval $DIALOG \
111*8cfe06eeSDevin Teske		--title \"\$DIALOG_TITLE\"         \
112*8cfe06eeSDevin Teske		--backtitle \"\$DIALOG_BACKTITLE\" \
113*8cfe06eeSDevin Teske		--hline \"\$hline\"                \
114*8cfe06eeSDevin Teske		--ok-label \"\$msg_ok\"            \
115*8cfe06eeSDevin Teske		--cancel-label \"\$msg_cancel\"    \
116*8cfe06eeSDevin Teske		--timebox \"\$prompt\"             \
117*8cfe06eeSDevin Teske		0 40                               \
118*8cfe06eeSDevin Teske		$input_time                        \
119*8cfe06eeSDevin Teske		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
120*8cfe06eeSDevin Teske	)
121*8cfe06eeSDevin Teske	retval=$?
122*8cfe06eeSDevin Teske	f_dialog_data_sanitize ret_time
123*8cfe06eeSDevin Teske	f_dprintf "retval=%u ret_time=[%s]" $retval "$ret_time"
124*8cfe06eeSDevin Teske	[ $retval -eq $DIALOG_OK ] || return $retval
125*8cfe06eeSDevin Teske
126*8cfe06eeSDevin Teske	cur_day=$( date "+%d/%m/%Y" )
127*8cfe06eeSDevin Teske	setstr=$( date -j -f "%d/%m/%Y %T" -- \
128*8cfe06eeSDevin Teske		"$cur_day $ret_time" "+%Y%m%d%H%M.%S" ) ||
129*8cfe06eeSDevin Teske		f_die 1 "$msg_unable_to_set_time"
130*8cfe06eeSDevin Teske
131*8cfe06eeSDevin Teske	f_eval_catch "$0" date 'date "%s"' "$setstr" ||
132*8cfe06eeSDevin Teske		f_die 1 "$msg_unable_to_set_time"
133*8cfe06eeSDevin Teske	f_datetime_adjkerntz
134*8cfe06eeSDevin Teske	f_show_msg "$msg_system_time_set" "$( date )"
135*8cfe06eeSDevin Teske	return $SUCCESS
136*8cfe06eeSDevin Teske}
137*8cfe06eeSDevin Teske
138*8cfe06eeSDevin Teske############################################################ MAIN
139*8cfe06eeSDevin Teske
140*8cfe06eeSDevin Teskef_dprintf "%s: Successfully loaded." datetime/datetime.subr
141*8cfe06eeSDevin Teske
142*8cfe06eeSDevin Teskefi # ! $_DATETIME_DATETIME_SUBR
143