因种种原因,最近很少上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. ogre世界坐标鱼屏幕坐标相互转换

    bool worldCoordToScreen(Vector3 objPos, Camera* cam, Vector2 screenRect,  Vector2& screenPos) { ...

  2. JavaScript关闭窗口的方法

    当你创建了一个新窗口时,将open()方法的返回值分配给一个变量非常重要.比如,下面的语句就是创建一个新窗口,然后立即关闭它: win = window.open("http://www.d ...

  3. core--进程

    前面我们说了线程是一系列的指令,那么进程是什么呢?进程就是装下这些指令的容器.该容器除了线程,还包含资源等内容.一个进程至少要有一个线程.没有线程的进程就不叫进程 "进程和程序的区别:进程必 ...

  4. UVa 11481 (计数) Arrange the Numbers

    居然没有往错排公式那去想,真是太弱了. 先在前m个数中挑出k个位置不变的数,有C(m, k)种方案,然后枚举后面n-m个位置不变的数的个数i,剩下的n-k-i个数就是错排了. 所以这里要递推一个组合数 ...

  5. Codeforces 505 A Mr. Kitayuta's Gift【暴力】

    题意:给出一个字符串,可以向该字符串的任意位置插入一个字母使其变成回文串 因为字符串的长度小于10,枚举插入的字母,和要插入的位置,再判断是否已经满足回文串 #include<iostream& ...

  6. PHP基础 CGI,FastCGI,PHP-CGI与PHP-FPM

    CGI CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI可以用任何一 ...

  7. Write operations are not allowed in read-only mode错误

    (转+作者个人理解) 最近在配置 Structs, Spring 和Hibernate整合的问题: 开启OpenSessionInViewFilter来阻止延迟加载的错误的时候抛出了这个异常: org ...

  8. Java多线程-工具篇-BlockingQueue

    前言: 在新增的Concurrent包中,BlockingQueue很好的解决了多线程中,如何高效安全“传输”数据的问题.通过这些高效并且线程安全的队列 类,为我们快速搭建高质量的多线程程序带来极大的 ...

  9. cocos2d-x for android:SimpleGame分析

    cocos2d-x for android:SimpleGame分析 作为cocos2d-x的标配DEMO,SimpleGame可算是给入门学cocos2d-x的俺们这些新手门学习的对象了,那么来分析 ...

  10. WCF tcpTrace.exe配置

    Web.config <?xml version="1.0" encoding="utf-8"?> <configuration> &l ...