openwrt / telephony

The telephony packages feed
105 stars 246 forks source link

"%F" is not compatible to format of strptime and strftime in asterisk musl build #750

Closed njhsi closed 2 years ago

njhsi commented 2 years ago

Problem encountered when doing asterisk calendar integration: time like "2022-02-28T03:30:00Z" was not parsed correctly.

After digging into codes, I found it's due to strptime format compatibility diff between GLIBC and MUSL As it's already stated in strptime man doc, at least ast_strptime(mstime, "%FT%TZ", &tm) which contains "%F" is for GLIBC only, and failure for MUSL.

For instance, for calendar of MS ews, fix below might help.

diff --git a/res/res_calendar_ews.c b/res/res_calendar_ews.c
index e4d250c..ac96993 100644
--- a/res/res_calendar_ews.c
+++ b/res/res_calendar_ews.c
@@ -165,7 +165,7 @@ static time_t mstime_to_time_t(char *mstime)
        struct ast_tm tm;
        struct timeval tv;

-       if (ast_strptime(mstime, "%FT%TZ", &tm)) {
+       if (ast_strptime(mstime, "%Y-%m-%dT%TZ", &tm)) {
                tv = ast_mktime(&tm, "UTC");
                return tv.tv_sec;
        }
@@ -475,7 +475,7 @@ static const char *mstime(time_t t, char *buf, size_t buflen)
        struct ast_tm tm;

        ast_localtime(&tv, &tm, "utc");
-       ast_strftime(buf, buflen, "%FT%TZ", &tm);
+       ast_strftime(buf, buflen, "%Y-%m-%dT%TZ", &tm);

        return S_OR(buf, "");
 }
@@ -668,10 +668,10 @@ static struct calendar_id *get_ewscal_ids_for(struct ewscal_pvt *pvt)
        /* Prepare timeframe strings */
        tv = ast_tvnow();
        ast_localtime(&tv, &tm, "UTC");
-       ast_strftime(start, sizeof(start), "%FT%TZ", &tm);
+       ast_strftime(start, sizeof(start), "%Y-%m-%dT%TZ", &tm);
        tv.tv_sec += 60 * pvt->owner->timeframe;
        ast_localtime(&tv, &tm, "UTC");
-       ast_strftime(end, sizeof(end), "%FT%TZ", &tm);
+       ast_strftime(end, sizeof(end), "%Y-%m-%dT%TZ", &tm);

        /* Prepare SOAP request */
        if (!(request = ast_str_create(512))) {
micmac1 commented 2 years ago

Hi NJ,

I guess you'd need somebody willing to test this and send it upstream. So far I haven't seen anybody raise this issue. So maybe it's in places not a lot of people use. Like, I'm pretty sure we added the calendar plugins not too long ago, even without anybody specifically asking for them (if my memory serves me well).

Kind regards, Seb

njhsi commented 2 years ago

Hi Seb,

Your guessing makes sense to me. I would not bother to send it upstream for the time being, just keep it here in case some one in future google searching.

BR, nj,