Configurando a Data e Hora
Os sistemas operacionais modernos distinguem entre os dois tipos de relógios a seguir:
-
A real-time clock (RTC), comumente referido como hardware clock, (normalmente um circuito integrado na placa do sistema) que é completamente independente do estado atual do sistema operacional e funciona mesmo quando o computador é desligado .
-
A system clock, também conhecido como software clock, que é mantido pelo kernel e seu valor inicial é baseado no relógio em tempo real. Depois que o sistema é inicializado e o relógio do sistema é inicializado, o relógio do sistema é completamente independente do relógio em tempo real.
A hora do sistema é sempre mantida em Coordinated Universal Time (UTC) e convertida nos aplicativos para a hora local conforme necessário. Local time é a hora real em seu fuso horário atual, levando em consideração o daylight saving time (DST). O relógio em tempo real pode usar o UTC ou a hora local. UTC é recomendado.
Fedora 31 oferece três ferramentas de linha de comando que podem ser usadas para configurar e exibir informações sobre a data e hora do sistema: o utilitário timedatectl, que é novo em Fedora 31 e faz parte do `systemd`pass:attribute []; o tradicional comando date; e o utilitário hwclock para acessar o relógio do hardware.
Usando o Comando timedatectl
O utilitário timedatectl é distribuído como parte do sistema
systemd
e gerenciador de serviços e permite que você reveja e altere a
configuração do relógio do sistema. Você pode usar esta ferramenta para
alterar a data e a hora atuais, definir o fuso horário ou ativar a
sincronização automática do relógio do sistema com um servidor remoto.
Para obter informações sobre como exibir a data e hora atuais em um formato personalizado, consulte também Configuring_the_Date_and_Time.adoc#sect-Configuring_the_Date_and_Time-date.
Exibindo a Data e Hora Atuais
Para exibir a data e hora atuais junto com informações detalhadas sobre a configuração do relógio do sistema e do hardware, execute o comando timedatectl sem opções de linha de comando adicionais:
timedatectl
Isso exibe a hora local e universal, o fuso horário atualmente usado, o
status da configuração do Protocolo de Tempo de Rede (NTP
) e informações
adicionais relacionadas ao DST.
A seguir está um exemplo de saída do comando timedatectl em um
sistema que não usa NTP
para sincronizar o relógio do sistema com um
servidor remoto:
~]$ timedatectl Local time: Mon 2013-09-16 19:30:24 CEST Universal time: Mon 2013-09-16 17:30:24 UTC Timezone: Europe/Prague (CEST, +0200) NTP enabled: no NTP synchronized: no RTC in local TZ: no DST active: yes Last DST change: DST began at Sun 2013-03-31 01:59:59 CET Sun 2013-03-31 03:00:00 CEST Next DST change: DST ends (the clock jumps one hour backwards) at Sun 2013-10-27 02:59:59 CEST Sun 2013-10-27 02:00:00 CET
Mudando a Hora Atual
Para alterar a hora atual, digite o seguinte em um prompt de shell como
root
:
timedatectl set-time
HH:MM:SS
Substitua HH por uma hora, MM por um minuto e SS por um segundo, todos digitados na forma de dois dígitos.
Este comando atualiza a hora do sistema e o relógio do hardware. O resultado é semelhante ao uso dos comandos date --set e hwclock --systohc.
O comando falhará se um serviço NTP
estiver habilitado. Veja
Configuring_the_Date_and_Time.adoc#sect-Configuring_the_Date_and_Time-timedatectl-NTP para desativar temporariamente
o serviço.
Para alterar o horário atual para 23h26, execute o seguinte comando como
root
:
~]# timedatectl set-time 23:26:00
Por padrão, o sistema está configurado para usar UTC. Para configurar seu
sistema para manter o relógio na hora local, execute o comando
timedatectl com a opção set-local-rtc
como`root`:
timedatectl set-local-rtc
boolean
Para configurar seu sistema para manter o relógio na hora local, substitua
boolean with yes
(ou, alternativamente,` y`, true
,` t` ou 1
). Para
configurar o sistema para usar UTC, substitua boolean with no
(ou,
alternativamente,` n`, false
,` f` ou 0
). A opção padrão é no
.
Mudando a Data Atual
Para alterar a data atual, digite o seguinte em um prompt de shell como
root
:
timedatectl set-time
YYYY-MM-DD
Substitua YYYY por um ano de quatro dígitos, MM por um mês de dois dígitos e DD por um dia do mês de dois dígitos.
Observe que alterar a data sem especificar a hora atual resulta na configuração da hora para 00:00:00.
Para alterar a data atual para 2 de junho de 2013 e manter a hora atual
(23h26), execute o seguinte comando como root
:
~]# timedatectl set-time "2013-06-02 23:26:00"
Mudança do Fuso Horário
Para listar todos os fusos horários disponíveis, digite o seguinte em um prompt de shell:
timedatectl list-timezones
Para alterar o fuso horário usado atualmente, digite como root
:
timedatectl set-timezone time_zone
Replace time_zone with any of the values listed by the timedatectl list-timezones command.
To identify which time zone is closest to your present location, use the
timedatectl command with the list-timezones
command
line option. For example, to list all available time zones in Europe, type:
~]# timedatectl list-timezones | grep Europe Europe/Amsterdam Europe/Andorra Europe/Athens Europe/Belgrade Europe/Berlin Europe/Bratislava …
To change the time zone to Europe/Prague
, type as root
:
~]# timedatectl set-timezone Europe/Prague
Synchronizing the System Clock with a Remote Server
As opposed to the manual adjustments described in the previous sections, the
timedatectl command also allows you to enable automatic
synchronization of your system clock with a group of remote servers using
the NTP
protocol. Enabling NTP enables the chronyd
or ntpd
service,
depending on which of them is installed.
The NTP
service can be enabled and disabled using a command as follows:
timedatectl set-ntp
boolean
To enable your system to synchronize the system clock with a remote NTP
server, replace boolean with yes
(the default option). To disable this
feature, replace boolean with no
.
To enable automatic synchronization of the system clock with a remote server, type:
~]# timedatectl set-ntp yes
The command will fail if an NTP
service is not installed. See
Installing
chrony for more information.
Using the date Command
The date utility is available on all Linux systems and allows you to display and configure the current date and time. It is frequently used in scripts to display detailed information about the system clock in a custom format.
For information on how to change the time zone or enable automatic synchronization of the system clock with a remote server, see Configuring_the_Date_and_Time.adoc#sect-Configuring_the_Date_and_Time-timedatectl.
Exibindo a Data e Hora Atuais
To display the current date and time, run the date command with no additional command line options:
date
This displays the day of the week followed by the current date, local time, abbreviated time zone, and year.
By default, the date command displays the local time. To display
the time in UTC, run the command with the --utc
or -u
command line option:
date --utc
You can also customize the format of the displayed information by providing
the +"format"
option on the command line:
date +"format"
Replace format with one or more supported control sequences as illustrated
in
Configuring_the_Date_and_Time.adoc#exam-Configuring_the_Date_and_Time-date-Display. See
Configuring_the_Date_and_Time.adoc#tabl-Configuring_the_Date_and_Time-date-Format for a list of the most frequently used formatting
options, or the date
(1) manual page for a complete list of these options.
Control Sequence | Description |
---|---|
|
The hour in the HH format (for example, |
|
The minute in the MM format (for example, |
|
The second in the SS format (for example, |
|
The day of the month in the DD format (for example, |
|
The month in the MM format (for example, |
|
The year in the YYYY format (for example, |
|
The time zone abbreviation (for example, |
|
The full date in the YYYY-MM-DD format (for example, |
|
The full time in the HH:MM:SS format (for example, 17:30:24). This option is equal to |
To display the current date and local time, type the following at a shell prompt:
~]$ date
Mon Sep 16 17:30:24 CEST 2013
To display the current date and time in UTC, type the following at a shell prompt:
~]$ date --utc
Mon Sep 16 15:30:34 UTC 2013
To customize the output of the date command, type:
~]$ date +"%Y-%m-%d %H:%M" 2013-09-16 17:30
Mudando a Hora Atual
To change the current time, run the date command with the
--set
or -s
option as root
:
date --set
HH:MM:SS
Substitua HH por uma hora, MM por um minuto e SS por um segundo, todos digitados na forma de dois dígitos.
By default, the date command sets the system clock to the local
time. To set the system clock in UTC, run the command with the
--utc
or -u
command line option:
date--set
HH:MM:SS--utc
Para alterar o horário atual para 23h26, execute o seguinte comando como
root
:
~]# date --set 23:26:00
Mudando a Data Atual
To change the current date, run the date command with the
--set
or -s
option as root
:
date --set
YYYY-MM-DD
Substitua YYYY por um ano de quatro dígitos, MM por um mês de dois dígitos e DD por um dia do mês de dois dígitos.
Observe que alterar a data sem especificar a hora atual resulta na configuração da hora para 00:00:00.
Para alterar a data atual para 2 de junho de 2013 e manter a hora atual
(23h26), execute o seguinte comando como root
:
~]# date --set 2013-06-02 23:26:00
Using the hwclock Command
hwclock
is a utility for accessing the hardware clock, also referred to as
the Real Time Clock (RTC). The hardware clock is independent of the
operating system you use and works even when the machine is shut down. This
utility is used for displaying the time from the hardware clock. hwclock
also contains facilities for compensating for systematic drift in the
hardware clock.
The hardware clock stores the values of: year, month, day, hour, minute, and second. It is not able to store the time standard, local time or Coordinated Universal Time (UTC), nor set the Daylight Saving Time (DST).
The hwclock
utility saves its settings in the /etc/adjtime
file, which
is created with the first change you make, for example, when you set the
time manually or synchronize the hardware clock with the system time.
In Fedora 6, the hwclock command was run automatically on every system shutdown or reboot, but it is not in Fedora 31. When the system clock is synchronized by the Network Time Protocol (NTP) or Precision Time Protocol (PTP), the kernel automatically synchronizes the hardware clock to the system clock every 11 minutes. |
For details about NTP, see Configuring NTP Using the chrony Suite and Configuring NTP Using ntpd. For information about PTP, see Configuring PTP Using ptp4l. For information about setting the hardware clock after executing ntpdate, see Configuring the Hardware Clock Update.
Exibindo a Data e Hora Atuais
Running hwclock with no command line options as the root
user
returns the date and time in local time to standard output.
hwclock
Note that using the --utc
or --localtime
options with
the hwclock command does not mean you are displaying the hardware
clock time in UTC or local time. These options are used for setting the
hardware clock to keep time in either of them. The time is always displayed
in local time. Additionally, using the hwclock --utc or
hwclock --local commands does not change the record in the
/etc/adjtime
file. This command can be useful when you know that the
setting saved in /etc/adjtime
is incorrect but you do not want to change
the setting. On the other hand, you may receive misleading information if
you use the command an incorrect way. See the hwclock
(8) manual page for
more details.
To display the current date and the current local time from the hardware
clock, run as root
:
~]# hwclock Tue 15 Apr 2014 04:23:46 PM CEST -0.329272 seconds
CEST is a time zone abbreviation and stands for Central European Summer Time.
For information on how to change the time zone, see Configuring_the_Date_and_Time.adoc#sect-Configuring_the_Date_and_Time-timedatectl-Time_Zone.
Setting the Date and Time
Besides displaying the date and time, you can manually set the hardware clock to a specific time.
When you need to change the hardware clock date and time, you can do so by
appending the --set
and --date
options along with your
specification:
hwclock --set --date "dd mmm yyyy HH:MM"
Replace dd with a day (a two-digit number), mmm with a month (a three-letter abbreviation), yyyy with a year (a four-digit number), HH with an hour (a two-digit number), MM with a minute (a two-digit number).
At the same time, you can also set the hardware clock to keep the time in
either UTC or local time by adding the --utc
or
--localtime
options, respectively. In this case, UTC
or LOCAL
is recorded in the /etc/adjtime
file.
If you want to set the date and time to a specific value, for example, to
"21:17, October 21, 2014", and keep the hardware clock in UTC, run the
command as root
in the following format:
~]# hwclock --set --date "21 Oct 2014 21:17" --utc
Synchronizing the Date and Time
You can synchronize the hardware clock and the current system time in both directions.
-
Either you can set the hardware clock to the current system time by using this command:
hwclock --systohc
Note that if you use NTP, the hardware clock is automatically synchronized to the system clock every 11 minutes, and this command is useful only at boot time to get a reasonable initial system time.
-
Or, you can set the system time from the hardware clock by using the following command:
hwclock --hctosys
When you synchronize the hardware clock and the system time, you can also
specify whether you want to keep the hardware clock in local time or UTC by
adding the --utc
or --localtime
option. Similarly to
using --set
, UTC
or LOCAL
is recorded in the /etc/adjtime
file.
The hwclock --systohc --utc command is functionally similar to timedatectl set-local-rtc false and the hwclock --systohc --local command is an alternative to timedatectl set-local-rtc true.
To set the hardware clock to the current system time and keep the hardware
clock in local time, run the following command as root
:
~]# hwclock --systohc --localtime
To avoid problems with time zone and DST switching, it is recommended to keep the hardware clock in UTC. The shown Configuring_the_Date_and_Time.adoc#exam4-sect4-synchornizing-systohc-hwclock is useful, for example, in case of a multi boot with a Windows system, which assumes the hardware clock runs in local time by default, and all other systems need to accommodate to it by using local time as well. It may also be needed with a virtual machine; if the virtual hardware clock provided by the host is running in local time, the guest system needs to be configured to use local time, too.
Additional Resources
For more information on how to configure the date and time in Fedora 31, see the resources listed below.
-
timedatectl
(1) — The manual page for the timedatectl command line utility documents how to use this tool to query and change the system clock and its settings. -
date
(1) — The manual page for the date command provides a complete list of supported command line options. -
hwclock
(8) — The manual page for the hwclock command provides a complete list of supported command line options.
-
System Locale and Keyboard Configuration documents how to configure the keyboard layout.