[Comm] Программа для настройки часов

Вячеслав Вячеслав
Пн Окт 31 15:37:42 MSK 2005


В Вск, 30/10/2005 в 23:03 +0300, Владимир Гусев пишет:
> >> [vova на vova vova]$ timetool
> >> Error in startup script: can't read "cpld": no such variable
> > повесил
> > https://bugzilla.altlinux.org/show_bug.cgi?id=8377
> > программой похоже годами не пользуются...
> 
> Да уж.. Хотя вещь неплохая.. судя по отзывам..
> 
> 
Я поправил (приложено). Там все элементарно - путь к картинке не нашелся.
Интерфейс неплохо бы переписать и на хелпер посадить, чтобы паролями не
донимало.  
----------- следующая часть -----------
#!/usr/bin/wish -f

# This requires GNU date
# This code is no longer process intensive!
# I added a date command to rhswish which returns
# the result of date(NULL) (see date(2)).  date
# is now used to keep time.

set ttld /usr/lib/timetool

if {[catch {source $ttld/dialog.tcl}] != 0} {
    puts "Couldn't load dialog.tcl"
    puts "(normally this is /usr/lib/timetool)"
    exit 0
}

set selected_color Red
set unselected_color Black

set month_length(1) 31
set month_length(2) 28
set month_length(3) 31
set month_length(4) 30
set month_length(5) 31
set month_length(6) 30
set month_length(7) 31
set month_length(8) 31
set month_length(9) 30
set month_length(10) 31
set month_length(11) 30
set month_length(12) 31

set month_name(1) Jan
set month_name(2) Feb
set month_name(3) Mar
set month_name(4) Apr
set month_name(5) May
set month_name(6) Jun
set month_name(7) Jul
set month_name(8) Aug
set month_name(9) Sep
set month_name(10) Oct
set month_name(11) Nov
set month_name(12) Dec


# Update the clock every second
proc update_clock {} {
    global cl_second old_second

    set new_second [exec date +%s]
    after 1000 update_clock

    set inc [expr $new_second - $old_second]
    set old_second $new_second
    # The takes care of old_second == 59 and new_second == 0
    if {$inc < 0} {
	incr inc 60
    }
    adj_clock $inc
}

# Add n seconds to the clock
proc adj_clock {n} {
    global cl_second

    incr cl_second $n

    if {$n > 0} {
	if {$cl_second > 59} {
	    incr cl_second -60
	    incr_minute
	}
    } else {
	if {$cl_second < 0} {
	    incr cl_second 60
	    decr_minute
	}
    }
    redraw_clock
}

proc incr_minute {} {
    global cl_minute

    incr cl_minute

    if {$cl_minute > 59} {
	set cl_minute 0
	incr_hour
    }
}

proc decr_minute {} {
    global cl_minute

    incr cl_minute -1

    if {$cl_minute < 0} {
	set cl_minute 59
	decr_hour
    }
}

proc incr_date {} {
    global cl_date cl_month cl_year cur_month_name month_name

    incr cl_date
    if {$cl_date > [days_in_month]} {
	set cl_date 1
	incr cl_month
	if {$cl_month > 12} {
	    set cl_month 1
	    incr cl_year
	}
	set cur_month_name $month_name($cl_month)
    }
}

proc decr_date {} {
    global cl_date cl_month cl_year cur_month_name month_name

    incr cl_date -1
    if {$cl_date == 0} {
	incr cl_month -1
	if {$cl_month == 0} {
	    set cl_month 12
	    incr cl_year -1
	}
	set cl_date [days_in_month]
	set cur_month_name $month_name($cl_month)
    }
}

proc incr_hour {} {
    global cl_hour

    incr cl_hour
    if {$cl_hour > 23} {
	set cl_hour 0
	incr_date
    }
}

proc decr_hour {} {
    global cl_hour

    incr cl_hour -1
    if {$cl_hour < 0} {
	set cl_hour 23
	decr_date
    }
}

proc sb_adj_hour {n} {
    if {$n > 0} {
	decr_hour
    } else {
	incr_hour
    }
    redraw_clock
}

proc sb_adj_minute {n} {
    if {$n > 0} {
	decr_minute
    } else {
	incr_minute
    }
    redraw_clock
}

proc sb_adj_second {n} {
    adj_clock [expr 0 - $n]
    # Ne need to redraw since adj_clock does it
}

proc sb_adj_date {n} {
    if {$n > 0} {
	decr_date
    } else {
	incr_date
    }
    redraw_clock
}

