8位单片机可用的 mktime localtime函数
8位单片机可用的 mktime localtime函数及源码
最近在做一个8位单片机项目,其中用到了时间戳转换函数,这个在32位机上一个库函数就解决了问题,没想到在8位单片机中没有对应库(time.h),没有办法只有自己来写。
目标:1,满足和库函数mktime localtime所计算出的数据一至;2,考虑8位单片机的处理能力慢软件效率问题。
分享给大家,方便有同样需求的朋友。
gcc 环境进行测试:
测试程序:
#include <stdio.h>
#include <stdint.h>
#include <time.h>
#include <string.h> #if 0
struct tm {
int tm_sec; /* seconds after the minute, 0 to 60
(0 - 60 allows for the occasional leap second) */
int tm_min; /* minutes after the hour, 0 to 59 */
int tm_hour; /* hours since midnight, 0 to 23 */
int tm_mday; /* day of the month, 1 to 31 */
int tm_mon; /* months since January, 0 to 11 */
int tm_year; /* years since 1900 */
// int tm_wday; /* days since Sunday, 0 to 6 */
// int tm_yday; /* days since January 1, 0 to 365 */
// int tm_isdst; /* Daylight Savings Time flag */
};
#endif
static const char mon_list[] = {, , , , , , , , , , , };
static const char leap_mon_list[] = {, , , , , , , , , , , }; /*******************************************************************************
* Function Name : fun_mktime
* Description : 时间转为时间戳
* Input :
* Output :
* Other :
* Date : 2016.11.14
*******************************************************************************/
int32_t fun_mktime(struct tm *pT)
{
const char *pDays = NULL;
int32_t tmp = ;
int16_t i = ; //计算总共有多少个闰年
tmp = (pT->tm_year / - pT->tm_year / + pT->tm_year / ) - ( / - / + / ); //如果当年是闰年,需要减去当年的闰年
if ((pT->tm_year % == ) && ((pT->tm_year % != ) || (pT->tm_year % == )))
{
tmp = tmp - + (pT->tm_year - ) * ;
pDays = leap_mon_list;
}
else
{
tmp = tmp + (pT->tm_year - ) * ;
pDays = mon_list;
} for (i = ; i < pT->tm_mon - ; i++)
tmp += pDays[i]; tmp = tmp + pT->tm_mday - ; tmp = tmp * + pT->tm_hour; tmp = tmp * + pT->tm_min; tmp = tmp * + pT->tm_sec; return tmp;
} /*******************************************************************************
* Function Name : fun_localtime
* Description : 时间戳转为时间
* Input : struct tm *pT: 输出的时间缓冲区 uint32_t tim:当前时间戳
* Output :
* Other :
* Date : 2016.11.14
*******************************************************************************/
void fun_localtime(struct tm *pT, int32_t tim)
{
const char *pDays = NULL; uint16_t index = ; memset(pT, , sizeof(*pT)); //year initialization
if (tim > 0x5685C180L) // 2016-1-1 0:0:0
{
pT->tm_year = ;
tim -= 0x5685C180L;
}
else if (tim > 0x4B3D3B00L) // 2010-1-1 0:0:0
{
pT->tm_year = ;
tim -= 0x4B3D3B00L;
}
else if (tim > 0x386D4380L) // 2000-1-1 0:0:0
{
pT->tm_year = ;
tim -= 0x386D4380L;
}
else
{
pT->tm_year = ;
} //now have year
while (tim >= 366L * * * )
{
if ((pT->tm_year % == ) && ((pT->tm_year % != ) || (pT->tm_year % == )))
tim -= 366L * * * ;
else
tim -= 365L * * * ; pT->tm_year++;
} // then 365 * 24 * 60 * 60 < tim < 366 * 24 * 60 * 60
if (!(((pT->tm_year % == ) && ((pT->tm_year % != ) || (pT->tm_year % == ))))
&& (tim > 365L * * * ))
{
tim -= 365L * * * ;
pT->tm_year++;
} // this year is a leap year?
if (((pT->tm_year % == ) && ((pT->tm_year % != ) || (pT->tm_year % == ))))
pDays = leap_mon_list;
else
pDays = mon_list; pT->tm_mon = ;
// now have mon
while (tim > pDays[index] * 24L * * )
{
tim -= pDays[index] * 24L * * ;
index++;
pT->tm_mon++;
} // now have days
pT->tm_mday = tim / (24L * * ) + ;
tim = tim % (24L * * ); // now have hour
pT->tm_hour = tim / ( * );
tim = tim % ( * ); // now have min
pT->tm_min = tim / ;
tim = tim % ; pT->tm_sec = tim;
} int main (void *parg)
{
struct tm *pT = {};
time_t timep = ;
uint32_t cur_tim = ; time(&timep); pT = localtime(&timep); printf("linux time \t= %d\n", (int32_t)timep);
pT->tm_year += ;
pT->tm_mon += ;
printf("fun_mktime \t= %d\n", cur_tim = (uint32_t)fun_mktime(pT)); printf("localtime \t= %d-%d-%d %d:%d:%d\n", pT->tm_year, pT->tm_mon, pT->tm_mday, pT->tm_hour, pT->tm_min, pT->tm_sec);
memset(pT, , sizeof(*pT));
fun_localtime(pT, cur_tim);
printf("fun_localtime \t= %d-%d-%d %d:%d:%d\n", pT->tm_year, pT->tm_mon, pT->tm_mday, pT->tm_hour, pT->tm_min, pT->tm_sec);
return ;
}
测试结果:
linux time =
fun_mktime =
localtime = -- ::
fun_localtime = -- ::
linux time 是库函数mktime计算结果,因为进行了时区处理,所以与fun_mktime计算出来刚好是8 * 3600 秒的差值
此函数在C51下进行过测试,符合要求。
8位单片机可用的 mktime localtime函数的更多相关文章
- freescale 16位单片机的地址映射
以MC9S12XS128MAL为例,其实DG128之类的类似.如图一,128代表的是单片机中的FLASH大小为128K Byte,同理64代表的是单片机中的FLASH大小为64 K Byte,256代 ...
- 为 32 位单片机设计的脚本语言 Berry
Berry是一款一款为32位单片机设计的脚本语言.Berry解释器使用C89标准实现,该语言可以在RAM或ROM很小的设备上运行. 尽管Berry的体积很小,但是它也支持class以及闭包等功能,使得 ...
- 8位、16位、32位单片机中的“XX位”指什么?
32位单片机的32位是指单片机的“字长”,也就是一次运算中参与运算的数据长度,这个位是指二进制位. 如果总线宽度与CPU一次处理的数据宽度相同,则这个宽度就是所说的单片机位数. 如果总线宽度与CPU一 ...
- localtime函数和strftime函数
localtime函数 功能: 把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为本地时间,而gmtime函数转换后的时间没有经过时区变换,是UTC时间 . 用法: #include & ...
- Linux C 中获取local日期和时间 time()&localtime()函数
1. time() 函数 /* time - 获取计算机系统当前的日历时间(Calender Time) * 处理日期时间的函数都是以本函数的返回值为基础进行运算 * * 函数原型: * #incl ...
- PHP localtime() 函数
------------恢复内容开始------------ 实例 以一个数值数组和一个关联数组的形式输出本地时间: <?phpprint_r(localtime());echo "& ...
- C语言的setlocale和localtime函数(C++也可用)
Example 1234567891011121314151617181920212223242526272829303132 /* setlocale example */ #include < ...
- C获取本地时间的localtime函数
最近有朋友问如下问题: #include <stdio.h>#include <stdlib.h>#include <iconv.h>#include <ti ...
- php strtotime,mktime,DateTime函数处理时间累加问题
时间戳(年月日时分秒) 使用strtotime函数,结合+1 month,-1 month,next month,last month的时候会出现一些问题. demo示例: //时间"20 ...
随机推荐
- Gold Balanced Lineup - poj 3274 (hash)
这题,看到别人的解题报告做出来的,分析: 大概意思就是: 数组sum[i][j]表示从第1到第i头cow属性j的出现次数. 所以题目要求等价为: 求满足 sum[i][0]-sum[j][0]=sum ...
- Python gevent学习笔记
gevent是Python的一个用于网络IO的函数库,其中应用到了 coroutine(协同程序) 的思想.首先来了解下目前网络框架的几种基本的网络I/O模型: 阻塞式单线程:这是最基本的I/O模型, ...
- tomcat NIO配置
1.tomcat NIO配置 今天在查看日志时发现tomcat的Socket连接方式为bio,于是我想既然有bio那肯定有nio.果然,一查就发现tomcat在6.0之后就可以配置nio的方式.nio ...
- spring cloud 订单调用用户
下面实现一个订单调用用户实现例子,使用技术只要是spring,为以后操作负载打基础.(基于昨天别人问我的基础上做了实例供大家参考) 1.用户工程截图 : 2.用户工程启动类 3.用户工程控制类 4. ...
- 【BZOJ1923】[Sdoi2010]外星千足虫 高斯消元
[BZOJ1923][Sdoi2010]外星千足虫 Description Input 第一行是两个正整数 N, M. 接下来 M行,按顺序给出 Charles 这M次使用“点足机”的统计结果.每行 ...
- 《从零开始学Swift》学习笔记(Day 44)——重写属性
原创文章,欢迎转载.转载请注明:关东升的博客 重写实例属性 我们可以在子类中重写从父类继承来的属性,属性有实例属性和静态属性之分,他们在具体实现也是不同的. 实例属性的重写一方面可以重写getter和 ...
- Python学习笔记(三)windows下安装theano
2016.6.28补充: 不论是实验室的电脑还是我的笔记本,只要是windows下,theano.test()都是不通过的.虽然能使用一些theano中的函数,但是我感觉很不好. 所以还是转Ubunt ...
- ECMAScript6重复字符串方法repeat()
repeat() 1.定义 repeat()方法返回一个新字符串,新字符串将对象字符串重复指定次数. 2.语法 str.repeat(count) count:表示新构造的字符串把原字符串重复的次数, ...
- Maven学习笔记【1】 -- Maven简介
本文主要讲Maven的基础知识. 一 什么是Maven? Maven是一个项目管理工具.Maven提供了一个项目对象模型(POM)文件的新概念来管理项目的构建,相关性和文档.最强大的功能就是能够自动下 ...
- Python 获取文件路径及文件目录
import os print (os.path.dirname(__file__)) print (os.path.abspath(__file__)) print (os.path.abspath ...