因种种原因,最近很少上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. pl/sql programming 03 语言基础

    PL/SQL 块结构 最小的有意义的代码单元叫做 块(block). 一个块是一组代码, 这个块给出了执行边界, 也为变量声明和异常处理提供了作用范围, pl/sql 准许我们创建匿名块和命名块, 命 ...

  2. mac 10.8 编译提示找不到GCC

    本机已安装xcode,但是在编译prce时提示找不到GCC,要不安装gcc.pkg ,简单起见,启动xcode->menu->preferences->Dowloads 把里面的co ...

  3. Qt Assistant介绍

    简介 Qt Assistant也就是我们常说的Qt助手,是一款用于呈现在线文档的工具. 简介 一分钟学会使用 Qt参考文档 Qt Assistant详解 命令行选项 工具窗口 文档窗口 工具栏 菜单 ...

  4. iOS NSNotificationCenter(消息机制)

    转自:http://blog.csdn.net/liliangchw/article/details/8276803 对象之间进行通信最基本的方式就是消息传递,在Cocoa中提供Notificatio ...

  5. mac 安装使用 webp 来压缩图片

    学习性网站: https://developers.google.com/speed/webp/docs/cwebp http://www.w3ctech.com//topic/1672 https: ...

  6. BZOJ 4636 蒟蒻的数列

    二分写错了血T..... 线段树标记永久化. #include<iostream> #include<cstdio> #include<cstring> #incl ...

  7. android 自定义组件-带图片的textView

    1. 定义属性 <?xml version="1.0" encoding="utf-8"?> <resources> <decla ...

  8. 【JavaScript学习笔记】调用google搜索

    <html> <form method=get action="http://www.google.com/search"> <a href=&quo ...

  9. 【英语】Bingo口语笔记(28) - 表示“秘密”

  10. 【英语】Bingo口语笔记(72) - play系列