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