How to view system time on the Linux Application Layer

Source: Internet
Author: User
Tags time zones

How to view system time on the Linux Application Layer

I. Basic concepts:
1. linux system time and hardware time:
System Time: Generally, it is the time we run the date command to view the time. in Linux, all the time calls (except the command for directly accessing the hardware time) use this time.
Hardware time: the time in the BIOS on the motherboard, which is maintained by the battery power of the motherboard. The system reads the time and sets the system time based on it. (Note: There may be time zone conversion during system startup based on the hardware time, depending on the specific system and related settings ).
2. UTC time and problem time:
UTC Time: Coordinated Universal Time, that is, the Coordinated Universal Time, also known as the unified Time of the world, the standard Time of the world, and the international coordination Time. In general precision requirements, it is the same as GMT (Greenwich Mean Time, universal Time UT is Greenwich Mean Time.
Local time: because it is in different time zones, the local time is generally different from UTC. The conversion method is: local time = UTC + time zone. The Time zone is positive in the east and negative in the West. For example, in China, Beijing Time is used for local Time. in Linux, CST (China Standard Time, China Standard Time, note that the Central Standart Time is also abbreviated as CST when the Central Standard is in the United States, which is not the same as CST here). The Time zone is UTC + 8, therefore, CST = UTC + (+ 8 hours)
For example, I use the time command (which will be explained below) to view the system time and hardware time of the embedded development board as follows:

It can be seen that the relationship between CST = UTC + time zone mentioned above is verified.
Ii. Time commands
1. system time date
View the system time and UTC operation: Call date directly to obtain the local time. If you want to get the UTC time, use date-u;
Operation for setting system time: Format: # date, month, day, hour, year, second. For example, if the time I checked is incorrect, you need to manually change it to the current time. Enter the following command:

You can also see through commands that the system time has indeed changed.
2. Hardware time hwclock
Is the time displayed when hwclock is called directly in the BIOS? Not necessarily! It depends on whether UTC is enabled in the relevant configuration file. If UTC (UTC = true) is enabled, the displayed time is converted by the time zone instead of the real time in the BIOS, if the-localtime option is added, the actual time in BIOS is always obtained. The command is as follows:

Because commands are input successively, they are not at the same time. However, we can see that the hwclock command in the embedded development board is the actual time in BIOS.
Hardware time setting operation: Format: hwclock-set-date = "month/day/year hour: minute: second ".
More detailed commands can be viewed using man and help commands.
Iii. Other commonly used viewing time commands
1. uptime command

2. View hosts? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcHJvYy91cHRpbWXOxLz + vMbL48 + others" 5 "src =" http://www.bkjia.com/uploads/allimg/160329/041005N19-4.png "title =" \ "/>
The first number is the system running time of 482.15 seconds. You can use the system tool date to calculate the system startup time. The command is as follows:

date -d "$(awk -F. '{print $1}' /proc/uptime) second ago" +"%Y-%m-%d %H:%M:%S"

Result:

3. Use the/proc/uptime file to calculate the system running time. The command is as follows;

Cat/proc/uptime | awk-F. '{run_days = $1/86400; run_hour = ($ 1% 86400 3600)/1%; run_minute = ($3600 1%)/60; run_second = $60; printf ("the system is running: % d day % d hour % d minute % d Second \ n ", run_days, run_hour, run_minute, run_second )}'

Result:

4. who command
Who-B # view the last system startup time

5. last rebbot: view the last Linux Startup time

6. You can also view the top and w times.


4. Program for calculating system time
Time is very important to the operating system. From the kernel level to the application layer, time expressions and precision are the same. In the Linux kernel, a constant named jiffes is used to calculate the number of ticking time. At the application layer, you can use time (), localtime (), and other functions to obtain the system time through the tm struct.
The tm struct of Linux kernel version 3.0.35 is as follows:

# Ifndef _ TM_DEFINEDstruct tm {int tm_sec;/* Second-value range: [] */int tm_min;/* minute-value range: [] */int tm_hour; /* Time-value range: [] */int tm_mday;/* date in a month-value range: [] */int tm_mon; /* month (from January 1, 1900, 0 indicates January 1,)-the value range is [] */int tm_year;/* the year, and the value is equal to the actual year minus */int tm_wday; /* day-value range: [0, 6], where 0 represents Sunday, 1 represents Monday, and so on */int tm_yday; /* The number of days starting from January 1, 0,365-the value range is []. 0 indicates January 1, and so on. */int tm_isdst;/* The identifier of the hour When the sequence is implemented, tm_isdst is positive. When the sequence is not implemented, the tm_isdst value is 0. If the sequence is unknown, the tm_isdst () value is negative. */


Functions required in the program:
The time function obtains the calendar time. The calendar time is represented by "the number of seconds elapsed from a standard time point to the current time. This standard time point varies with compilers, but for a compilation system, this standard time point remains unchanged, the calendar time corresponding to the time in the compilation system is measured by the standard time point. Therefore, it can be said that the calendar time is relative time, But no matter which time zone you are in, the calendar time is the same for the same standard time point at the same time point.
The localtime () function converts the calendar time to the local time.
The asctime () function converts the stored time in the tm struct pointed to by timeptr to the string format: Back, Www Mmm dd hh: mm: ss yyyy. Www indicates a week, Mmm indicates a month, dd indicates a day, hh indicates a week, mm indicates a minute, ss indicates a second, and yyyy indicates a year.
The final code is as follows:

# Include  # Include  # Include  # Include  Int print_system_boot_time (); int main (int argc, char ** argv) {if (print_system_boot_time ()! = 0) return-1; return 0;} print_system_boot_time () {int year, month, day, wday, hour, min, sec; struct sysinfo info; struct tm * ptm = NULL; time_t cur_time = 0; if (sysinfo (& info) {fprintf (stderr, "Failed to get sysinfo, errno: % u, reason: % s \ n ", errno, strerror (errno);} time (& cur_time); ptm = localtime (& cur_time); year = ptm-> tm_year + 1900; // year month = ptm-> tm_mon + 1; // month day = ptm-> tm_mday; // day wday = ptm-> tm_wday; // week hour = ptm-> tm_hour; // hour min = ptm-> tm_min; // minute sec = ptm-> tm_sec; // second printf ("The current date: % d-% -. 2d-% d week % d: %. 2d: %. 2d \ n ", year, month, day, wday, hour, min, sec); printf (" The current date is % s ", asctime (ptm )); return 0 ;}    

Result:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.