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 ...
随机推荐
- Oracle 查询并修改
update test1 a set a.name=(select b.name from test2 b where a.id=b.id) where a.id in (select id from ...
- 【转】Silverlight全开源工作流设计器
声明 此工作流是作者自行构思和设计的被动式数据触发模式的工作流.没有遵循各种现有的工作流设计标准(如WFMC或WSFL),也没有与其他工作流通用性的接口规范.这里体现更多的是作者对工作流的使用思想,及 ...
- 下载 Microsoft SQL Server JDBC 驱动程序
JDBC 驱动程序中使用 Maven 中心 JDBC 驱动程序可以通过将其添加为依赖项在 POM.xml 文件中使用以下代码添加到 Maven 项目: XML复制 <dependency> ...
- python django -7 Git与项目
git的使用,主要包括: 本地仓库的命令 远程仓库的命令 项目需求.页面.模型类的设计,及页面的使用 Git简介 Git是目前世界上最先进的分布式版本控制系统 安装 sudo apt-get inst ...
- point-position2修改版
说明: 在共面直线测试中,由于计算误差等原因,共面条件判断不准,但计算结果依然正确. // point-position2.cpp : 定义控制台应用程序的入口点. #include "st ...
- EasyUI怎么利用onBeforeRender事件
onBeforeRender事件是view的属性,该事件发生在把ajax请求到的数据填充到表格内容中之前 将此段代码附加在DataGrid初始化后执行,即可完成在DataGrid渲染之前进行操作 // ...
- PHPStorm2017去掉参数提示 parameter name hints
JetBrains 的各种语言的 IDE 都灰常灰常好用, 个个都是神器, PHPStorm 作为PHP开发的神器也不必多说了 今天升级到 PHPStorm 2017.1 发现增加了好些新功能, 有个 ...
- 当公有云Azure拥抱Docker容器技术
本文转载至 http://3387405.blog.51cto.com/3377405/1598977 预见未来看似是一件不太可能的事情,然而现在企业科技高速发展的态势完全超乎想象. 就在几周前Inf ...
- Android UI开发第二十七篇——实现左右划出菜单
年前就想写左右滑动菜单,苦于没有时间,一直拖到现在,这篇代码实现参考了网上流行的SlidingMenu,使用的FrameLayout布局,不是扩展的HorizontalScrollView. 程序中自 ...
- IntelliJ IDEA使用手册
开发工具现在转到IDEA了,看到关于该工具很好的入门文档,于是记录一下: IntelliJ IDEA 使用教程