Linux时间操作(time、gettimeofday)
来源:互联网 发布:淘宝天猫买汽车靠谱吗 编辑:程序博客网 时间:2024/06/10 10:14
自:http://blog.chinaunix.net/space.php?uid=24148050&do=blog&id=320294 一、time函数
一旦取得这种以秒计的很大的时间值后,通常要调用另一个时间函数将其变换为人们可读的时间和日期
#include <time.h>
//calendar time into a broken-down time expressed as UTC
struct tm *gmtime(const time_t *calptr);
//converts the calendar time to the local time, taking into account the local time zone and
//daylight saving time flag
struct tm *localtime(const time_t *calptr);
//converts it into a time_t value
time_t mktime(struct tm *tmptr);
struct tm { /* a broken-down time */
int tm_sec; /* seconds after the minute: [0 - 60] */
int tm_min; /* minutes after the hour: [0 - 59] */
int tm_hour; /* hours after midnight: [0 - 23] */
int tm_mday; /* day of the month: [1 - 31] */
int tm_mon; /* months since January: [0 - 11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday: [0 - 6] */
int tm_yday; /* days since January 1: [0 - 365] */
int tm_isdst; /* daylight saving time flag: <0, 0, >0 */
};
Tue Feb 10 18:27:38 2004/n/0
二、gettimeofday函数得到更精确的时间
time_t tv_sec; /*** second ***/
susecond_t tv_usec; /*** microsecond 微妙***/
}
1秒=1000毫秒,
1毫秒=1000微秒,
1微妙=1000纳秒,
1纳秒=1000皮秒。
秒用s表现,毫秒用ms,微秒用μs表示,纳秒用ns表示,皮秒用ps表示。
三、内核时间
内核有两个重要的全局变量:
unsigned long jiffies;
timeval xtime ;
jiffies 是记录着从电脑开机到现在总共的"时钟中断"的次数。
文件linux-2.6.24/kernel/timer.c
void do_timer(unsigned long ticks)
{
jiffies_64 += ticks;
update_times(ticks);
}
xtime 是从cmos电路或rtc芯片中取得的时间,一般是从某一历史时刻开始到现在的时间。
这个就是所谓的"墙上时钟walltimer",通过它可计算得出操作系统需要的日期时间,它的精确度是微秒。
xtime第一次赋值是在系统启动时调用timekeeping_init或time_init进行的
再调用read_persistent_clock进一步调用get_rtc_time得到的
PS:在/proc/uptime里面的两个数字分别表示:
the uptime of the system(seconds),
and the amount of time spent in idle process(seconds).
四、代码示例
“UTC时间字符串”与 “time函数返回值”互换
生成Date
------------------------------ 华丽的分割线 ------------------------------------
关于scanf的返回值
Both scanf and wscanf return the number of fields successfully converted
and assigned; the return value does not include fields that were read but
not assigned. A return value of 0 indicates that no fields were assigned.
The return value is EOF for an error or if the end-of-file character or the
end-of-string character is nocountered in the first attempt to read a character.
如:scanf("%d%d", &a, &b);
如果a和b都被成功读入,那么scanf的返回值就是2
如果只有a被成功读入,返回值为1
如果a和b都未被成功读入,返回值为0
如果遇到错误或遇到end of file,返回值为EOF。
scanf()的返回值对我们来说也很有用的,例如可使用if(scanf("%d,%d",&a,&b)==2)这样语句来判断是否正确的给所有的变量赋值了,正确的话才能使用这个变量与运算,这样才能提高我们代码的安全性。
- Linux时间操作(time、gettimeofday)
- Linux时间操作(time、gettimeofday)
- Linux时间操作(time、gettimeofday)
- Linux时间操作(time、gettimeofday)
- linux时间函数time和gettimeofday
- linux 上时间相关的函数 time ,_ftime , gettimeofday, clock_gettime
- Linux毫秒时间gettimeofday
- 时间相关 time() ctime time_t localtime() gettimeofday
- 时间相关 time() ctime time_t localtime() gettimeofday
- linux下常用的几个时间函数:time,gettimeofday,clock_gettime,_ftime
- linux下常用的几个时间函数:time,gettimeofday,clock_gettime,_ftime
- Linux下时间编程(综合知识+综合编程):time,ctime,strftime,localtime,gmtime,gettimeofday
- linux下常用的几个时间函数:time,gettimeofday,clock_gettime,_ftime
- linux时间函数gettimeofday解析
- linux时间函数gettimeofday解析
- 时间编程:time(),localtime(),gmtime(),asctime(),ctime(),gettimeofday(),settimeofday()
- gettimeofday时间
- Linux时间函数之gettimeofday()函数
- Photoshop Save for Web 功能 - PC
- Mongodb源码分析--主程序入口main()
- 八皇后问题的进化(2)-用递归函数实现
- Mongodb源码分析--主程序入口main()
- ADO.NET对象模型
- Linux时间操作(time、gettimeofday)
- DataSet对象
- Vim 常用命令
- sphinx配置文件书写完成(前期)
- mongodb源码分析--查询
- 内核与用户空间共享内存
- Mongodb源码分析--日志及持久化
- Mongodb源码分析--插入记录及索引B树构建
- 視圖 with check option&with read only