单片机时间戳转换

2023-11-18

看了网上的例程,大多繁琐,直接利用time.h里的库函数进行转换即可

#include <time.h>
//本地时间生成时间戳函数

uint32_t TimeToTimeStamp(void) 
{
    struct tm stm;
    memset(&stm,0,sizeof(stm));
    stm.tm_year = TimeData.year - 1900; //RTC_Year 2023,but tm_year since 1900
    stm.tm_mon = TimeData.month - 1; //RTC_Month rang 1-12,but tm_mon rang 0-11
    stm.tm_mday = TimeData.day;      //RTC_Date rang 1-31 and tm_mday rang 1-31
    stm.tm_hour = TimeData.hour - 8; //RTC_Hours rang 0-23 and tm_hour rang 0-23
    stm.tm_min = TimeData.minute;    //RTC_Minutes rang 0-59 and tm_min rang 0-59
    stm.tm_sec = TimeData.second;
    return mktime(&stm);  
}

//时间戳解析成本地时间函数
void TimeStampToTime(uint32_t TimeStamp)
{
     struct tm *stm = NULL;
    stm = localtime(&TimeStamp);
     TimeDataRead.year = stm->tm_year + 1900;
     TimeDataRead.month = stm->tm_mon + 1;
     TimeDataRead.day = stm->tm_mday;
     TimeDataRead.hour = stm->tm_hour + 8;
     TimeDataRead.minute = stm->tm_min;
     TimeDataRead.second = stm->tm_sec;
}
/* time.h: ANSI 'C' (X3J11 Oct 88) library header, section 4.12 */
/* Copyright (C) Codemist Ltd., 1988-1993.                      */
/* Copyright 1991-1993 ARM Limited. All rights reserved.        */
/* version 0.03 */

/*
 * time.h declares two macros, four types and several functions for
 * manipulating time. Many functions deal with a calendar time that
 * represents the current date (according to the Gregorian
 * calendar) and time. Some functions deal with local time, which
 * is the calendar time expressed for some specific time zone, and
 * with Daylight Savings Time, which is a temporary change in the
 * algorithm for determining local time.
 */

/*
 * RCS $Revision$
 * Checkin $Date$
 * Revising $Author: drodgman $
 */

#ifndef __time_h
#define __time_h
#define __ARMCLIB_VERSION 5060037

