《linux 内核全然剖析》 mktime.c
tm结构体的定义在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 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.
*/
#define MINUTE 60 //一分钟60秒。这里60是以秒为单位计数
#define HOUR (60*MINUTE) //一小时60分钟
#define DAY (24*HOUR) //一天24小时
#define YEAR (365*DAY) //一年365天 /* 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)
}; long kernel_mktime(struct tm * tm)
{
long res;
int year; year = tm->tm_year - 70;//从1970年開始计时。year得到的是和70年的年差
/* magic offsets (y+1) needed to get leapyears right.*/
res = YEAR*year + DAY*((year+1)/4);//闰年多一天
res += month[tm->tm_mon];//时间精确到月
/* and (y+2) here. If it wasn't a leap-year, we have to adjust */
if (tm->tm_mon>1 && ((year+2)%4))
//由于是从1970年算起的,这里year+2就能耦合上闰年。。。换而言之,1972年是闰年,可是年差是2。这里补上2就对齐了。 。 。。
res -= DAY;//year不是闰年。则减一天
res += DAY*(tm->tm_mday-1);
res += HOUR*tm->tm_hour;
res += MINUTE*tm->tm_min;
res += tm->tm_sec;//时间精确到秒
return res;
}
《linux 内核全然剖析》 mktime.c的更多相关文章
- 《linux 内核全然剖析》 fork.c 代码分析笔记
fork.c 代码分析笔记 verifiy_area long last_pid=0; //全局变量,用来记录眼下最大的pid数值 void verify_area(void * addr,int s ...
- 《linux 内核全然剖析》sched.c sched.h 代码分析笔记
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011368821/article/details/25129835 sched.c sched.h ...
- 《linux 内核全然剖析》 chapter 2 微型计算机组成结构
微型计算机组成结构 系统的基本组成: 软件是一种控制硬件操作和动作的指令流. 2.1 微型计算机的组成原理 当中CPU通过地址线,数据线,和控制信号线组成的内部总线与系统其它部分进行数据通信.地址线用 ...
- 《linux 内核全然剖析》 sys.c 代码分析
sys.c 代码分析 setregid /* * This is done BSD-style, with no consideration of the saved gid, except * th ...
- 《linux 内核全然剖析》编译linux 0.12 内核 Ubuntu 64bits 环境
我×.. . 最终好了,大概3 4个小时吧...各种毛刺问题.终究还是闯过来了.. .. ubuntu2@ubuntu:~/Downloads/linux-0.00-050613/linux-0.00 ...
- 《linux 内核全然剖析》 include/asm/io.h
include/asm/io.h #define outb(value,port) \ __asm__ ("outb %%al,%%dx"::"a" (valu ...
- 《linux 内核全然剖析》 chapter 4 80x86 保护模式极其编程
80x86 保护模式极其编程 首先我不得不说.看这章真的非常纠结...看了半天.不知道这个东西能干嘛.我感觉唯一有点用的就是对于内存映射的理解...我假设不在底层给80x86写汇编的话.我 ...
- 《linux 内核全然剖析》 笔记 CODE_SPACE 宏定义分析
在memory.c里面.遇到一个宏定义,例如以下: #define CODE_SPACE(addr) ((((addr)+4095)&~4095) < \ current->sta ...
- 第三十二课 linux内核链表剖析
__builtin_prefetch是gcc扩展的,用来提高访问效率,需要硬件的支持. 在标准C语言中是不允许static inline联合使用的. 删除依赖的头文件,将相应的结构拷贝到LinuxLi ...
随机推荐
- 微信小程序--问题汇总及详解之清空电话号码
wxml: <view class="btns" wx:for="{{phoneList}}" wx:key="id"> < ...
- [openjudge6043]哆啦A梦的时光机
[openjudge6043]哆啦A梦的时光机 试题描述 哆啦A梦有一个神奇的道具:时光机.坐着它,大雄和他的伙伴们能穿越时空,回到过去或者去到未来. 有一天,大雄和他的伙伴们想穿越时空进行探险,可是 ...
- crontab中执行java程序的脚本
测试场景说明(操作系统:centos7): 有一个bash脚本,脚本内容是执行某个java程序,该脚本为 /data/project1/start.sh crontab -e,添加了以下任务: * * ...
- serviceImpl中,方法加@Override注释后报错
@Override public List<SysAdminMenu> getAdminMenusAll() { return sysAdminMenuMapper.getAdminMen ...
- leetcode 27 水
class Solution { public: int removeElement(vector<int>& nums, int val) { int length=nums.s ...
- jquery 实践操作:attr()方法
此篇要记录的是 关于 jquery 的 attr() 方法 在JS中设置节点的属性与属性值用到setAttribute(),获得节点的属性与属性值用到getAttribute(),而在jquery中 ...
- Python操作MySQL[转]
本篇对于Python操作MySQL主要使用两种方式: 1.原生模块pymsql. 2.ORM框架SQLAchemy. pymsql pymsql是Python中操作MySQL的模块,其使用方法和MyS ...
- SPOJ QTREE Query on a tree V
You are given a tree (an acyclic undirected connected graph) with N nodes. The tree nodes are number ...
- Bzoj3652 大新闻
Time Limit: 10 Sec Memory Limit: 512 MBSec Special JudgeSubmit: 215 Solved: 112 Description Input ...
- codechef AUG17 T4 Palindromic Game
Palindromic Game Problem Code: PALINGAM There are two players A, B playing a game. Player A has a st ...