proc sb_adj_year {n} {
    global cl_year cl_month cl_date

    # Only thing to watch out for is if cl_date is 29 and cl_month is 2
    # If so, just change it to 28 to be safe
    if {$cl_month == 2 && $cl_date == 29} {
	set cl_date 28
    }
    if {$n > 0} {
	incr cl_year -1
    } else {
	incr cl_year
    }
    redraw_clock
}

proc sb_adj_month {n} {
    global cl_month cl_date cl_year cur_month_name month_name

    # Just watch cl_date to see if it goes over days_in_month
    if {$n > 0} {
	incr cl_month -1
	if {$cl_month == 0} {
	    set cl_month 12
	    incr cl_year -1
	}
    } else {
	incr cl_month
	if {$cl_month == 13} {
	    set cl_month 1
	    incr cl_year
	}
    }
    # Now check the days in month
    set d [days_in_month]
    if {$cl_date > $d} {
	set cl_date $d
    }
    set cur_month_name $month_name($cl_month)
    redraw_clock
}

proc sb_dispatch {n} {
    global selected_item

    switch -exact $selected_item {
	.hour {sb_adj_hour $n}
	.minute {sb_adj_minute $n}
	.second {sb_adj_second $n}
	.month {sb_adj_month $n}
	.date {sb_adj_date $n}
	.year {sb_adj_year $n}
    }
}

proc days_in_month {} {
    global cl_month cl_year month_length

    if {$cl_month == 2} {
        if {[expr $cl_year / 4.0] != [expr $cl_year / 4]} {
            return 28
        }
        if {[expr $cl_year / 100.0] != [expr $cl_year / 100]} {
            return 29
        }
        if {[expr $cl_year / 400.0] != [expr $cl_year / 400]} {
            return 28
        }
        return 29
    } else {
        return $month_length($cl_month)
    }
}

proc redraw_clock {} {
    global cl_year cl_month cl_date cl_hour cl_minute cl_second time_mode cur_month_name

    # AM/PM
    if {$cl_hour > 11} {
	.ampm configure -text " PM"
    } else {
	.ampm configure -text " AM"
    }

    # Hours
    if {$time_mode == "military"} {
	.hour configure -text [format "%02d" $cl_hour]
    } else {
	if {$cl_hour > 12} {
	    .hour configure -text [format "%2d" [expr $cl_hour - 12]]
	} elseif {$cl_hour == 0} {
	    .hour configure -text "12"
	} else {
	    .hour configure -text [format "%2d" $cl_hour]
	}
    }

    # Minutes and Seconds
    .minute configure -text [format "%02d" $cl_minute]
    .second configure -text [format "%02d" $cl_second]

    # Date
    .month configure -text $cur_month_name
    .date configure -text $cl_date
    .year configure -text $cl_year

    # Red Hat Software rules
    spin_hat
}

proc reset_clock {} {
    global cl_month cl_date cl_hour cl_minute cl_second cl_year month_name cur_month_name

    set time [exec /bin/date "+%-m %-d %-H %-M %-S %-Y"]
    set cl_month [lindex $time 0]
    set cl_date [lindex $time 1]
    set cl_hour [lindex $time 2]
    set cl_minute [lindex $time 3]
    set cl_second [lindex $time 4]
    set cl_year [lindex $time 5]
    set cur_month_name $month_name($cl_month)
}

proc set_system_time {} {
    global cl_year cl_month cl_date cl_hour cl_minute cl_second utc_time

    # Warning
    set res [rhs_continue_dialog "WARNING!\n\nChanging your system clock can wreak havoc\nwith processes that depend on the time.\n\nIf you continue and reset the clock,\nyou should probably reboot your system."]
    if {$res == 1} {
	return
    }

    # Set the system time
    exec /bin/date [format "%02d%02d%02d%02d%04d.%02d" $cl_month $cl_date $cl_hour $cl_minute $cl_year $cl_second]

    # Write system time to CMOS clock
    if {$utc_time == 1} {
	exec /sbin/hwclock --utc --systohc
    } else {
	exec /sbin/hwclock --localtime --systohc
    }

    # This keeps things sane
    update_clock
    reset_clock
}

proc spin_hat {} {
    global hat_pos ttld

    incr hat_pos
    if {$hat_pos > 12} {
	set hat_pos 1
    }
    .hat configure -bitmap @$ttld/loopy/loopy_$hat_pos.xbm
}

