因种种原因,最近很少上cnblogs了。刚写了一个实现time的函数,可以通过该函数获取当前时间与1970年1月1日 0时0分0秒的差值,精确到秒,可以用在某些没有时候使用time不正确而不得不调用硬件时钟的场合。把其中HWCLOCK打开即可,关闭的话使用的是date获取的系统时间。其中打开时是应用在PPC架构的嵌入式linux上,具体源码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h> typedef unsigned short YEAR;
typedef unsigned short MONTH;
typedef unsigned short DAY;
typedef unsigned short HOUR;
typedef unsigned short MIN;
typedef unsigned short SEC; char g_month[][] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; #define HWCLOCK typedef struct struct_time
{
YEAR year;
MONTH mon;
DAY day;
HOUR hour;
MIN min;
SEC sec;
}time_s; void PrintTimeStruct(time_s *t)
{
if(t)
printf("%d-%d-%d %d:%d:%d\n",t->year,t->mon,t->day,t->hour,t->min,t->sec); } void FillInElem(time_s *t,char *buf,int index)
{
int i = ; switch(index)
{
//week
case :
{
break;
}
//month
case :
{
for(i = ; i < ; i++)
{
if(strcasecmp(buf,g_month[i]) == )
{
t->mon = i+;
break;
}
}
break;
}
//day
case :
{
t->day = atoi(buf);
break;
}
//time
case :
{
#ifdef HWCLOCK
buf[] = '\0';
t->hour = atoi(buf); buf[] = '\0';
t->min = atoi(buf+); t->sec = atoi(buf+);
#else
sscanf(buf,"%d:%d:%d",&t->hour,&t->min,&t->sec);
#endif
break;
}
#ifdef HWCLOCK
case :
{
t->year = atoi(buf);
break;
}
#else
//cst
case :
{
break;
}
//year
case :
{
t->year = atoi(buf);
break;
}
#endif default:break; } } void GetTimeStruct(time_s *t,char *buf)
{
int len = strlen(buf);
int i = ;
int j = ;
int index = ; char temp[]; if(!t || !buf)
{
return ;
} memset(temp,,sizeof(temp));
for(;i < len ; i++)
{
if(buf[i] != ' ')
{
temp[j] = buf[i];
j++;
}
else
{
index++;
//printf("get %s index %d\n",temp,index);
FillInElem(t,temp,index);
memset(temp,,sizeof(temp));
j = ;
#ifdef HWCLOCK
if(index == )
{
i++;
}
if(index >= )
{
return;
}
#endif
} } //printf("get %s \n",temp);
index++;
FillInElem(t,temp,index);
} int GetHWClock(time_s *t)
{
char buf[];
#ifdef HWCLOCK
system("hwclock > time.txt"); #else
system("date > time.txt"); #endif FILE *fp = NULL; if((fp = fopen("time.txt","r")) != NULL)
{
fgets(buf,sizeof(buf),fp);
fclose(fp); GetTimeStruct(t,buf);
//printf("get :%s",buf);
system("rm time.txt");
return ;
}
else
{
return ;
} } int IsRunYear(YEAR y)
{
if( (y % == ) || ( (y % == )&&(y % != ) ))
{
return ;
}
return ;
} unsigned long long GetTickCount(time_s *t)
{
if(t)
{
unsigned long long temp = ;
int RunYearNum = ;// run nian 365 int i = ; //run nian
for(i = ;i < t->year ; i++)
{
if( IsRunYear(i) == )
{
RunYearNum++;
}
} temp += RunYearNum* * * * ;
temp += (t->year - - RunYearNum)* * * * ;
//printf("%d run temp %llu\n",RunYearNum,temp); //month
int day = ;
for(i = ; i < t->mon ; i++)
{
if(i == )
{
if(IsRunYear(t->year) == )
{
day += ;
}
else
{
day += ;
}
}
else if((i == ) || (i == ) || (i == ) || (i == ) || (i == ) || (i == ))
{
day += ;
}
else
{
day += ;
}
} temp += day***;
//printf("day %d temp %llu\n",day,temp);
//day
temp += (t->day - )***; //hour
temp += (t->hour)**; //min
temp += (t->min)*; //sec
temp += t->sec; return temp; }
else
{
return ;
} } int main(void)
{
time_s t; while()
{
GetHWClock(&t);
PrintTimeStruct(&t); printf("tick %llu \n",GetTickCount(&t));
sleep();
} return ;
}

实现系统函数time,获取当前时间与UTC的间隔的更多相关文章

  1. 程序运行时间测试 - 使用系统函数 getrusage 获取程序运行时间

    https://github.com/yaowenxu/Workplace/blob/master/timer/getrusagetimer.c 关键结构体: struct rusage { stru ...

  2. linux和Windows双系统让 Windows 把硬件时间当作 UTC

    linux和Windows双系统让 Windows 把硬件时间当作 UTC Windows设置如下:开 始->运行->CMD,打开命令行程序(Vista则要以管理员方式打开命令行程序方可有 ...

  3. 前端PHP入门-020-重点日期函数之获取时期时间信息函数

    你需要知道关于时间的几个概念: 时区/世界时/unix时间戳 1.时区 这个概念,之前大家听说过很多.我们来啰嗦两句,我们现实生活中使用的实区,在电脑里面也是一样有规定的. 1884年在华盛顿召开国际 ...

  4. round函数和获取当前时间函数

    round函数:对一个浮点类型的数据进行四舍五入:round(3.14)   运行结果就是3 获取当前时间: time.time()    ,time函数需要导入才能使用,import time  

  5. vc 获取当前时间 (zhuan)

    vc 获取当前时间(2010-02-10 11:34:32) http://wenku.baidu.com/view/6ade96d049649b6648d7475e.html 1.使用CTime类 ...

  6. vc 获取当前时间

    1.使用CTime类 CString str; //获取系统时间 CTime tm; tm=CTime:: GetCurrentTime_r(); str=tm.Format("现在时间是% ...

  7. 日期时间函数 mysql 和sqlserver 中对于常用函数的日期和时间函数的区别

    1. sqlserver中获取时间用getdate(),默认返回格式是2019-01-21 13:58:33.053,具体的年月日,时分秒毫米,年月日之间用短线连接,时分秒之间用冒号连接,秒和毫米之间 ...

  8. 获取当前时间 YYYY-MM-DD

    1.函数封装 /** * 获取当前时间 * 格式YYYY-MM-DD */ Vue.prototype.getNowFormatDate = function() { var date = new D ...

  9. Sql Server 的本地时间和UTC时间

    一,本地时间和UTC时间 本地时间 世界的每个地区都有自己的本地时间,整个地球分为二十四时区,每个时区都有自己的本地时间. UTC时间 在国际无线电通信中,为统一而普遍使用一个标准时间,称为通用协调时 ...

随机推荐

  1. LEFT JOIN、Right、Full后ON和WHERE的区别

    今天在工作的时候碰到了一个问题,A表B表left join后在on后面关于A表的条件过滤语句没起到我想要的过滤作用,还是对左连接等理解的不够呀. SELECT * FROM student; SELE ...

  2. poj 2773(容斥原理)

    容斥原理入门题吧. Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9798   Accepted: 3 ...

  3. android开发找不到模拟器(PANIC: Could not open:)解决办法

    android开发找不到模拟器(PANIC: Could not open:)解决办法   2013/4/3 17:44:15 0人评论 213次浏览 分类:android开发 在系统环境变量设置名为 ...

  4. UVa 11107 (后缀数组 二分) Life Forms

    利用height值对后缀进行分组的方法很常用,好吧,那就先记下了. 题意: 给出n个字符串,求一个长度最大的字符串使得它在超过一半的字符串中出现. 多解的话,按字典序输出全部解. 分析: 在所有输入的 ...

  5. [转]使用 jQuery Mobile 与 HTML5 开发 Web App —— jQuery Mobile 事件详解

    在前文<使用 jQuery Mobile 与 HTML5 开发 Web App —— jQuery Mobile 默认配置与事件基础>中,Kayo 对 jQuery Mobile 事件的基 ...

  6. iso中自动伸缩属性

    一.自动伸缩属性 UIViewAutoresizingNone                 不伸缩 UIViewAutoresizingFlexibleLeftMargin   跟父控件左边的距离 ...

  7. UVA 11354 Bond 邦德 (RMQ,最小瓶颈MST)

    题意: n个城市,m条路,每条路有个危险值,要使得从s走到t的危险值最小.回答q个询问,每个询问有s和t,要求输出从s到t最小的危险值.(5万个点,10万条边) 思路: 其实要求的是任意点对之间的最小 ...

  8. ecshop 多语言切换

    1.打开includes/init.php找到下面两行代码并删除 require(ROOT_PATH . 'languages/' . $_CFG['lang'] . '/common.php'); ...

  9. reverse(), extend(), sort() methods of list

    >>> l = list('sdf') >>> l ['s', 'd', 'f'] >>> id(l) 4520422000 >>&g ...

  10. PHP校验ISBN码的函数

    国际标准书号(International Standard Book Number,ISBN:拟发音is-ben),是国际通用的图书或独立的出版物(除定期出版的期刊)代码.出版社可以通过国际标准书号清 ...