修改系统时间
时间:2006-08-03 来源:haojiawei
公司做了一个考试系统,总是出现考试提交不成功和答题超时的问题。服务器环境是RH Linux 9 run jboss,后来排除了其它一下问题确定是系统时间比正常的时间要快。问题找到了,接下来就是修改系统时间,下面是调整的过程。
[root@localhost root]#hwclock --help
hwclock - query and set the hardware clock (RTC)
Usage: hwclock [function] [options...]
Functions:
--help show this help
--show read hardware clock and print result
--set set the rtc to the time given with --date
--hctosys set the system time from the hardware clock
--systohc set the hardware clock to the current system time
--adjust adjust the rtc to account for systematic drift since
the clock was last set or adjusted
--getepoch print out the kernel's hardware clock epoch value
--setepoch set the kernel's hardware clock epoch value to the
value given with --epoch
--version print out the version of hwclock to stdout
Options:
--utc the hardware clock is kept in coordinated universal time
--localtime the hardware clock is kept in local time
--directisa access the ISA bus directly instead of /dev/rtc
--badyear ignore rtc's year because the bios is broken
--date specifies the time to which to set the hardware clock
--epoch=year specifies the year which is the beginning of the
hardware clock's epoch value
--noadjfile do not access /etc/adjtime. Requires the use of
either --utc or --localtime
[root@localhost root]# hwclock --set --date="8/3/06 14:35:05"
[root@localhost root]# hwclock --show
2006年08月03日 星期四 14时37分21秒 -0.085919 seconds
这样设置完后,系统就正常了。
Linux命令:hwclock是用来显示与设定硬件时钟的。
语 法:hwclock [--adjust][--debug][--directisa][--hctosys][--show][--systohc][--test]
[--utc][--version][--set --date=<日期与时间>]
补充说明:在Linux中有硬件时钟与系统时钟等两种时钟。硬件时钟是指主机板上的时钟设备,也就是通常可在BIOS画面设定的时钟。系统时钟则是指kernel中的时钟。当Linux启动时,系统时钟会去读取硬件时钟的设定,之後系统时钟即独立运作。所有Linux相关指令与函数都是读取系统时钟的设定。
参 数:
--adjust hwclock每次更改硬件时钟时,都会记录在/etc/adjtime文件中。使用--adjust参数,可使hwclock根据先前的记录来估算硬件时钟的偏差,并用来校正目前的硬件时钟。
--debug 显示hwclock执行时详细的信息。
--directisa hwclock预设从/dev/rtc设备来存取硬件时钟。若无法存取时,可用此参数直接以I/O指令来存取硬件时钟。
--hctosys 将系统时钟调整为与目前的硬件时钟一致。
--set --date=<日期与时间> 设定硬件时钟。
--show 显示硬件时钟的时间与日期。
--systohc 将硬件时钟调整为与目前的系统时钟一致。
--test 仅测试程序,而不会实际更改硬件时钟。
--utc 若要使用格林威治时间,请加入此参数,hwclock会执行转换的工作。
--version 显示版本信息。
英文版本,欢迎翻译:
NAME hwclock - query and set the hardware clock (RTC)
SYNOPSIS hwclock -r or hwclock --show hwclock -w or hwclock --systohc hwclock -s or hwclock --hctosys hwclock -a or hwclock --adjust hwclock -v or hwclock --version hwclock --set --date=newdate hwclock --getepoch hwclock --setepoch --epoch=year
other options:
[-u|--utc] --localtime --directisa --test --debug
and arcane options for DEC Alpha:
[-A|--arc] [-J|--jensen] [-S|--srm] [-F|--funky-toy]
Minimum unique abbreviations of all options are acceptable.
Also, equivalent options -r, -w, -s, -a, -v, -u, -D, -A, -J, -S, and -F are accepted for compatibility with the program "clock", while -h asks for a help message.
DESCRIPTION hwclock is a tool for accessing the Hardware Clock. You can display the current time, set the Hardware Clock to a specified time, set the Hardware Clock to the System Time, and set the System Time from the Hardware Clock.
You can also run hwclock periodically to insert or remove time from the Hardware Clock to compensate for systematic drift (where the clock consistently gains or loses time at a certain rate if left to run).
OPTIONS You need exactly one of the following options to tell hwclock what function to perform:
--show Read the Hardware Clock and print the time on Standard Output. The time is always in local time, even if you keep your Hardware Clock in Coordinated Universal Time. See the --utc option.
--set Set the Hardware Clock to the time given by the --date option.
--hctosys Set the System Time from the Hardware Clock.
Also set the kernels timezone value to the local timezone as indicated by the TZ environment variable and/or /usr/lib/zoneinfo, as tzset(3) would interpret them. The obsolete tz_dsttime field of the kernels timezone value is set to DST_NONE. (For details on what this field used to mean, see settimeofday(2).)
This is a good option to use in one of the system startup scripts.
--systohc Set the Hardware Clock to the current System Time.
--adjust Add or subtract time from the Hardware Clock to account for systematic drift since the last time the clock was set or adjusted. See discussion below.
--getepoch Print out standard output the kernels Hardware Clock epoch value. This is the number of years into AD to which a zero year value in the Hardware Clock refers. For example, if you are using the convention that the year counter in your Hardware Clock contains the number of full years since 1952, then the kernels Hardware Counter epoch value must be 1952.
This epoch value is used whenever hwclock reads or sets the Hardware Clock.
--setepoch Set the kernels Hardware Clock epoch value to the value specified by the --epoch option. See the --getepoch option for details.
--version Print the version of hwclock on Standard Output. You need the following option if you specify --set option. Otherwise, it is ignored.
--date=date_string Specifies the time to which to set the Hardware Clock. The value of this option is an argument to the date(1) program. For example,
hwclock --set --date="9/22/96 16:45:05"
The argument is in local time, even if you keep your Hardware Clock in Coordinated Universal time. See the --utc option.
--epoch=year Specifies the year which is the beginning of the Hardware Clocks epoch. I.e. the number of years into AD to which a zero value in the Hardware Clocks year counter refers.
For example,
hwclock --setepoch --epoch=1952
The following options apply to most functions.
--utc
--localtime Indicates that the Hardware Clock is kept in Coordinated Universal Time or local time, respec? tively. It is your choice whether to keep your clock in UTC or local time, but nothing in the clock tells which youve chosen. So this option is how you give that information to hwclock.
If you specify the wrong one of these options (or specify neither and take a wrong default), both setting and querying of the Hardware Clock will be messed up.
If you specify neither --utc nor --localtime , the default is whichever was specified the last time hwclock was used to set the clock (i.e. hwclock was successfully run with the --set , --sys? tohc , or --adjust options), as recorded in the adjtime file. If the adjtime file doesnt exist, the default is local time.
--directisa is meaningful only on an ISA machine or an Alpha (which implements enough of ISA to be, roughly speaking, an ISA machine for hwclocks purposes). For other machines, it has no effect. This option tells hwclock to use explicit I/O instructions to access the Hardware Clock. Without this option, hwclock will try to use the /dev/rtc device (which it assumes to be driven by the rtc device driver). If it is unable to open the device (for read), it will use the explicit I/O instructions anyway.
The rtc device driver was new in Linux Release 2.
--badyear Indicates that the Hardware Clock is incapable of storing years outside the range 1994-1999. There is a problem in some BIOSes (almost all Award BIOSes made between 4/26/94 and 5/31/95) wherein they are unable to deal with years after 1999. If one attempts to set the year-of-cen? tury value to something less than 94 (or 95 in some cases), the value that actually gets set is 94 (or 95). Thus, if you have one of these machines, hwclock cannot set the year after 1999 and cannot use the value of the clock as the true time in the normal way.
To compensate for this (without your getting a BIOS update, which would definitely be prefer? able), always use --badyear if you have one of these machines. When hwclock knows its working with a brain-damaged clock, it ignores the year part of the Hardware Clock value and instead tries to guess the year based on the last calibrated date in the adjtime file, by assuming that that date is within the past year. For this to work, you had better do a hwclock --set or hwclock --systohc at least once a year!
Though hwclock ignores the year value when it reads the Hardware Clock, it sets the year value when it sets the clock. It sets it to 1995, 1996, 1997, or 1998, whichever one has the same position in the leap year cycle as the true year. That way, the Hardware Clock inserts leap days where they belong. Again, if you let the Hardware Clock run for more than a year without setting it, this scheme could be defeated and you could end up losing a day.
hwclock warns you that you probably need --badyear whenever it finds your Hardware Clock set to 1994 or 1995.
--srm
--arc
--jensen
--funky-toy These options all tell hwclock what kind of Alpha machine you have. They are invalid if you dont have an Alpha and shouldnt be necessary if you do, because hwclock should be able to determine by itself what its running on, at least when /proc is mounted. These options make it possible for hwclock to work even when its environment does not conform to its expectations and thus it cannot accurately determine what sort of system it is running on. If you think hwclock is incorrectly determining the systems characteristics, try running with the --debug option to see what conclusions the program is reaching and how. If you find you need one of these options to make hwclock work, contact the hwclock maintainer to see if the program can be improved to detect your system automatically.
--jensen means you are running on a Jensen model.
--arc means your machine uses epoch 1980 in its hardware clock, as is commonly the case for machines on ARC console (but Ruffians have epoch 1900).
--srm means your machine uses epoch 1900 in its hardware clock, as is commonly the case for machines on SRM console.
--funky-toy means that on your machine, one has to use the UF bit instead of the UIP bit in the Hardware Clock to detect a time transition. "Toy" in the option name refers to the Time Of Year facility of the machine.
--test Do everything except actually updating the Hardware Clock or anything else. This is useful, especially in conjunction with --debug, in learning about hwclock.
--debug Display a lot of information about what hwclock is doing internally. Some of its function is complex and this output can help you understand how the program works.
NOTES Clocks in a Linux System There are two main clocks in a Linux system:
The Hardware Clock: This is a clock that runs independently of any control program running in the CPU and even when the machine is powered off.
On an ISA system, this clock is specified as part of the ISA standard. The control program can read or set this clock to a whole second, but the control program can also detect the edges of the 1 second clock ticks, so the clock actually has virtually infinite precision.
This clock is commonly called the hardware clock, the real time clock, the RTC, the BIOS clock, and the CMOS clock. Hardware Clock, in its capitalized form, was coined for use by hwclock because all of the other names are inappropriate to the point of being misleading.
The System Time: This is the time kept by a clock inside the Linux kernel and driven by a timer inter? rupt. (On an ISA machine, the timer interrupt is part of the ISA standard). It has meaning only while Linux is running on the machine. The System Time is the number of seconds since 00:00:00 January 1, 1970 UTC (or more succinctly, the number of seconds since 1969). The System Time is not an integer, though. It has virtually infinite precision.
The System Time is the time that matters. The Hardware Clocks basic purpose in a Linux system is to keep time when Linux is not running. You initialize the System Time to the time from the Hardware Clock when Linux starts up, and then never use the Hardware Clock again. Note that in DOS, for which ISA was designed, the Hardware Clock is the only real time clock.
It is important that the System Time not have any discontinuities such as would happen if you used the date(1L) program to set it while the system is running. You can, however, do whatever you want to the Hardware Clock while the system is running, and the next time Linux starts up, it will do so with the adjusted time from the Hardware Clock. You can also use the program adjtimex(8) to smoothly adjust the System Time while the system runs.
A Linux kernel maintains a concept of a local timezone for the system. But dont be misled -- almost nobody cares what timezone the kernel thinks it is in. Instead, programs that care about the timezone (perhaps because they want to display a local time for you) almost always use a more traditional method of determining the timezone: They use the TZ environment variable and/or the /usr/local/timezone direc? tory, as explained in the man page for tzset(3). However, some programs and fringe parts of the Linux kernel such as filesystems use the kernel timezone value. An example is the vfat filesystem. If the kernel timezone value is wrong, the vfat filesystem will report and set the wrong timestamps on files.
hwclock sets the kernel timezone to the value indicated by TZ and/or /usr/local/timezone when you set the System Time using the --hctosys option.
The timezone value actually consists of two parts: 1) a field tz_minuteswest indicating how many minutes local time (not adjusted for DST) lags behind UTC, and 2) a field tz_dsttime indicating the type of Day? light Savings Time (DST) convention that is in effect in the locality at the present time. This second field is not used under Linux and is always zero. (See also settimeofday(2).)