日期不會從控制台更改

日期不會從控制台更改

我需要暫時更改日期來調試一個腳本,但我遇到了意外問題:

root@xxx:/# LC_ALL=C date
Wed Jan 31 17:09:02 EET 2018
root@xxx:/# LC_ALL=C date -s "2018-01-10 17:09:30"
Wed Jan 10 17:09:30 EET 2018
root@xxx:/# LC_ALL=C date
Wed Jan 31 17:09:28 EET 2018

筆記:我有一個ru_RU區域設置,所以我LC_ALL=C為了您的方便而使用。使用命令而不LC_ALL做任何改變。

我猜我的文法有問題或閱讀man不正確,所以我使用了來自線程並沒有解決任何問題。嘗試將 hwclock 設定為符合日期沒有結果:

root@xxx:/# LC_ALL=C hwclock --systohc
root@xxx:/# LC_ALL=C hwclock --show
Wed Jan 31 17:18:17 2018  .451473 seconds
root@xxx:/# man hwclock
root@xxx:/# hwclock --set --date='2011-08-14 16:45:05'
root@xxx:/# LC_ALL=C hwclock --show
Sun Aug 14 16:45:09 2011  .389008 seconds
root@xxx:/# LC_ALL=C date
Wed Jan 31 17:21:07 EET 2018

我怎樣才能更改日期?

系統:

root@xxx:/# cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.3 LTS"

@Yaron 的更新

kovjr@xxx:~$ LC_ALL=C sudo date 120622432007.55
[sudo] password for kovjr: 
Thu Dec  6 22:43:55 EET 2007
kovjr@xxx:~$ LC_ALL=C date
Wed Jan 31 18:19:23 EET 2018

答案1

如上所述這裡:

觀看時間

要查看當前日期和時間,以下命令就足夠了

date

設定時間

更改時間意味著設定新時間。要在 Ubuntu(或任何 Linux)中設定時間,只需執行以下命令

sudo date newdatetimestring

其中必須遵循 下面描述的newdatetimestring格式nnddhhmmyyyy.ss

nn is a two digit month, between 01 to 12
dd is a two digit day, between 01 and 31, with the regular rules for days according to month and year applying
hh is two digit hour, using the 24-hour period so it is between 00 and 23
mm is two digit minute, between 00 and 59
yyyy is the year; it can be two digit or four digit: your choice. I prefer to use four digit years whenever I can for better clarity and less confusion
ss is two digit seconds. Notice the period ‘.’ before the ss.

假設您要將計算機的新時間設定為 2007 年 12 月 6 日 22:43:55,那麼您可以使用:

sudo date 120622432007.55

例子:

$ sudo date 120622432007.55
[sudo] password for xyz: 
Thu Dec  6 22:43:55 IST 2007
$ date
Thu Dec  6 22:43:56 IST 2007
$

相關內容