nrf51822 ---时间换成时间戳



单片机中时期如何转换成时间戳
 Unix时间戳(Unix timestamp),或称Unix时间(Unix time)、POSIX时间(POSIX time),是一种时间表示方式,定义为从 格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至 现在的总秒数。Unix时间戳不仅被使用在Unix系统、类Unix系统中(比如 Linux系统),也在许多其他操作系统中被广泛采用。
 转换程序如下:

//**************************************************************************
/********************************************************************************
 * @file     calender.h
 * @author   Depth
    * @version  V1.0.0
    * @date     12-May-2014
 * @Emial    [email protected]
 * @brief    head file for calender.c
 ******************************************************************************/
#ifndef CALENER_H
#define CALENER_H

#ifdef __cplusplus
extern "C"
{
#endif

#include <stdint.h>
 
/*********************************************************************
 * MACROS
 */
 
#define IsLeapYear(yr) (!((yr) % 400) || (((yr) % 100) && !((yr) % 4)))
/*********************************************************************
 * TYPEDEFS
 */
 // To be used with
typedef struct
{
  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+
} UTCTimeStruct;

// number of seconds since 0 hrs, 0 minutes, 0 seconds, on the
// 1st of January 2000 UTC
typedef uint32_t UTCTime;
extern UTCTime TimeSeconds;
extern UTCTimeStruct Time_iOS;
 
/*********************************************************************
 * FUNCTIONS
 */
  /*
   * Updates the OSAL clock and Timers from the MAC 320us timer tick.
   */
  extern void TimeUpdate( void );
  /*
   * Set the new time.  This will only set the seconds portion
   * of time and doesn't change the factional second counter.
   *     newTime - number of seconds since 0 hrs, 0 minutes,
   *               0 seconds, on the 1st of January 2000 UTC
   */
  extern void Set_Clock( UTCTime newTime );
  /*
   * Gets the current time.  This will only return the seconds
   * portion of time and doesn't include the factional second counter.
   *     returns: number of seconds since 0 hrs, 0 minutes,
   *              0 seconds, on the 1st of January 2000 UTC
   */
  extern UTCTime Get_Clock( void );
  /*
   * Converts UTCTime to UTCTimeStruct
   *
   * secTime - number of seconds since 0 hrs, 0 minutes,
   *          0 seconds, on the 1st of January 2000 UTC
   * tm - pointer to breakdown struct
   */
  extern void ConvertUTCTime( UTCTimeStruct *tm, UTCTime secTime );
  /*
   * Converts UTCTimeStruct to UTCTime (seconds since 00:00:00 01/01/2000)
   *
   * tm - pointer to UTC time struct
   */
  extern UTCTime ConvertUTCSecs( UTCTimeStruct *tm );
  /*
   * Update/Adjust the osal clock and timers
   * Msec - elapsed time in milli seconds 
   */
  extern void AdjustTimer( uint32_t Msec );
/*********************************************************************
*********************************************************************/
#ifdef __cplusplus
}
#endif
#endif /* OSAL_CLOCK_H */

/********************************************************************************
 * @file     calender.c
 * @author   Depth
    * @version  V1.0.0
    * @date     12-May-2014
 * @Emial    [email protected]
 * @brief    calender module based on app timer
******************************************************************************/
#include "calender.h"

/*********************************************************************
 * MACROS
 */
#define YearLength(yr) (IsLeapYear(yr) ? 366 : 365)
//ÈÕÀú±äÁ¿
UTCTimeStruct Time_iOS = {14,13,12,11,1,2015};
UTCTimeStruct Time_Current;
/*********************************************************************
 * CONSTANTS
 */
// (MAXCALCTICKS * 5) + (max remainder) must be <= (uint16_t max),
// so: (13105 * 5) + 7 <= 65535
#define MAXCALCTICKS  ((uint16_t)(13105))
#define BEGYEAR         1970     // UTC started at 00:00:00 January 1, 2000
#define DAY             86400UL  // 24 hours * 60 minutes * 60 seconds

/*********************************************************************
 * EXTERNAL FUNCTIONS
 */
extern uint16_t ll_McuPrecisionCount(void);
/*********************************************************************
 * LOCAL VARIABLES
 */
