Linux时间操作(time、gettimeofday)

摘要:
秒以秒表示,毫秒以毫秒表示,微秒以微秒表示μs,纳秒以纳秒表示,皮秒以ps表示。文件linux-2.6.24/kernel/timer cvoiddo_timer{jiffies_64+=ticks;update_times;}Xtime是从cmos电路或rtc芯片获得的时间,通常从历史时刻到现在。这就是所谓的“挂钟”。它可以计算操作系统所需的日期和时间,精度为微秒。

http://blog.chinaunix.net/uid/24148050.html

 http://blog.csdn.net/scottgly/article/details/6568513

一、time函数   

  1. #include <time.h>
  2. time_t time(time_t *calptr);
返回距计算机元年的秒数
一旦取得这种以秒计的很大的时间值后,通常要调用另一个时间函数将其变换为人们可读的时间和日期

#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 */
   };  
  1. char *asctime(const struct tm *tmptr);
  2. char *ctime(const time_t *calptr);

  3. asctime()和ctime()函数产生形式的26字节字符串,这与date命令的系统默认输出形式类似:
        Tue Feb 10 18:27:38 2004/n/0
Linux时间操作(time、gettimeofday)第1张
二、gettimeofday函数得到更精确的时间

  1. #include <sys/time.h>
  2. int gettimeofday(struct timeval *restrict tp, void *restrict tzp);
  3. 第二个形参是基于平台实现的,使用的时候最好用NULL
