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 ...
随机推荐
- servlet部署到tomcat中
引用:http://blog.csdn.net/shuidao/article/details/1738059 配置,部署 servlet: 1. 在tomcat的安装目录下 找到webapps 文件 ...
- Unity官方发布热更新方案性能对照
孙广东 2016.3.11 Unity应用的iOS热更新 作者:丁治宇 Unity TechnologiesChina Agenda • 什么是热更新 • 为何要热更新 • 怎样在iOS 上对 ...
- 微信小程序 一些要点
微信小程序,weixin,关于微信小程序,那些开发文档没有告诉你的-微信小程序开发资源-微信开发者平台,微信开发者社区,微信小程序开发者社区 Discuz! Team and Comsenz UI T ...
- asp.net *.ashx类型的文件使用说明
你想创建一个ASP.NET文件,它不是aspx文件,它能动态的返回一个图片.XML文件或其他非HTML文件. 解决方案:使用ashx文件. 1. 使用ASHX handlers 首先,我们要回顾要使用 ...
- c/c++输入处理,制定变量参数和值
void usage(char* s){ fprintf(stderr, "\n"); fprintf(stderr, "%s -s <source file> ...
- ios - UINavigationBar添加背景图片的几种简单思路
UITabBarController下面常常需要为多个ViewController设置导航栏样式,总结了一下遇到过的为UINavigationBar添加背景图片的几种简单思路 以设置背景图片为例: 第 ...
- iOS 实现从后台切换到前台-复制分享宝贝内容,打开淘宝APP,自动弹出宝贝提示信息
- (void)applicationDidBecomeActive:(UIApplication *)application { NSLog(@"\n ===> 程序重新激活 !&q ...
- SQL.Cookbook 读书笔记4 插入更新和删除
第四章 插入更新和删除 4.1 插入数据 ,'PROGRA','NEW YOURK'); 4.2 从一个表向另一个表中复制 insert into dept_east(deptno,dname,loc ...
- NetWorkUtil
import java.io.IOException; import java.io.UnsupportedEncodingException; import org.apache.http.Http ...
- MVC架构模式概述
MVC MVC概述: Model–view–controller (MVC) is a software architectural pattern for implementing user int ...