#define _ARMABI __declspec(__nothrow)
#define _ARMABI_PURE __declspec(__nothrow) __attribute__((const))

  #ifndef __TIME_DECLS
  #define __TIME_DECLS

    #undef __CLIBNS

    #ifdef __cplusplus
      namespace std {
          #define __CLIBNS std::
          extern "C" {
    #else
      #define __CLIBNS
    #endif  /* __cplusplus */

#if defined(__cplusplus) || !defined(__STRICT_ANSI__)
 /* unconditional in C++ and non-strict C for consistency of debug info */
  #if __sizeof_ptr == 8
    typedef unsigned long size_t;   /* see <stddef.h> */
  #else
    typedef unsigned int size_t;   /* see <stddef.h> */
  #endif
#elif !defined(__size_t)
  #define __size_t 1
  #if __sizeof_ptr == 8
    typedef unsigned long size_t;   /* see <stddef.h> */
  #else
    typedef unsigned int size_t;   /* see <stddef.h> */
  #endif
#endif

#undef NULL
#define NULL 0                   /* see <stddef.h> */

    /* CLOCKS_PER_SEC: the number per second of the value returned by the
     * clock function. */
#if _AEABI_PORTABILITY_LEVEL != 0 || (!defined _AEABI_PORTABILITY_LEVEL && __DEFAULT_AEABI_PORTABILITY_LEVEL != 0)
  extern const int __aeabi_CLOCKS_PER_SEC;
  #define CLOCKS_PER_SEC (__aeabi_CLOCKS_PER_SEC)
#else
  #ifdef __CLK_TCK
    #define CLOCKS_PER_SEC  __CLK_TCK
  #else
    #define CLOCKS_PER_SEC  100
  #endif
#endif

#ifndef __STRICT_ANSI__
  #define CLK_TCK CLOCKS_PER_SEC
#endif

typedef unsigned int clock_t;    /* cpu time type */
typedef unsigned int time_t;     /* date/time in unix secs past 1-Jan-70 */

#pragma push
#pragma anon_unions

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 */
    union {       /* ABI-required extra fields, in a variety of types */
        struct {
            int __extra_1, __extra_2;
        };
        struct {
            long __extra_1_long, __extra_2_long;
        };
        struct {
            char *__extra_1_cptr, *__extra_2_cptr;
        };
        struct {
            void *__extra_1_vptr, *__extra_2_vptr;
        };
    };
};

#pragma pop

   /* struct tm holds the components of a calendar time, called the broken-down
    * time. The value of tm_isdst is positive if Daylight Savings Time is in
    * effect, zero if Daylight Savings Time is not in effect, and negative if
    * the information is not available.
    */

extern _ARMABI clock_t clock(void);
   /* determines the processor time used.
    * Returns: the implementation's best approximation to the processor time
    *          used by the program since program invocation. The time in
    *          seconds is the value returned divided by the value of the macro
    *          CLK_TCK. The value (clock_t)-1 is returned if the processor time
    *          used is not available.
    */
extern _ARMABI double difftime(time_t /*time1*/, time_t /*time0*/);
   /*
    * computes the difference between two calendar times: time1 - time0.
    * Returns: the difference expressed in seconds as a double.
    */
extern _ARMABI time_t mktime(struct tm * /*timeptr*/) __attribute__((__nonnull__(1)));
   /*
    * converts the broken-down time, expressed as local time, in the structure
    * pointed to by timeptr into a calendar time value with the same encoding
    * as that of the values returned by the time function. The original values
    * of the tm_wday and tm_yday components of the structure are ignored, and
    * the original values of the other components are not restricted to the
    * ranges indicated above. On successful completion, the values of the
    * tm_wday and tm_yday structure components are set appropriately, and the
    * other components are set to represent the specified calendar time, but
    * with their values forced to the ranges indicated above; the final value
    * of tm_mday is not set until tm_mon and tm_year are determined.
    * Returns: the specified calendar time encoded as a value of type time_t.
    *          If the calendar time cannot be represented, the function returns
    *          the value (time_t)-1.
    */
extern _ARMABI time_t time(time_t * /*timer*/);
   /*
    * determines the current calendar time. The encoding of the value is
    * unspecified.
    * Returns: the implementations best approximation to the current calendar
    *          time. The value (time_t)-1 is returned if the calendar time is
    *          not available. If timer is not a null pointer, the return value
    *          is also assigned to the object it points to.
    */

extern _ARMABI char *asctime(const struct tm * /*timeptr*/) __attribute__((__nonnull__(1)));
extern _ARMABI char *_asctime_r(const struct tm * /*timeptr*/,
                                char * __restrict /*buf*/) __attribute__((__nonnull__(1,2)));
#ifndef __STRICT_ANSI__
extern _ARMABI char *asctime_r(const struct tm * /*timeptr*/,
                               char * __restrict /*buf*/) __attribute__((__nonnull__(1,2)));
#endif
   /*
    * converts the broken-down time in the structure pointed to by timeptr into
    * a string in the form "Sun Sep 16 01:03:52 1973\n\0".
    * Returns: a pointer to the string containing the date and time.
    */
extern _ARMABI char *ctime(const time_t * /*timer*/) __attribute__((__nonnull__(1)));
   /*
    * converts the calendar time pointed to by timer to local time in the form
    * of a string. It is equivalent to asctime(localtime(timer));
    * Returns: the pointer returned by the asctime function with that
    *          broken-down time as argument.
    */
extern _ARMABI struct tm *gmtime(const time_t * /*timer*/) __attribute__((__nonnull__(1)));
   /*
    * converts the calendar time pointed to by timer into a broken-down time,
    * expressed as Greenwich Mean Time (GMT).
    * Returns: a pointer to that object or a null pointer if GMT not available.
    */
extern _ARMABI struct tm *localtime(const time_t * /*timer*/) __attribute__((__nonnull__(1)));
extern _ARMABI struct tm *_localtime_r(const time_t * __restrict /*timer*/,
                                       struct tm * __restrict /*result*/) __attribute__((__nonnull__(1,2)));
#ifndef __STRICT_ANSI__
extern _ARMABI struct tm *localtime_r(const time_t * __restrict /*timer*/,
                                      struct tm * __restrict /*result*/) __attribute__((__nonnull__(1,2)));
#endif
   /*
    * converts the calendar time pointed to by timer into a broken-down time,
    * expressed a local time.
    * Returns: a pointer to that object.
    */
extern _ARMABI size_t strftime(char * __restrict /*s*/, size_t /*maxsize*/,
                       const char * __restrict /*format*/,
                       const struct tm * __restrict /*timeptr*/) __attribute__((__nonnull__(1,3,4)));
   /*
    * places characters into the array pointed to by s as controlled by the
    * string pointed to by format. The format string consists of zero or more
    * directives and ordinary characters. A directive consists of a % character
    * followed by a character that determines the directive's behaviour. All
    * ordinary characters (including the terminating null character) are copied
    * unchanged into the array. No more than maxsize characters are placed into
    * the array. Each directive is replaced by appropriate characters  as
    * described in the following list. The appropriate characters are
    * determined by the LC_TIME category of the current locale and by the
    * values contained in the structure pointed to by timeptr.
    * %a is replaced by the locale's abbreviated weekday name.
    * %A is replaced by the locale's full weekday name.
    * %b is replaced by the locale's abbreviated month name.
    * %B is replaced by the locale's full month name.
    * %c is replaced by the locale's appropriate date and time representation.
    * %d is replaced by the day of the month as a decimal number (01-31).
    * %H is replaced by the hour (24-hour clock) as a decimal number (00-23).
    * %I is replaced by the hour (12-hour clock) as a decimal number (01-12).
    * %j is replaced by the day of the year as a decimal number (001-366).
    * %m is replaced by the month as a decimal number (01-12).
    * %M is replaced by the minute as a decimal number (00-59).
    * %p is replaced by the locale's equivalent of either AM or PM designations
    *       associated with a 12-hour clock.
    * %S is replaced by the second as a decimal number (00-61).
    * %U is replaced by the week number of the year (Sunday as the first day of
    *       week 1) as a decimal number (00-53).
    * %w is replaced by the weekday as a decimal number (0(Sunday) - 6).
    * %W is replaced by the week number of the year (Monday as the first day of
    *       week 1) as a decimal number (00-53).
    * %x is replaced by the locale's appropriate date representation.
    * %X is replaced by the locale's appropriate time representation.
    * %y is replaced by the year without century as a decimal number (00-99).
    * %Y is replaced by the year with century as a decimal number.
    * %Z is replaced by the timezone name or abbreviation, or by no characters
    *       if no time zone is determinable.
    * %% is replaced by %.
    * If a directive is not one of the above, the behaviour is undefined.
    * Returns: If the total number of resulting characters including the
    *          terminating null character is not more than maxsize, the
    *          strftime function returns the number of characters placed into
    *          the array pointed to by s not including the terminating null
    *          character. otherwise, zero is returned and the contents of the
    *          array are indeterminate.
    */

    #ifdef __cplusplus
         }  /* extern "C" */
      }  /* namespace std */
    #endif /* __cplusplus */
  #endif /* __TIME_DECLS */

  #if _AEABI_PORTABILITY_LEVEL != 0 && !defined _AEABI_PORTABLE
    #define _AEABI_PORTABLE
  #endif

  #ifdef __cplusplus
    #ifndef __TIME_NO_EXPORTS
      using ::std::clock_t;
      using ::std::time_t;
      using ::std::tm;
      using ::std::tm;
      using ::std::clock;
      using ::std::difftime;
      using ::std::mktime;
      using ::std::time;
      using ::std::asctime;
      using ::std::_asctime_r;
