小言_互联网的博客

嵌入式开发-C语言实现localtime()和mktime()

654人阅读  评论(0)

一、前言

        在嵌入式的开发中很多时候会用到时间戳,会用到time.h中的localtime()和mktime(),这两个函数像STM32这种MCU中运行效率并不高,不使用标准库的情况下可以自己实现。

二、代码

utc_time.h

#ifndef TIME_H
#define TIME_H

#ifdef __cplusplus
extern "C"
{
#endif

#include <stdint.h>

#define	IsLeapYear(yr)	(!((yr) % 400) || (((yr) % 100) && !((yr) % 4)))
// number of seconds since 0 hrs, 0 minutes, 0 seconds, on the
// 1st of January 2000 UTC
typedef uint32_t utc_time;

// To be used with
struct utc_time_t
{
  uint8_t seconds;  // 0-59
  uint8_t minutes;  // 0-59
  uint8_t hour;     // 0-23
  uint8_t day;      // 0-30
  uint8_t month;    // 0-11
  uint16_t year;    // 2000+
} ;

/*********************************************************************
 * GLOBAL VARIABLES
 */

/*********************************************************************
 * FUNCTIONS
 */

  extern void convert_utc_time( struct utc_time_t *tm, utc_time sec_time );

  /*
   * Converts UTCTimeStruct to UTCTime (seconds since 00:00:00 01/01/2000)
   *
   * tm - pointer to UTC time struct
   */
  extern utc_time convert_utc_secs( struct utc_time_t *tm );

#ifdef __cplusplus
}
#endif

#endif 

utc_time.c


#include "utc_time.h"

#define	YearLength(yr)	(IsLeapYear(yr) ? 366 : 365)

/*********************************************************************
 * CONSTANTS
 */

// (MAXCALCTICKS * 5) + (max remainder) must be <= (uint16 max),
// so: (13105 * 5) + 7 <= 65535
#define MAXCALCTICKS  ((uint16)(13105))

#define	BEGYEAR	        2000     // UTC started at 00:00:00 January 1, 2000

#define	DAY             86400UL  // 24 hours * 60 minutes * 60 seconds


/*********************************************************************
 * EXTERNAL VARIABLES
 */


static uint8_t monthLength( uint8_t lpyr, uint8_t mon )
{
  uint8_t days = 31;

  if ( mon == 1 ) // feb
  {
    days = ( 28 + lpyr );
  }
  else
  {
    if ( mon > 6 ) // aug-dec
    {
      mon--;
    }

    if ( mon & 1 )
    {
      days = 30;
    }
  }

  return ( days );
}


void convert_utc_time( struct utc_time_t *tm, utc_time sec_time )
{
  // calculate the time less than a day - hours, minutes, seconds
  {
    uint32_t day = sec_time % DAY;
    tm->seconds = day % 60UL;
    tm->minutes = (day % 3600UL) / 60UL;
    tm->hour = day / 3600UL;
  }
  // Fill in the calendar - day, month, year
  {
    uint16_t numDays = sec_time / DAY;
    tm->year = BEGYEAR;
    while ( numDays >= YearLength( tm->year ) )
    {
      numDays -= YearLength( tm->year );
      tm->year++;
    }

    tm->month = 0;
    while ( numDays >= monthLength( IsLeapYear( tm->year ), tm->month ) )
    {
      numDays -= monthLength( IsLeapYear( tm->year ), tm->month );
      tm->month++;
    }

    tm->day = numDays;
  }
}




utc_time convert_utc_secs( struct utc_time_t *tm )
{
  uint32_t seconds;

  /* Seconds for the partial day */
  seconds = (((tm->hour * 60UL) + tm->minutes) * 60UL) + tm->seconds;

  /* Account for previous complete days */
  {
    /* Start with complete days in current month */
    uint16_t days = tm->day;

    /* Next, complete months in current year */
    {
      int8_t month = tm->month;
      while ( --month >= 0 )
      {
        days += monthLength( IsLeapYear( tm->year ), month );
      }
    }

    /* Next, complete years before current year */
    {
      uint16_t year = tm->year;
      while ( --year >= BEGYEAR )
      {
        days += YearLength( year );
      }
    }

    /* Add total seconds before partial day */
    seconds += (days * DAY);
  }

  return ( seconds );
}

三、运行时间测试

实际测试可见,自写函数比标准库效率高很多。


转载:https://blog.csdn.net/ziqi5543/article/details/101768096
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场