frame .time -borderwidth 2 -relief groove
frame .clock
#set font -adobe-times-bold-r-normal--20-100-*-*-p-150-*-*
set font -*-fixed-medium-r-normal--24-170-100-100-c-120-*
label .hour -font $font\
    -borderwidth 0 -padx 0 -foreground $unselected_color
label .hourcolon -text ":" -font $font \
    -borderwidth 0 -padx 0
label .minute -font $font \
    -borderwidth 0 -padx 0 -foreground $unselected_color
label .minutecolon -text ":" -font $font \
    -borderwidth 0 -padx 0
label .second -font $font \
    -borderwidth 0 -padx 0 -foreground $unselected_color
label .ampm -font $font \
    -borderwidth 0 -padx 0
pack .hour .hourcolon .minute .minutecolon .second .ampm -side left -in .clock

frame .calendar
label .month -font $font \
    -borderwidth 0 -padx 0 -foreground $unselected_color
label .monthspace -text " " -font $font \
    -borderwidth 0 -padx 0
label .date -font $font \
    -borderwidth 0 -padx 0 -foreground $unselected_color
label .datecomma -text ", " -font $font \
    -borderwidth 0 -padx 0
label .year -font $font \
    -borderwidth 0 -padx 0 -foreground $unselected_color
pack .month .monthspace .date .datecomma .year -side left -in .calendar

pack .clock .calendar -side top -in .time -padx 4 -pady 1

frame .control -borderwidth 2 -relief groove
checkbutton .militarytime -text "24 Hour Time" -variable time_mode -onvalue military -offvalue standard
message .msg -text "Click on the part of the time and date you wish to change and use the arrows to adjust it."
scrollbar .sb -command sb_dispatch -repeatinterval 50 -width 30
button .reset -text "Reset Time" -command "reset_clock ; redraw_clock"

pack .reset .militarytime -side bottom -in .control -pady 2 -ipadx 4 -ipady 1
pack .msg .sb -side left -in .control -padx 2 -pady 2

frame .setf -borderwidth 2 -relief sunken
button .set -text "Set System Clock" -command "set_system_time"
pack .set -in .setf -padx 4 -pady 2 -ipadx 4 -ipady 1

button .quit -text "Exit Time Machine" -command "exit 0"

#label .hat -padx 10 -pady 10 -bitmap [image create photo -file $env(CONTROL_PANEL_LIB_DIR)/loopy/loopy_1.gif] \
#  -foreground Red

label .hat -padx 10 -pady 10 -bitmap @/usr/lib/timetool/loopy/loopy_1.xbm -foreground Red

pack .time .control .setf -side top -padx 4 -pady 2
pack .quit -side top -padx 4 -pady 3 -ipadx 4 -ipady 1

wm title . "Time Machine"

proc time_mode_trace {we dont care} {
    global time_mode

    if {$time_mode == "military"} {
	pack unpack .ampm
    } else {
	pack .ampm -side left -after .second
    }

    redraw_clock
}

proc select_item {i} {
    global selected_item unselected_color selected_color

    if {$selected_item != ""} {
	$selected_item configure -foreground $unselected_color
    }
    set selected_item $i
    $i configure -foreground $selected_color
}

proc toggle_hat {} {
    global hat_up

    if {$hat_up == 1} {
	pack unpack .hat
	set hat_up 0
    } else {
	pack .hat -side top -after .quit
	set hat_up 1
    }
}

# Set up bindings on items of interest
foreach i ".hour .minute .second .month .date .year" {
    bind $i <1> "select_item $i"
}

bind .ampm <1> "toggle_hat"

if {[exec id -u] != 0} {
    rhs_error_dialog "You must be root to run the Time Tool."
    exit 1
}

# Find out if we are using UTC or local time
set utc_time 0
if {[file isfile "/etc/sysconfig/clock"]} {
    set fd [open "/etc/sysconfig/clock" r]
    
    while {[gets $fd x] != "-1"} {
      if {[regexp ".*GMT.*" $x]} {
	set utc_time 1
       } elseif {[regexp ".*UTC.*" $x]} {
        if {[regexp ".*UTC.*(yes|true)" $x]} {
	  set utc_time 1
        }
       }
    }
    close $fd
}

reset_clock
# This keeps track of time on the system clock
set old_second [exec date +%s]

set time_mode standard
trace variable time_mode w time_mode_trace

set selected_item ""
set hat_pos 1
set hat_up 0

redraw_clock
after 1000 update_clock


Подробная информация о списке рассылки community