In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to view the system time in the Linux application layer? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
1. Basic concepts:
1. Linux system time and hardware time:
System time: generally speaking, it is the time we see when we execute the date command, which is used by all time calls under the Linux system (except for commands that directly access the hardware time).
Hardware time: the time in the BIOS on the motherboard, which is powered by the motherboard battery to maintain operation. Read this time when the system is powered on and set the system time according to it. (note: there may be time zone conversion in the process of setting the system time according to the hardware time when the system starts, depending on the specific system and related settings.
2. UTC time and problem time:
UTC time: Coordinated Universal Time, that is, coordinated Universal time, also known as Universal Unified time, World Standard time, International coordinated time. Under the general precision requirements, it is the same as GMT (Greenwich Mean Time, UT, Greenwich mean time).
Local time: due to different time zones, 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, the local time is Beijing time, which is CST on Linux (China Standard Time, Chinese standard time, Central Standart Time is also abbreviated to CST when paying attention to the American central standard, which is not the same thing as CST here), and the time zone is East Zone 8, that is, Zone + 8, so CST = UTC + (+ 8 hours)
For example, through the time instruction (explained below), I check the system time and hardware time of the embedded development board as follows:
As you can see from the figure above, it just validates the relationship of CST = UTC + time zone mentioned above.
Second, time instruction
1. System time date
Check the system time and the operation of UTC: call date directly to get the local time. Use date-u if you want to get UTC time
Set the operation of the system time: format: # date month, day, hour and year. Seconds. For example, the time I checked before is not correct. I want to change the time to the present time manually. Enter the following instructions:
It can also be seen from the instructions that the system time has indeed changed.
two。 Hardware time hwclock
Is the time displayed by calling hwclock directly the time in BIOS? Not exactly! It depends on whether UTC is enabled in the relevant configuration file. If UTC (UTC=true) is enabled, it shows the time converted by the time zone instead of the real time in BIOS. If you add the-localtime option, you always get the actual time in BIOS. Check the instructions as follows:
Because the instructions are entered one after another, they are not at the same time, but you can see that the hwclock instruction in the embedded development board looks at the actual time in the BIOS.
Set hardware time: format: hwclock-set-date== "month / day / year: minutes: seconds".
More detailed instructions can be viewed through the man and help instructions.
Other commonly used viewing time instructions
1. Uptime command
2. Check / proc/uptime file to calculate the startup time of the system
The first number is the time that the system has been running for 482.15 seconds. The startup time of the system can be calculated with the system tool date. The instructions are as follows:
Date-d "$(awk-F.'{print $1}'/ proc/uptime) second ago" + "% Y-%m-%d% H:%M:%S"
The results show that:
3. Calculate the running time of the system through / proc/uptime file. The instructions are as follows.
Cat / proc/uptime | awk-F. '{run_days=$1 / 86400X runaway minutes hourly = ($1% 86400) / 3600th runners minute = ($1% 3600) / 60th runners secondhands 1% 60ash printf ("system has run:% d days% d hours% d minutes\ n", run_days,run_hour,run_minute,run_second)}'
The results show that:
4. Who command
Who-b # View the last time the system booted
5. Last rebbot to check the last startup time of the Linux system
6. Some top and w can also check the time.
Fourth, the program for calculating the system time
Time is very important to the operating system, from the kernel level to the application layer, the expression and accuracy of time are the same. In the linux kernel, a constant called jiffes is used to calculate the tick count of time. In the application layer, the system time can be obtained through the tm structure by using time (), localtime () and other functions.
The tm structure with Linux kernel version 3.0.35 is as follows:
# ifndef _ TM_DEFINEDstruct tm {int tm_sec; / * second-value range is [0Magne 59] * / int tm_min; / * minute-value range is [0Magne 59] * / int tm_hour; / *-value range is [0Magne 23] * / int tm_mday; / * the date-value range in a month is [1jue 31] * / int tm_mon / * month (starting from January, 0 represents January)-value range is [0Magne11] * / int tm_year; / * year, its value is equal to the actual year minus 1900 * / int tm_wday; / * week-value range is [0prime6], where 0 represents Sunday, 1 represents Monday, and so on * / int tm_yday / * the number of days starting from January 1 of each year-the value range is [0365], where 0 represents January 1, 1 represents January 2, and so on * / int tm_isdst; / * daylight saving time identifier. When daylight saving time is implemented, tm_isdst is positive. If daylight saving time is not implemented, the tm_isdst is 0; if you do not know the situation, tm_isdst () is negative. , /
Functions that need to be used in the program:
The time function gets the calendar time. Calendar time is expressed as the number of seconds that have elapsed from a standard point in time to this time. This standard time point will be different for different compilers, but for a compilation system, this standard time point is constant, and the calendar time corresponding to the time in the compilation system is measured by this standard time point, so it can be said that calendar time is "relative time", but no matter which time zone you are in, at the same time for the same standard time point, the calendar time is the same.
The localtime () function converts calendar time to local time.
The asctime () function converts the time stored in the tm structure pointed to by timeptr into a string format returned as: back, Www Mmm dd hh:mm:ss yyyy. Where Www is the week; Mmm is the month; dd is the day; hh is the time; mm is the minute; ss is the second; yyyy is the year
The final code is as follows:
# 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 / / 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% d:%.2d:%.2d\ n", year, month, day, wday, hour, min, sec); printf ("The current date is% s", asctime (ptm)); return 0;}
The results show that:
This is the answer to the question about how to view the system time in the Linux application layer. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.