#ifndef __STRICT_ANSI__
      using ::std::asctime_r;
#endif
      using ::std::ctime;
      using ::std::gmtime;
      using ::std::localtime;
      using ::std::_localtime_r;
#ifndef __STRICT_ANSI__
      using ::std::localtime_r;
#endif
      using ::std::strftime;
      using ::std::size_t;
    #endif /* __TIME_NO_EXPORTS */
  #endif /* __cplusplus */

#endif

/* end of time.h */

试了下,单片机执行

//时间戳解析成本地时间函数

void TimeStampToTime(uint32_t TimeStamp)

会卡死,用这位大哥写的函数没问题

https://blog.csdn.net/Stack_/article/details/105916302

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

单片机时间戳转换 的相关文章

  • springboot 配置文件中属性变量引用方式@@解析

    这种属性应用方式是field name field value 两个 符号是springboot为替代 属性占位符产生 原因是 会被maven处理 所以应该是起不到引用变量的作用 方式可以引用springboot非默认配置文件 即其他配置文

随机推荐

  • 【01】OpenCV模块架构介绍+示例程序演示

    本系列文章是基于Windows下 结合Visual Studio2017和OpenCV4 7进行编写 使用C 代码进行演示 目录 1 OpenCV模块架构 2 示例程序效果展示 2 0创建工程 2 1边缘检测示例edge cpp 2 2K聚
  • 求学在卡梅

    卡内基梅隆大学坐落在美国宾夕法尼亚州匹兹堡市 对于卡梅 我同样慕名已久 清华大学的计算机学科在国内名列前茅 而卡内基梅隆大学计算机学院下属计算机 机器人和语言工程等几个系 和麻省理工 斯坦福 伯克利一起在计算机领域排名第一 1999年8月
  • 纯新手入门机器/深度学习自学指南(附一个月速成方案)

    原作 Masum Hasan问耕 编译整理量子位 出品 公众号 QbitAI 怎么入门机器 深度学习 回答这个问题 最先要考虑的问题是 你有多少时间 准备用三个月入门 和想要一个月速成 肯定是截然不同的路径 当然我建议大家稳扎稳打 至少可以
  • 如何解决K8S节点显示NotReady

    文章目录 kubernetes节点断电重启 kubernetes节点断电重启 背景 运行的好好的k8s集群 某天断电 发现一个节点炸了 显示NotReady kubectl get nodes 那么如何查找问题呢 我们用它 journalc
  • 如何在移动端猎豹浏览器中设置代理IP

    手机浏览器作为一款功能强大且广受欢迎的移动浏览器 提供了丰富的功能和个性化选项 其中包括设置动态ip地址的功能 通过设置动态ip地址 您可以改变您的网络访问路径 保护个人隐私 或者访问被地理限制的内容 接下来 我将为您介绍在手机浏览器中如何
  • Count the string【KMP】

    It is well known that AekdyCoin is good at string problems as well as number theory problems When given a string s we ca
  • 深度学习里面正则化的理解

    一 正则化的概念与用处 正则化 也叫规范化 在神经网络里主要是对代价函数高次项添加一些惩罚 防止其过拟合 相当于对某些特征的权重施加惩罚 降低其影响权重 防止过拟合 欠拟合时需要去掉正则化 因为本来就特征不足 再惩罚一下就更没用了 正则化目
  • JCenter() 与 mavenCenter() 的区别

    网络上有两种Maven仓库 分别是mavenCenter 和jCenter 1 Maven Central 则是由sonatype org维护的Maven仓库 http jcenter bintray com 2 jcenter是一个由 b
  • ARM开发——搭建嵌入式开发环境

    ckermit串口工具的下载 sudo apt get install ckermit 如果我们有新的串口连接上 我们可以通过查看 dev 来看连接的数据 ckermit 配置文件的配置 kermrc set line dev ttyUSB
  • jemter接口测试+断言+参数化+接口关联实战教程

    此次实例准备 jmeter实战接口申请地址 https www tianapi com 实例信息 请求10次身份证归属地查询接口 提取响应数据的星座信息作为请求参数 给星座运势查询接口使用 即接口之间关联调用 下面是两个接口的基本信息 A
  • 【Unity】一个场景跳转到多个场景的解决方案

    在某些情况下 比如我们需要设计一个探索类游戏 那么一个场景可以根据选择的不同 跳转至多个场景 甚至能够回跳场景 那么在这种情况下 我们该如何进行多场景的精确跳转呢 思路 假设角色走到某一扇门时 场景将进行切换 如果场景切换是非线性的 那么就
  • python入门选择题

    下列哪个是Python的注释符号 A B C D 答案 B 解析 在Python中 使用 来进行注释 以 开头的行也被认为是注释 在Python中 如何将两个整数相除并得到一个浮点数 A 使用 运算符 B 使用 运算符 C 使用 运算符 D
  • office2021安装教程

    需要用到的工具 office tool plus https otp landian vip zh cn 进到官网点立即下载 下载 包含框架 推荐 下载好解压 运行officetool plus 选择部署 在这里可以看到电脑已存在的offi
  • Spring依赖注入

    一 什么是依赖注入 DI Dependency Injection 依赖注入是指在 Spring IOC 容器创建对象的过程中 将所依赖的对象通过配置进行注入 我们可以通过依赖注入的方式来降低对象间的耦合度 在软件工程中 对象之间的耦合度就
  • 三维重建-opencv实现sfm

    注意 本文中的代码必须使用OpenCV3 0或以上版本进行编译 因为很多函数是3 0以后才加入的 目录 SfM介绍 小孔相机模型 坐标系 内参矩阵 外参矩阵 相机的标定 SfM介绍 SfM的全称为Structure from Motion
  • Java 利用hutool工具实现导出excel并合并单元格

    Java 利用hutool工具实现导出excel并合并单元格 controller层调用service 就一个核心方法 没错就下面这个代码就能实现了 前提是项目里面要引用hutool包 把我这个复制到项目里面然后改掉字段应该能直接跑起来的
  • SQL Server统计数据库表空间大小和数据量

    SQL Server统计数据库表空间大小和数据量 在大数据环境中 对于SQL Server数据库的管理和优化是非常重要的 其中一个关键任务是统计数据库中各个表的空间大小和数据量 通过了解每个表所占用的存储空间 可以进行容量规划 性能优化和资
  • 虚拟机上的Ubuntu开机显示“无法应用原保存的显示器配置”

    如图 解决方法 删除monitors xml 文件 rm config monitors xml
  • pdf模板,java替换变量

    开发十年 就只剩下这套Java开发体系了 gt gt gt 1 创建pdf 现在word中创建模板 输出为pdf文件2 pdf 2 java项目引入依赖
  • 单片机时间戳转换

    看了网上的例程 大多繁琐 直接利用time h里的库函数进行转换即可 include