1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
#!/usr/bin/python
import tkinter as tk
from tkinter import ttk, messagebox
import subprocess
import requests
import datetime
import pytz
import time
class DateTimeSetter(tk.Tk):
def __init__(self):
super().__init__()
self.title("dateTimeSetter")
self.geometry("640x480")
self.resizable(True, True)
self.attributes("-type", "dialog") # make the window floating
self.create_widgets()
self.populate_fields()
def create_widgets(self):
# automatic time setting checkbox
automatic_time_label = ttk.Label(
self, text="Set the date, time, and timezone automatically?")
automatic_time_label.pack(pady=5)
self.automatic_time_check = ttk.Checkbutton(
self, text="Yes", command=self.on_automatic_time_toggled)
self.automatic_time_check.pack(pady=5)
# date entry fields
date_frame = ttk.LabelFrame(self, text="Date")
date_frame.pack(pady=10, padx=10, fill="x")
ttk.Label(date_frame, text="YYYY:").grid(
row=0, column=0, padx=5, pady=5)
self.year_var = tk.StringVar()
self.year_combo = ttk.Combobox(date_frame, textvariable=self.year_var, values=[str(
year) for year in range(1970, 2039)], state="normal")
self.year_combo.grid(row=0, column=1, padx=5, pady=5)
ttk.Label(date_frame, text="MM:").grid(row=0, column=2, padx=5, pady=5)
self.month_var = tk.StringVar()
self.month_combo = ttk.Combobox(date_frame, textvariable=self.month_var, values=[str(
month) for month in range(1, 13)], state="normal")
self.month_combo.grid(row=0, column=3, padx=5, pady=5)
ttk.Label(date_frame, text="DD:").grid(row=0, column=4, padx=5, pady=5)
self.day_var = tk.StringVar()
self.day_combo = ttk.Combobox(
date_frame, textvariable=self.day_var, values=[str(day) for day in range(1, 32)], state="normal")
self.day_combo.grid(row=0, column=5, padx=5, pady=5)
# time entry fields
time_frame = ttk.LabelFrame(self, text="Time (24hr)")
time_frame.pack(pady=10, padx=10, fill="x")
ttk.Label(time_frame, text=" HH:").grid(
row=0, column=0, padx=5, pady=5)
self.hour_var = tk.StringVar()
self.hour_combo = ttk.Combobox(time_frame, textvariable=self.hour_var, values=[str(
hour) for hour in range(0, 24)], state="normal")
self.hour_combo.grid(row=0, column=1, padx=5, pady=5)
ttk.Label(time_frame, text="MM:").grid(row=0, column=2, padx=5, pady=5)
self.minute_var = tk.StringVar()
self.minute_combo = ttk.Combobox(time_frame, textvariable=self.minute_var, values=[str(
minute) for minute in range(0, 60)], state="normal")
self.minute_combo.grid(row=0, column=3, padx=5, pady=5)
ttk.Label(time_frame, text="SS:").grid(row=0, column=4, padx=5, pady=5)
self.second_var = tk.StringVar()
self.second_combo = ttk.Combobox(time_frame, textvariable=self.second_var, values=[str(
second) for second in range(0, 60)], state="normal")
self.second_combo.grid(row=0, column=5, padx=5, pady=5)
# timezone entry field
timezone_frame = ttk.LabelFrame(self, text="Timezone")
timezone_frame.pack(pady=10, padx=10, fill="x")
ttk.Label(timezone_frame, text="Timezone:").grid(
row=0, column=0, padx=5, pady=5)
self.timezone_var = tk.StringVar()
self.timezone_combo = ttk.Combobox(
timezone_frame, textvariable=self.timezone_var, values=pytz.all_timezones, state="normal")
self.timezone_combo.grid(row=0, column=1, padx=5, pady=5)
# set-local-rtc checkbox
self.local_rtc_check = ttk.Checkbutton(
timezone_frame, text="Set local RTC")
self.local_rtc_check.grid(
row=1, column=0, columnspan=2, padx=5, pady=5)
# apply button
button_frame = ttk.Frame(self)
button_frame.pack(pady=20)
self.apply_button = ttk.Button(
button_frame, text="Apply All", command=self.on_apply_clicked)
self.apply_button.grid(row=0, column=0, padx=5)
self.timezone_apply_button = ttk.Button(
button_frame, text="Apply Timezone", command=self.on_timezone_apply_clicked)
self.timezone_apply_button.grid(row=0, column=1, padx=5)
self.date_time_apply_button = ttk.Button(
button_frame, text="Apply Date & Time", command=self.on_date_time_apply_clicked)
self.date_time_apply_button.grid(row=0, column=2, padx=5)
self.local_rtc_apply_button = ttk.Button(
button_frame, text="Apply Local RTC", command=self.on_local_rtc_apply_clicked)
self.local_rtc_apply_button.grid(row=0, column=3, padx=5)
self.update_apply_button_state()
# Set trace on all variables to call update_apply_button_state when they change
self.year_var.trace_add("write", self.update_apply_button_state)
self.month_var.trace_add("write", self.update_apply_button_state)
self.day_var.trace_add("write", self.update_apply_button_state)
self.hour_var.trace_add("write", self.update_apply_button_state)
self.minute_var.trace_add("write", self.update_apply_button_state)
self.second_var.trace_add("write", self.update_apply_button_state)
self.timezone_var.trace_add("write", self.update_apply_button_state)
def on_automatic_time_toggled(self):
if self.automatic_time_check.instate(['selected']):
self.year_combo.state(['disabled'])
self.month_combo.state(['disabled'])
self.day_combo.state(['disabled'])
self.hour_combo.state(['disabled'])
self.minute_combo.state(['disabled'])
self.second_combo.state(['disabled'])
self.timezone_combo.state(['disabled'])
else:
self.year_combo.state(['!disabled'])
self.month_combo.state(['!disabled'])
self.day_combo.state(['!disabled'])
self.hour_combo.state(['!disabled'])
self.minute_combo.state(['!disabled'])
self.second_combo.state(['!disabled'])
self.timezone_combo.state(['!disabled'])
self.update_apply_button_state()
def update_apply_button_state(self, *_):
if self.automatic_time_check.instate(['selected']):
self.apply_button.state(['!disabled'])
self.timezone_apply_button.state(['disabled'])
self.date_time_apply_button.state(['disabled'])
self.local_rtc_apply_button.state(['disabled'])
else:
all_date_filled = all([
self.year_combo.get(),
self.month_combo.get(),
self.day_combo.get(),
])
all_time_filled = all([
self.hour_combo.get(),
self.minute_combo.get(),
self.second_combo.get(),
])
timezone_filled = self.timezone_combo.get()
all_filled = all_date_filled and all_time_filled and timezone_filled
if all_filled:
self.apply_button.state(['!disabled'])
else:
self.apply_button.state(['disabled'])
if timezone_filled and self.validate_timezone(timezone_filled):
self.timezone_apply_button.state(['!disabled'])
else:
self.timezone_apply_button.state(['disabled'])
if all_date_filled and all_time_filled:
self.date_time_apply_button.state(['!disabled'])
else:
self.date_time_apply_button.state(['disabled'])
self.local_rtc_apply_button.state(['!disabled'])
def on_apply_clicked(self):
automatic_time = self.automatic_time_check.instate(['selected'])
local_rtc = self.local_rtc_check.instate(['selected'])
if automatic_time:
subprocess.run(["timedatectl", "set-ntp", "true"])
try:
automatic_timezone_output = subprocess.run(
["curl", "--fail", "https://ipinfo.io/timezone"], capture_output=True, text=True)
automatic_timezone = automatic_timezone_output.stdout.strip()
if automatic_timezone:
subprocess.run(
["timedatectl", "set-timezone", automatic_timezone])
messagebox.showinfo(
"Info", "Automatic date, time, and timezone setting complete.")
else:
messagebox.showwarning(
"Warning", "Automatic date, time, and timezone setting failed. Please try again or use timedatectl.")
except requests.RequestException:
messagebox.showwarning(
"Warning", "Failed to fetch date, time, and timezone information. Please try again or use timedatectl.")
else:
try:
date_input = f"{self.year_combo.get()}-{int(self.month_combo.get()):02d}-{int(self.day_combo.get()):02d}"
time_input = f"{int(self.hour_combo.get()):02d}:{int(self.minute_combo.get()):02d}:{int(self.second_combo.get()):02d}"
timezone_input = self.timezone_combo.get()
# check for invalid numbers in fields
if not all([
self.validate_number(self.year_combo.get(), 1970, 2038),
self.validate_number(self.month_combo.get(), 1, 12),
self.validate_number(self.day_combo.get(), 1, 31),
self.validate_number(self.hour_combo.get(), 0, 23),
self.validate_number(self.minute_combo.get(), 0, 59),
self.validate_number(self.second_combo.get(), 0, 59),
self.validate_timezone(timezone_input)
]):
messagebox.showerror(
"Error", "Invalid number entered in one or more fields.")
return
subprocess.run(["timedatectl", "set-ntp", "false"])
time.sleep(1)
subprocess.run(["timedatectl", "set-timezone", timezone_input])
time.sleep(1)
subprocess.run(["timedatectl", "set-time",
f"{date_input} {time_input}"])
messagebox.showinfo(
"Info", "Manual date, time, and timezone setting complete.")
except ValueError as e:
messagebox.showerror("Error", f"Error:\n{e}\n\nOne or more blank fields!")
# handle local rtc setting
if local_rtc:
subprocess.run(["timedatectl", "set-local-rtc", "1"])
else:
subprocess.run(["timedatectl", "set-local-rtc", "0"])
def on_timezone_apply_clicked(self):
timezone_input = self.timezone_combo.get()
if not timezone_input or not self.validate_timezone(timezone_input):
messagebox.showerror("Error", "Invalid or empty timezone field.")
return
try:
subprocess.run(["timedatectl", "set-timezone", timezone_input])
messagebox.showinfo(
"Info", "Timezone setting complete.")
except subprocess.CalledProcessError as e:
messagebox.showerror("Error", f"Error setting timezone:\n{e}")
def on_date_time_apply_clicked(self):
try:
date_input = f"{self.year_combo.get()}-{int(self.month_combo.get()):02d}-{int(self.day_combo.get()):02d}"
time_input = f"{int(self.hour_combo.get()):02d}:{int(self.minute_combo.get()):02d}:{int(self.second_combo.get()):02d}"
# check for invalid numbers in fields
if not all([
self.validate_number(self.year_combo.get(), 1970, 2038),
self.validate_number(self.month_combo.get(), 1, 12),
self.validate_number(self.day_combo.get(), 1, 31),
self.validate_number(self.hour_combo.get(), 0, 23),
self.validate_number(self.minute_combo.get(), 0, 59),
self.validate_number(self.second_combo.get(), 0, 59),
]):
messagebox.showerror(
"Error", "Invalid number entered in one or more fields.")
return
subprocess.run(["timedatectl", "set-ntp", "false"])
time.sleep(1)
subprocess.run(["timedatectl", "set-time", f"{date_input} {time_input}"])
messagebox.showinfo(
"Info", "Manual date and time setting complete.")
except ValueError as e:
messagebox.showerror("Error", f"Error:\n{e}\n\nOne or more blank fields!")
def on_local_rtc_apply_clicked(self):
local_rtc = self.local_rtc_check.instate(['selected'])
if local_rtc:
subprocess.run(["timedatectl", "set-local-rtc", "1"])
messagebox.showinfo("Info", "Local RTC setting enabled.")
else:
subprocess.run(["timedatectl", "set-local-rtc", "0"])
messagebox.showinfo("Info", "Local RTC setting disabled.")
def validate_number(self, value, min_val, max_val):
try:
num = int(value)
return min_val <= num <= max_val
except ValueError:
return False
def validate_timezone(self, timezone):
return timezone in pytz.all_timezones
def populate_fields(self):
now = datetime.datetime.now()
self.year_combo.set(now.year)
self.month_combo.set(now.month)
self.day_combo.set(now.day)
self.hour_combo.set(now.hour)
self.minute_combo.set(now.minute)
self.second_combo.set(now.second)
try:
current_timezone = subprocess.check_output(
["timedatectl", "show", "--property=Timezone"]).decode().strip().split("=")[1]
self.timezone_combo.set(current_timezone)
except subprocess.CalledProcessError:
pass
try:
ntp_status = subprocess.check_output(
["timedatectl", "show", "--property=NTP"]).decode().strip().split("=")[1]
if ntp_status == "yes":
self.automatic_time_check.state(['selected'])
self.on_automatic_time_toggled()
else:
self.automatic_time_check.state(['!selected'])
self.on_automatic_time_toggled()
except subprocess.CalledProcessError:
pass
try:
local_rtc_status = subprocess.check_output(
["timedatectl", "show", "--property=LocalRTC"]).decode().strip().split("=")[1]
if local_rtc_status == "yes":
self.local_rtc_check.state(['selected'])
else:
self.local_rtc_check.state(['!selected'])
except subprocess.CalledProcessError:
pass
self.year_combo.bind("<<ComboboxSelected>>",
self.update_apply_button_state)
self.month_combo.bind("<<ComboboxSelected>>",
self.update_apply_button_state)
self.day_combo.bind("<<ComboboxSelected>>",
self.update_apply_button_state)
self.hour_combo.bind("<<ComboboxSelected>>",
self.update_apply_button_state)
self.minute_combo.bind("<<ComboboxSelected>>",
self.update_apply_button_state)
self.second_combo.bind("<<ComboboxSelected>>",
self.update_apply_button_state)
self.timezone_combo.bind(
"<<ComboboxSelected>>", self.update_apply_button_state)
self.timezone_combo.bind(
"<KeyRelease>", self.update_apply_button_state)
if __name__ == "__main__":
app = DateTimeSetter()
app.mainloop()
|