#!/bin/sh # the next line restarts using wish \ exec wish "$0" "$@" # TkSetClock # Version 0.11, 2000-09-15 # # Copyright (C) 2000 by T.Sato # # http://member.nifty.ne.jp/tsato/tkdeskset/ # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # See the GNU General Public License for more details. set program_name "TkSetClock" set title "$program_name (Version 0.11)" # "date -a" may not supported at some systems other than SunOS. # set adj_slowly_available 0 if { $tcl_platform(os) == "SunOS" } { set adj_slowly_available 1 } # if may or may not available to set seconds with "date" command # set second_available 1 set date_cmd [ exec which date ] set f [ open "| strings $date_cmd" r ] while { 0 <= [ gets $f line ] } { if { [ string match "*YY]" $line ] || [ string match "*yy]" $line ] } { set second_available 0 break; } } close $f # Linux may have /sbin/hwclock command to set hardware clock (RTC). # set hwclock_cmd "" if [ file executable "/sbin/hwclock" ] { set hwclock_cmd "/sbin/hwclock" } # In default, both "Adjust Clock Slowly" and "Set Hardware Clock (RTC) Only" # options will be set OFF. # set adj_slowly 0 set use_hwclock 0 # check if Japanese available # set lang "C" if { ! [ catch { kanji code "abc" } ] } { catch { set lang $env(LANG) } catch { set lang $env(LC_ALL) } catch { set lang $env(LC_MESSAGES) } set env(LC_MESSAGES) "C" } # For Japanese locale, translate messages to Japanese. # proc gettext msg { global lang if [ regexp {^ja_|^japanese|^ja$} $lang junk ] { switch -glob $msg { "Year:" \ { return "年:" } "Month:" \ { return "月:" } "Day:" \ { return "日:" } "Hour:" \ { return "時:" } "Minute:" \ { return "分:" } "Second:" \ { return "秒:" } "Option:" \ { return "オプション:" } "Adjust Clock Slowly" \ { return "時計を徐々に調整する" } "Set Hardware Clock (RTC) Only" \ { return "ハードウェア時計 (RTC) のみを設定する" } "Set Clock" \ { return "時計の設定" } "Quit" \ { return "終了" } "The clock will be slowly adjusted by*" \ { return "時計の 進みを 通常よりも 数パーセント早く、\ あるいは 遅くして、 徐々に 時刻を 調整します。\ このため、 調整を 完了するまでには、\ 長い時間を 要することが あります。" } "NOTICE: You must become the super-user*" \ { return "警告: このプログラムを 実行する前に\ スーパー ユーザー (root) になる 必要が あります。\ スーパー ユーザーになる ためには、\ \"su\" コマンドを 使用する ことが できます。" } "Changing the system clock while running the system*" \ { return "システムの 動作中に システム時計を 変更すると、\ 何らかの 誤動作 (ハングアップを含む) が\ 引き起こされる 可能性が あります。\n" } "To avoid this problem,*it is suggested to adjust the clock slowly*" \ { return "この問題を 避けるため、\ 「時計を 徐々に 調整する」 オプションを ON に\ 設定しておく ことにより、\ 時計の 進みを 通常よりも 数パーセント早く、\ あるいは 遅くして、 徐々に 時刻を 調整する ことを 推奨します。\n" } "On some systems,*it is possible to set only the hardware clock (RTC)*" \ { return "システムによっては、\ 「ハードウェア時計 (RTC) のみを 設定する」 オプションを ON に\ 設定しておく ことにより、\ 次に システムを 起動した 際に システム時計を 修正できる ように\ ハードウェア時計 (RTC) のみを 設定する ことも 可能です。\n" } "Do you really want to set the system clock now?" \ { return "今、本当に システム時計の 設定を 行ないますか?" } } } return $msg } # Popup a window and show given error message. # proc show_error { msg { parent . } { bitmap warning } } { global program_name if [ winfo exists .error ] { destroy .error } toplevel .error wm title .error "$program_name - Notice" wm transient .error . frame .error.top -relief raised -bd 1 frame .error.bot -relief raised -bd 1 pack .error.top -side top -fill both -expand true pack .error.bot -side bottom -fill both label .error.mark -bitmap $bitmap label .error.message -text [ gettext $msg ] -justify left -wraplength 400 pack .error.mark .error.message -in .error.top \ -side left -padx 3m -pady 3m -fill both -expand true button .error.ok -text [ gettext "OK" ] -command { destroy .error } pack .error.ok -in .error.bot -side bottom -pady 1m # set_geometry .error $parent bell } ################################################################ # increment/decrement items proc adj_year { n } { global year if [ catch { if { $n < 0 } { incr year } elseif { 0 < $n } { incr year -1 } } ] { set year 2000 } if { 2037 < $year } { set year 2037 } if { $year < 1970 } { set year 1970 } } proc adj_month { n } { global month if [ catch { if { $n < 0 } { incr month } elseif { 0 < $n } { incr month -1 } } ] { set month 1 } if { 12 < $month } { set month 12 } if { $month < 1 } { set month 1 } } proc adj_day { n } { global day if [ catch { if { $n < 0 } { incr day } elseif { 0 < $n } { incr day -1 } } ] { set day 1 } if { 31 < $day } { set day 31 } if { $day < 1 } { set day 1 } } proc adj_hour { n } { global hour if [ catch { if { $n < 0 } { incr hour } elseif { 0 < $n } { incr hour -1 } } ] { set hour 0 } if { 23 < $hour } { set hour 23 } if { $hour < 0 } { set hour 0 } } proc adj_minute { n } { global minute if [ catch { if { $n < 0 } { incr minute } elseif { 0 < $n } { incr minute -1 } } ] { set minute 0 } if { 59 < $minute } { set minute 59 } if { $minute < 0 } { set minute 0 } } proc adj_second { n } { global second global second_available if { $second_available } { if [ catch { if { $n < 0 } { incr second } elseif { 0 < $n } { incr second -1 } } ] { set second 0 } if { 59 < $second } { set second 59 } if { $second < 0 } { set second 0 } } } ################################################################ # set clock proc set_clock {} { global program_name hwclock_cmd global year month day hour minute second global adj_slowly_available adj_slowly second_available global use_hwclock hwclock_cmd .buttons.set configure -state disabled update if { $adj_slowly } { set t1 [ clock seconds ] set t2 [ clock scan [ format "%02d/%02d/%04d %02d:%02d:%02d" \ $month $day $year $hour $minute $second ] ] set delta [ expr $t2 - $t1 ] if { $delta < -60 * 60 || 60 * 60 < $delta } { show_error "The clock will be slowly adjusted by\ speeding up or slowing down the clock few percents from the normal,\ and it can require long time to complete the adjustment." \ . info } set cmd "date -a $delta" } elseif { $use_hwclock } { set cmd "$hwclock_cmd --set --date \"[ format "%04d-%02d-%02d %02d:%02d:%02d" \ $year $month $day $hour $minute $second ]\"" } else { set msg1 [ gettext "Changing the system clock while running the system\ can cause some sort of malfunction (including hangup)\ on the operation of the system.\n" ] set msg2 "" if { $adj_slowly_available } { set msg2 [ gettext "To avoid this problem,\ it is suggested to adjust the clock slowly\ by speeding up or slowing down the clock few percents from the normal,\ by setting \"Adjust Clock Slowly\" option ON.\n" ] } set msg3 "" if { $hwclock_cmd != "" } { set msg3 [ gettext "On some systems,\ it is possible to set only the hardware clock (RTC)\ so that the system clock will be adjusted\ when the system is rebooted next time,\ by setting \"Set Hardware Clock (RTC) Only\" option ON.\n" ] } set msg4 [ gettext "Do you really want to set the system clock now?" ] if { [ tk_messageBox -title "$program_name - Confirm Apply" \ -type yesno -icon question \ -message "$msg1\n$msg2$msg3\n$msg4" ] != "yes" } { .buttons.set configure -state normal return } if { $second_available } { set cmd "date [ format "%02d%02d%02d%02d%04d.%02d" \ $month $day $hour $minute $year $second ]" } else { set cmd "date [ format "%02d%02d%02d%02d%04d" \ $month $day $hour $minute $year ]" } } eval exec $cmd if { ! $use_hwclock && $hwclock_cmd != "" } { # for Linux exec $hwclock_cmd --systohc } after 1000 .buttons.set configure -state normal after cancel { update_cur_time } after 200 { update_cur_time } } ################################################################ # display current date & time proc update_cur_time {} { global use_hwclock hwclock_cmd if { $use_hwclock } { .cur_time configure -text [ exec $hwclock_cmd --show ] after 1000 { update_cur_time } } else { .cur_time configure -text [ clock format [ clock seconds ] \ -format "%Y-%m-%d %H:%M:%S %Z" ] after 200 { update_cur_time } } update } ################################################################ # The main routine. wm title . $program_name wm iconname . $program_name . configure -borderwidth 10 option add *Entry.width 6 option add *Scrollbar.RepeatInterval 50 option add *Scrollbar.Width 10 option add *Dialog.msg.wrapLength 6i set row 0 label .year_l -text [ gettext "Year:" ] frame .year_f entry .year_v -textvariable year scrollbar .year_sb -command { adj_year } pack .year_v .year_sb -in .year_f -side left -fill both grid .year_l -row $row -sticky e grid .year_f -row $row -column 1 -sticky w incr row label .month_l -text [ gettext "Month:" ] frame .month_f entry .month_v -textvariable month scrollbar .month_sb -command { adj_month } pack .month_v .month_sb -in .month_f -side left -fill both grid .month_l -row $row -sticky e grid .month_f -row $row -column 1 -sticky w incr row label .day_l -text [ gettext "Day:" ] frame .day_f entry .day_v -textvariable day scrollbar .day_sb -command { adj_day } pack .day_v .day_sb -in .day_f -side left -fill both grid .day_l -row $row -sticky e grid .day_f -row $row -column 1 -sticky w incr row frame .sep1 -height 8 grid .sep1 -row $row incr row label .hour_l -text [ gettext "Hour:" ] frame .hour_f entry .hour_v -textvariable hour scrollbar .hour_sb -command { adj_hour } pack .hour_v .hour_sb -in .hour_f -side left -fill both grid .hour_l -row $row -sticky e grid .hour_f -row $row -column 1 -sticky w incr row label .minute_l -text [ gettext "Minute:" ] frame .minute_f entry .minute_v -textvariable minute scrollbar .minute_sb -command { adj_minute } pack .minute_v .minute_sb -in .minute_f -side left -fill both grid .minute_l -row $row -sticky e grid .minute_f -row $row -column 1 -sticky w incr row label .second_l -text [ gettext "Second:" ] frame .second_f entry .second_v -textvariable second scrollbar .second_sb -command { adj_second } pack .second_v .second_sb -in .second_f -side left -fill both grid .second_l -row $row -sticky e grid .second_f -row $row -column 1 -sticky w incr row frame .sep2 -height 12 grid .sep2 -row $row incr row label .opt_l -text [ gettext "Option:" ] grid .opt_l -row $row -sticky e checkbutton .adj_v -text [ gettext "Adjust Clock Slowly" ] \ -variable adj_slowly grid .adj_v -row $row -column 1 -sticky w if { $hwclock_cmd != "" } { incr row checkbutton .hwclock_v -text [ gettext "Set Hardware Clock (RTC) Only" ] \ -variable use_hwclock grid .hwclock_v -row $row -column 1 -sticky w } incr row frame .sep3 -height 12 grid .sep3 -row $row incr row frame .buttons button .buttons.set -text [ gettext "Set Clock" ] -command { set_clock } button .buttons.cancel -text [ gettext "Quit" ] -command { exit 0 } pack .buttons.set .buttons.cancel -side left -padx 6 -pady 6 grid .buttons -row $row -columnspan 2 incr row label .cur_time -border 2 -relief ridge grid .cur_time -row $row -columnspan 2 -sticky ew update catch { set f [ open "| id" r ] gets $f uid close $f if { ! [ string match "uid=0(*" $uid ] } { show_error "NOTICE: You must become the super-user (root)\ before start this program.\ You can use \"su\" command to become the super-user." } } set time [ clock seconds ] regsub {^0+} [ clock format $time -format "%Y" ] "" year regsub {^0+} [ clock format $time -format "%m" ] "" month regsub {^0+} [ clock format $time -format "%d" ] "" day regsub {^0+} [ clock format $time -format "%H" ] "" hour regsub {^0+} [ clock format $time -format "%M" ] "" minute if { $second_available } { regsub {^0+} [ clock format $time -format "%S" ] "" second } if { $hour == "" } { set hour 0 } if { $minute == "" } { set minute 0 } if { $second == "" } { set second 0 } if { ! $adj_slowly_available } { .adj_v configure -state disabled } if { ! $second_available } { .second_l configure -fg gray60 .second_v configure -fg gray60 bind .second_v { after 10 { set second 0 } } } update_cur_time