struct timeval{
    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函数返回值”互换
  1. int64_t TimeToUTC(char *time)
  2. {
  3.     struct tm temp1;
  4.     int rc;
  5.     int year;
  6.     int mon;
  7.     int day;
  8.     int hour;
  9.     int min;
  10.     int sec;
  11.     rc = sscanf(time, "%4d-%2d-%2d %2d:%2d:%2d",
  12.                     &(temp1.tm_year),
  13.                     &(temp1.tm_mon),
  14.                     &(temp1.tm_mday),
  15.                     &(temp1.tm_hour),
  16.                     &(temp1.tm_min),
  17.                     &(temp1.tm_sec));
  18.     sscanf(time, "%4d-%2d-%2d %2d:%2d:%2d",
  19.                     &year,
  20.                     &mon,
  21.                     &day,
  22.                     &hour,
  23.                     &min,
  24.                     &sec);
  25.     if((rc<6)
  26.         || (temp1.tm_year<1900) || (temp1.tm_year>2100)
  27.         || (temp1.tm_mon<1) || (temp1.tm_mon>12)
  28.         || (temp1.tm_mday<1) || (temp1.tm_mday>31)
  29.         || (temp1.tm_hour<0) || (temp1.tm_hour>23)
  30.         || (temp1.tm_min<0) || (temp1.tm_min>59)
  31.         || (temp1.tm_sec<0) || (temp1.tm_sec>59))
  32.     {
  33.         return -1;
  34.     }
  35.     temp1.tm_mon -= 1;
  36.     temp1.tm_year -= 1900;
  37.     struct tm temp3;
  38.     time_t temp2 = mktime(&temp1);
  39.     if (temp2 == -1){
  40.         return -1;
  41.     }
  42.     else{
  43.         localtime_r(&temp2, &temp3);
  44.         if (!((mon == (temp3.tm_mon+1))
  45.             && (day == temp3.tm_mday)
  46.             &&(year == (temp3.tm_year+1900)))){
  47.             return -1;
  48.         }
  49.         else{
  50.                 return temp2*1000000LL;
  51.         }
  52.     }
  53. }
  54. void UTCToTime(int64_t utc, char * clock)
  55. {
  56.     struct tm temp1;
  57.     time_t sec;
  58.     float msec;
  59.     int rc;
  60.     sec = utc/1000000;
  61.     msec = utc/1000000.0-sec;
  62.     localtime_r(&sec, &temp1);
  63.     temp1.tm_year += 1900;
  64.     temp1.tm_mon += 1;
  65.     rc = sprintf(clock, "%04d-%02d-%02d %02d:%02d:%02d",
  66.                     temp1.tm_year,
  67.                     temp1.tm_mon,
  68.                     temp1.tm_mday,
  69.                     temp1.tm_hour,
  70.                     temp1.tm_min,
  71.                     temp1.tm_sec);
  72.     clock[rc] = '/0';
  73. }

生成Date
  1. static char * g_weekstr[7] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
  2. static char * g_monthstr[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  3.                 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  4. /*generate the current date string*/
  5. time_t now = time(NULL);
  6. struct tm tt;
  7. gmtime_r( &now, &tt );
  8. char timebuf[64];
  9. snprintf( timebuf, sizeof(timebuf),
  10.         "%s, %02d %s %d %02d:%02d:%02d GMT",
  11.         g_weekstr[tt.tm_wday], tt.tm_mday,
  12.         g_monthstr[tt.tm_mon], tt.tm_year + 1900,
  13.         tt.tm_hour, tt.tm_min, tt.tm_sec );

------------------------------ 华丽的分割线 ------------------------------------

关于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。
  1. void main()
  2. {
  3.     int a;
  4.     int b;
  5.     int c;
  6.     int x;
  7.     printf("请输入三个整数:");
  8.     x=scanf("%d%d%d",&a,&b,&c);
  9.     printf("%d/n%d/n",a,x);
  10. }
  11. 输入三个整数:5 6 7则x的值为3;
  12. 输入5 6 d(即给c 赋值不正确)则x的值为2;
  13. 输入5 t d(即给b和c 赋值不正确)则x的值为1;

scanf()的返回值对我们来说也很有用的,例如可使用if(scanf("%d,%d",&a,&b)==2)这样语句来判断是否正确的给所有的变量赋值了,正确的话才能使用这个变量与运算,这样才能提高我们代码的安全性。

免责声明:文章转载自《Linux时间操作(time、gettimeofday)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇使用存储过程实现批量删除1-5.引入样式下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

c语言进阶6-指针

指针是c语言的一个重要组成部分 是c语言的核心、精髓所在,用好指针可以在c语言编程中起到事半功倍的效果。一方面,可以提高程序的编译效率和执行速度以及实现动态的存储分配;另一方面,使用指针可使程序更灵活,全球表示各种数据结构,编写高质量的程序。 指针是c语言显著的优点之一,其使用起来十分灵活而且能提高某些程序的效率,但是如果使用不当则很容易造成系统错误。许多...

C++ 获取文件夹下的所有文件名(此方法只适用于win)

获取文件夹下所有的文件名是常用的功能,今天再一次有这样的需求,所有就在网上查找了很多,并记下以供后用。 原文:http://blog.csdn.NET/cxf7394373/article/details/7195661 原文:http://qiaoxinwang.blog.163.com/blog/static/86096452010612139172...

time,gettimeofday,clock_gettime

time()提供了秒级的精确度 1、头文件 <time.h> 2、函数原型 time_t time(time_t * timer) 函数返回从UTC1970-1-1 0:0:0开始到现在的秒数 用time()函数结合其他函数(如:localtime、gmtime、asctime、ctime)可以获得当前系统时间或是标准时间。 #inc...

PHP 5 Date/Time 函数

PHP Date/Time 简介 Date/Time 函数允许您从 PHP 脚本运行的服务器上获取日期和时间。您可以使用 Date/Time 函数通过不同的方式来格式化日期和时间。 注释:这些函数依赖于服务器的本地设置。使用这些函数时请记住要考虑夏令时和闰年。 安装 PHP Date/Time 函数是PHP 核心的组成部分。无需安装即可使用这些函数。...

python编程系列---进程池的优越性体验

1.通过multiprocessing.Process()类创建子进程 1 import multiprocessing, time, os, random 2 3 4 def work(index): 5 """ 6 任务 7 :param index:任务索引号 8 """ 9 start_...

算法训练 安慰奶牛

算法训练 安慰奶牛 时间限制:1.0s 内存限制:256.0MB 问题描述 Farmer John变得非常懒,他不想再继续维护供奶牛之间供通行的道路。道路被用来连接N个牧场,牧场被连续地编号为1到N。每一个牧场都是一个奶牛的家。FJ计划除去P条道路中尽可能多的道路,但是还要保持牧场之间 的连通性。你首先要决定那些道路是需要保留的N-1条道路。第j条双向...