Full anatomy of the Linux kernel mktime.c

Source: Internet
Author: User

The definition of the TM structure is inside the time.h.

struct TM {    int tm_sec;    int tm_min;    int tm_hour;    int tm_mday;    int Tm_mon;    int tm_year;    int tm_wday;    int tm_yday;    int tm_isdst;};



/* * LINUX/KERNEL/MKTIME.C * * (c) 1991 Linus Torvalds */#include <time.h>/* * This isn ' t the library routine, it is only used in the kernel. * As such, we don ' t care on years<1970 etc, but assume everything * is OK. Similarly, TZ etc is happily ignored. We just do everything * as easily as possible. Let's find something public for the library * routines (although I think Minix times are public). *//* * PS. I hate whoever though up the year 1970-couldn ' t they has gotten * a leap-year instead?

I also hate Gregorius, Pope or No. i ' M grumpy. */#define MINUTE 60//一分钟60秒. Here 60 is the count in seconds # # HOUR (60*minute)//hour 60 Minutes # define Day (24*hour)//24 hours a year (365*day)//365 days/day/* inte restingly, we assume leap-years */static int month[12] = {//Initialize the number of seconds to start every one months, that is, the starting time in seconds 0, day* (+), day* (31+29), day* (31+29+31), day* (31+29+31+30), day* (31+29+31+30+31), day* (31+29+31+30+31+30), day* (31+29+31+30+31+30+31), day* (31+29+31+30+31+30+31+31), day* (31+29+31+30+31+30+31+31+30), day* (31+29+31+30+31+30+31+31+30+31), DAY* (31 +29+31+30+31+30+31+31+30+31+30)};long kernel_mktime (struct TM * tm) {long res; int year; Year = tm->tm_year-70;//from the beginning of 1970. Year get is and 70 years difference/* Magic offsets (y+1) needed to get leapyears right.*/res = year*year + day* ((year+1)/4);//Leap day multiple days re s + = month[tm->tm_mon];//time is accurate to month/* and (y+2) here. If it wasn ' t a leap-year, we have to adjust */if (tm->tm_mon>1 && ((year+2)%4)//due to be counted from 1970, here year+2 can be coupled on a leap year ... In other words, 1972 is a leap year, but the annual difference is 2. The 2 is aligned here.

。。 Res-= Day;//year is not a leap year. Minus one day res + = day* (tm->tm_mday-1); Res + = hour*tm->tm_hour; Res + = minute*tm->tm_min; Res + = tm->tm_sec;//Time accurate to seconds return res;}












Full anatomy of the Linux kernel mktime.c

Related Article

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.