/*
* linux/kernel/mktime.c
*
* (C) 1991 Linus Torvalds
*/

#include <time.h>		// ʱͷļ˱׼ʱݽṹtm һЩʱ亯ԭ͡

/*
* This isn't the library routine, it is only used in the kernel.
* as such, we don't care about 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 is public).
*/
/*
* PS. I hate whoever though up the year 1970 - couldn't they have gotten
* a leap-year instead? I also hate Gregorius, pope or no. I'm grumpy.
 */
/*
 * ⲻǿ⺯ںʹáǲС1970 ݵȣٶһо
 * ͬʱTZ ҲȺԡֻǾܼ򵥵ش⡣ҵһЩĿ⺯
 * Ϊminix ʱ亯ǹģ
 * ⣬ҺǸ1970 꿪ʼ - ѵǾͲѡһ꿪ʼҺ޸
 * ̻ʡ̣ʲôںǸƢˡ
 */
#define MINUTE 60		// 1 ӵ
#define HOUR (60*MINUTE)	// 1 Сʱ
#define DAY (24*HOUR)		// 1 
#define YEAR (365*DAY)		// 1 

/* interestingly, we assume leap-years */
/* Ȥǿǽ */
// Ϊޣÿ¿ʼʱʱ顣
static int month[12] = {
  0,
  DAY * (31),
  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)
};

// ú1970 1 1 0 ʱ𵽿վΪʱ䡣
long
kernel_mktime (struct tm *tm)
{
  long res;
  int year;

  year = tm->tm_year - 70;	// 70 굽ھ(2 λʾʽ)
  // ˻2000 ⡣
  /* magic offsets (y+1) needed to get leapyears right. */
  /* Ϊ˻ȷҪһħƫֵ(y+1) */
  res = YEAR * year + DAY * ((year + 1) / 4);	// Щ꾭ʱ + ÿʱ1 
  res += month[tm->tm_mon];	// ʱ䣬ڼϵ굽ʱ
  /* and (y+2) here. If it wasn't a leap-year, we have to adjust */
  /* Լ(y+2)(y+2)꣬ôǾͱе(ȥһʱ) */
  if (tm->tm_mon > 1 && ((year + 2) % 4))
    res -= DAY;
  res += DAY * (tm->tm_mday - 1);	// ټϱ¹ȥʱ䡣
  res += HOUR * tm->tm_hour;	// ټϵȥСʱʱ䡣
  res += MINUTE * tm->tm_min;	// ټ1 Сʱڹȥķʱ䡣
  res += tm->tm_sec;		// ټ1 ѹ
  return res;			// ڴ1970 ʱ䡣
}
