MQX "struct tm" not standard C

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

MQX "struct tm" not standard C

1,190 次查看
hbouch
Contributor III

Hello,

When a look at the definition of the struct tm in mqx.h I see that it is a little different then standard C. Here is the definition in mqx.h:

struct tm {

    /*! \brief Range from 0 to 59. */
    int32_t         tm_sec;

    /*! \brief Range from 0 to 59. */
    int32_t         tm_min;

    /*! \brief Range from 0 to 23. */
    int32_t         tm_hour;

    /*! \brief Range from 0 to 30, depend on month. */
    int32_t         tm_mday;

    /*! \brief Range from 0 to 11. */
    int32_t         tm_mon;

    /*! \brief Range from 0, since 1900. */
    int32_t         tm_year;

    /*! \brief Range from 0 to 6. */
    int32_t         tm_wday;

    /*! \brief Range from 0 to 365. */
    int32_t         tm_yday;

    /*! \brief A flag that indicates whether daylight saving
    *  time is in effect at the time described
    */
    int32_t         tm_isdst;
};

As you can see, tm_mday is ranged from 0 to 30. According to C standard, tm_mday should be ranged from 1 to 31.

Is this normal?

3 回复数

923 次查看
danielchen
NXP TechSupport
NXP TechSupport

Hi  Hugo:

It is normal.    tm structure is a broken-down time format. you need conversion functions mktime, gmtime_r, timegm convert between this tm struct and the _time_t.

For example, in function mktime

...

    /* The first day of month in DATE_STRUCT is 1, while in tm struct is 0*/
    date.DAY    = (int32_t)tm_ptr->tm_mday + 1;

    /* The first month of year in DATE_STRUCT is 1, while in tm struct is 0*/
    date.MONTH  = (int32_t)tm_ptr->tm_mon  + 1;

...

Regards

Daniel

0 项奖励

923 次查看
hbouch
Contributor III

Hi Daniel,

I know there is conversion functions to convert between broken-down time format and unix format and that's exactly the problem. I have a RTC which I have to set. I get the command and time to set from a telnet connection from a PC. When I fill the broken-down time structure, if I respect the standard C of struct tm, the date saved is not what I would expect. It is one day later. I have to substract 1 to tm_mday to be compatible with MQX struct tm.

I know it's not a big deal and I can live with that. But why didn't MQX respect the C standard ? Why is MQX implementing time.c file when it is already provided by the standard C librairies of all C compilers?

Regards,

Hugo

923 次查看
adyr
Contributor V

I wish I had noticed this sooner.

I have been implementing a daylight saving plan in my project and have wasted a few days, thinking I had got my math wrong, only to discover this discrepancy :-(

If it is changed to be C compliant then it will break existing code that has worked around the problem so I guess it will have to stay this way.

0 项奖励