RTC localtime()

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
JoVM
Posts: 89
Joined: Thu Mar 12, 2015 12:13 pm

RTC localtime()

Post by JoVM »

The RTC driver for STM32F4 contains this code

Code: Select all

/**
 * @brief   Type of a structure representing an RTC date/time stamp.
 */
typedef struct {
  uint32_t      year: 8;            /**< @brief Years since 1980.           */
  uint32_t      month: 4;           /**< @brief Months 1..12.               */
  uint32_t      dstflag: 1;         /**< @brief DST correction flag.        */
  uint32_t      dayofweek: 3;       /**< @brief Day of week 1..7.           */
  uint32_t      day: 5;             /**< @brief Day of the month 1..31.     */
  uint32_t      millisecond: 27;    /**< @brief Milliseconds since midnight.*/
} RTCDateTime;


The Unix epoch started somewhere at the '70. So this isn't compatible with libc localtime()?
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: RTC localtime()

Post by Giovanni »

Hi,

That structure is custom, it requires a conversion if you need localtime.

Giovanni
JoVM
Posts: 89
Joined: Thu Mar 12, 2015 12:13 pm

Re: RTC localtime()

Post by JoVM »

Hi Giovanni,

Do you know by heart how to customise for localtime()? Other I will look into it.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: RTC localtime()

Post by Giovanni »

Hi,

I have no plans to modify that structure, you need to implement a conversion routine, it should be easily convertible to any calendar-like format.

Giovanni
JoVM
Posts: 89
Joined: Thu Mar 12, 2015 12:13 pm

Re: RTC localtime()

Post by JoVM »

Code: Select all

/**
 * @brief   Convert broken-down time structure to @p RTCDateTime.
 *
 * @param[in] timp        pointer to a broken-down time structure
 * @param[in] tv_msec     milliseconds value
 * @param[out] timespec   pointer to a @p RTCDateTime structure
 *
 * @api
 */
void rtcConvertStructTmToDateTime(const struct tm *timp,
                                  uint32_t tv_msec,
                                  RTCDateTime *timespec) {

  timespec->year      = timp->tm_year - (1980 - 1900);
  timespec->month     = timp->tm_mon + 1;
  timespec->day       = timp->tm_mday;
  timespec->dayofweek = timp->tm_wday + 1;
  if (-1 == timp->tm_isdst)
    timespec->dstflag = 0;  /* set zero if dst is unknown */
  else
    timespec->dstflag = timp->tm_isdst;
  timespec->millisecond = tv_msec +
      (timp->tm_hour * 3600 + timp->tm_min * 60 + timp->tm_sec) * 1000;
}


Already provided, only didn't got it until now. :oops:
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: RTC localtime()

Post by Giovanni »

Apparently I forgot about it too :) Barthess worked on it.

Giovanni
Post Reply