//static uint16_t previousLLTimerTick = 0;
//static uint16_t remUsTicks = 0;
//static uint16_t timeMSec = 0;
// number of seconds since   2010.1.1.  00:00:00
UTCTime TimeSeconds = 0;
/*********************************************************************
 * LOCAL FUNCTION PROTOTYPES
 */
static uint8_t monthLength( uint8_t lpyr, uint8_t mon );
/*********************************************************************
 * FUNCTIONS
 */
/*********************************************************************
 * @fn      Set_Clock
 *
 * @brief   Set the new time.  This will only set the seconds portion
 *          of time and doesn't change the factional second counter.
 *
 * @param   newTime - number of seconds since 0 hrs, 0 minutes,
 *                    0 seconds, on the 1st of January 2000 UTC
 *
 * @return  none
 */
void Set_Clock( UTCTime newTime )
{
  TimeSeconds = newTime;
}
/*********************************************************************
 * @fn      Get_Clock
 *
 * @brief   Gets the current time.  This will only return the seconds
 *          portion of time and doesn't include the factional second
 *          counter.
 *
 * @param   none
 *
 * @return  number of seconds since 0 hrs, 0 minutes, 0 seconds,
 *          on the 1st of January 2000 UTC
 */
UTCTime Get_Clock( void )
{
  return ( TimeSeconds );
}
/*********************************************************************
 * @fn      ConvertUTCTime
 *
 * @brief   Converts UTCTime to UTCTimeStruct
 *
 * @param   Êä³ö tm - pointer to breakdown struct
 *
 * @param   ÊäÈë secTime - number of seconds since 0 hrs, 0 minutes,
 *          0 seconds, on the 1st of January 2000 UTC
 *
 * @return  none
 */
void ConvertUTCTime( UTCTimeStruct *tm, UTCTime secTime )
{
  // calculate the time less than a day - hours, minutes, seconds
  {
    uint32_t day = secTime % DAY;  // Çó³ö×îºóÒ»ÌìµÄÊ£ÓàÃëÊý£¬£¨³ýÈ¥ÕûÊýµÄÌìÊýÖ®ºóµÄÊ£ÓàÃëÊý£©
    tm->seconds = day % 60UL;      // Çó³ö×îºóÒ»·ÖÖÓµÄÊ£ÓàÃëÊý £¨×îºóÒ»ÌìµÄÊ£ÓàÃëÊý¶Ô60ÇóÓࣩ
    tm->minutes = (day % 3600UL) / 60UL;  //Çó³ö ×îºóһСʱʣÓàµÄÕû·ÖÖÓÊý
    tm->hour = day / 3600UL;       //Çó×îºóÒ»ÌìµÄÕûСʱÊý
  }
  // Fill in the calendar - day, month, year
  {
    uint16_t numDays = secTime / 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->month++;
    tm->day = numDays;
  tm->day++;  //СÍõ
  }
}
/*********************************************************************
 * @fn      monthLength
 *
 * @param   lpyr - 1 for leap year, 0 if not
 *
 * @param   mon - 0 - 11 (jan - dec)
 *
 * @return  number of days in specified month
 */
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 );
}
/*********************************************************************
 * @fn      ConvertUTCSecs
 *
 * @brief   Converts a UTCTimeStruct to UTCTime
 *
 * @param   tm - pointer to provided struct
 *
 * @return  number of seconds since 00:00:00 on 01/01/2000 (UTC)
 */
UTCTime ConvertUTCSecs( UTCTimeStruct *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 - 1;//
      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 );
}

/***************************************************************************
***************************************************************************/ 
 void   main()
{

  UTCTimeStruct   Time = {14,30,16,18,8,2015};
  Secondflag  = ConvertUTCSecs(&Time) - 8*3600;  //可以得到多少s,但是因为时间戳是从8:0:0开始计数的 所以应该减去8*3600s
  ConvertUTCTime(&Time,1439885936+8*3600);  //为什么要加8*3600 因为是从1970:1:1:8:0:0开始计数的
} 



你可能感兴趣的:(nrf51822 ---时间换成时间戳)