实现系统函数time,获取当前时间与UTC的间隔
因种种原因,最近很少上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的间隔的更多相关文章
- 程序运行时间测试 - 使用系统函数 getrusage 获取程序运行时间
https://github.com/yaowenxu/Workplace/blob/master/timer/getrusagetimer.c 关键结构体: struct rusage { stru ...
- linux和Windows双系统让 Windows 把硬件时间当作 UTC
linux和Windows双系统让 Windows 把硬件时间当作 UTC Windows设置如下:开 始->运行->CMD,打开命令行程序(Vista则要以管理员方式打开命令行程序方可有 ...
- 前端PHP入门-020-重点日期函数之获取时期时间信息函数
你需要知道关于时间的几个概念: 时区/世界时/unix时间戳 1.时区 这个概念,之前大家听说过很多.我们来啰嗦两句,我们现实生活中使用的实区,在电脑里面也是一样有规定的. 1884年在华盛顿召开国际 ...
- round函数和获取当前时间函数
round函数:对一个浮点类型的数据进行四舍五入:round(3.14) 运行结果就是3 获取当前时间: time.time() ,time函数需要导入才能使用,import time
- vc 获取当前时间 (zhuan)
vc 获取当前时间(2010-02-10 11:34:32) http://wenku.baidu.com/view/6ade96d049649b6648d7475e.html 1.使用CTime类 ...
- vc 获取当前时间
1.使用CTime类 CString str; //获取系统时间 CTime tm; tm=CTime:: GetCurrentTime_r(); str=tm.Format("现在时间是% ...
- 日期时间函数 mysql 和sqlserver 中对于常用函数的日期和时间函数的区别
1. sqlserver中获取时间用getdate(),默认返回格式是2019-01-21 13:58:33.053,具体的年月日,时分秒毫米,年月日之间用短线连接,时分秒之间用冒号连接,秒和毫米之间 ...
- 获取当前时间 YYYY-MM-DD
1.函数封装 /** * 获取当前时间 * 格式YYYY-MM-DD */ Vue.prototype.getNowFormatDate = function() { var date = new D ...
- Sql Server 的本地时间和UTC时间
一,本地时间和UTC时间 本地时间 世界的每个地区都有自己的本地时间,整个地球分为二十四时区,每个时区都有自己的本地时间. UTC时间 在国际无线电通信中,为统一而普遍使用一个标准时间,称为通用协调时 ...
随机推荐
- JS 点击事件学习总结
废话篇: 在我们编写无论什么网页内容的时候我们总是或多或少的要接触到点击事件这一范畴的,写过几个简短的demo和网页之后,感觉自己对电机事件一类的东西了解也更为的清楚了,之前写过很多零零散散的东西现在 ...
- Android在OnCreate中获取控件的宽度和高度
在Android中,有时需要对控件进行测量,得到的控件宽度和高度可以用来做一些计算.在需要自适应屏幕的情况下,这种计算就显得特别重要.另一方便,由于需求的原因,希望一进入界面后,就能得到控件的宽度和高 ...
- 一个发光的搜索边框(纯CSS3)
这是效果图,边框会不停的闪,兼容各种浏览器 HTML代码: <body> <div class="container"> <form method=& ...
- 13.Object-C--浅谈Foundation框架常用的结构体
------- android培训.iOS培训.期待与您交流! ---------- 昨天学习了Foundation框架中常用的结构体,下面我简单的总结一下,如果错误麻烦请留言指正,谢谢! Found ...
- SQLServer—系统中的内存配置
前言: 本文讲述32位和64位系统中的内存配置,在SQLServer 2005/2008中,DBA们往往尝试开启AWE来限制内存.但是,在SQLServer2012以后,这个选项将被弃用,所以不能使用 ...
- Linux Autotools
/********************************************************************** * Linux Autotools * 说明: * 我们 ...
- hihoCoder #1179 : 永恒游戏 (暴力枚举)
题意: 给出一个有n个点的无向图,每个点上有石头数个,现在的游戏规则是,设置某个点A的度数为d,如果A点的石子数大于等于d,则可以从A点给每个邻接点发一个石子.如果游戏可以玩10万次以上,输出INF, ...
- 决策树之 CART
继上篇文章决策树之 ID3 与 C4.5,本文继续讨论另一种二分决策树 Classification And Regression Tree,CART 是 Breiman 等人在 1984 年提出的, ...
- UVa11582 Colossal Fibonacci Numbers!
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> us ...
- AIX 第4章 指令记录
root@db:/#lsdev -Cc disk --查看磁盘设备信息 -C customized -c class hdisk0 Available 00-08-00 SAS Dis ...