【问题标题】:strptime in windows? [duplicate]Windows中的strptime? [复制]
【发布时间】:2010-10-14 14:43:03
【问题描述】:

我编写了这个在 Linux 中运行良好的非常好的应用程序。

它使用 strptime()。 Windows 没有这个。

是否有 Windows 替代方案?

我的同事需要使用这个应用程序。

(我已经用谷歌搜索了它没有用)

【问题讨论】:

    标签: c++ c windows visual-studio linux


    【解决方案1】:

    我已经编写了一个 strptime,我在 Windows 上使用它来弥补它在 Microsoft C 运行时库中的缺失。 这里是:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include <ctype.h>
    
    #ifdef _MSC_VER
    const char * strp_weekdays[] = 
        { "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"};
    const char * strp_monthnames[] = 
        { "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"};
    bool strp_atoi(const char * & s, int & result, int low, int high, int offset)
        {
        bool worked = false;
        char * end;
        unsigned long num = strtoul(s, & end, 10);
        if (num >= (unsigned long)low && num <= (unsigned long)high)
            {
            result = (int)(num + offset);
            s = end;
            worked = true;
            }
        return worked;
        }
    char * strptime(const char *s, const char *format, struct tm *tm)
        {
        bool working = true;
        while (working && *format && *s)
            {
            switch (*format)
                {
            case '%':
                {
                ++format;
                switch (*format)
                    {
                case 'a':
                case 'A': // weekday name
                    tm->tm_wday = -1;
                    working = false;
                    for (size_t i = 0; i < 7; ++ i)
                        {
                        size_t len = strlen(strp_weekdays[i]);
                        if (!strnicmp(strp_weekdays[i], s, len))
                            {
                            tm->tm_wday = i;
                            s += len;
                            working = true;
                            break;
                            }
                        else if (!strnicmp(strp_weekdays[i], s, 3))
                            {
                            tm->tm_wday = i;
                            s += 3;
                            working = true;
                            break;
                            }
                        }
                    break;
                case 'b':
                case 'B':
                case 'h': // month name
                    tm->tm_mon = -1;
                    working = false;
                    for (size_t i = 0; i < 12; ++ i)
                        {
                        size_t len = strlen(strp_monthnames[i]);
                        if (!strnicmp(strp_monthnames[i], s, len))
                            {
                            tm->tm_mon = i;
                            s += len;
                            working = true;
                            break;
                            }
                        else if (!strnicmp(strp_monthnames[i], s, 3))
                            {
                            tm->tm_mon = i;
                            s += 3;
                            working = true;
                            break;
                            }
                        }
                    break;
                case 'd':
                case 'e': // day of month number
                    working = strp_atoi(s, tm->tm_mday, 1, 31, 0);
                    break;
                case 'D': // %m/%d/%y
                    {
                    const char * s_save = s;
                    working = strp_atoi(s, tm->tm_mon, 1, 12, -1);
                    if (working && *s == '/')
                        {
                        ++ s;
                        working = strp_atoi(s, tm->tm_mday, 1, 31, 0);
                        if (working && *s == '/')
                            {
                            ++ s;
                            working = strp_atoi(s, tm->tm_year, 0, 99, 0);
                            if (working && tm->tm_year < 69)
                                tm->tm_year += 100;
                            }
                        }
                    if (!working)
                        s = s_save;
                    }
                    break;
                case 'H': // hour
                    working = strp_atoi(s, tm->tm_hour, 0, 23, 0);
                    break;
                case 'I': // hour 12-hour clock
                    working = strp_atoi(s, tm->tm_hour, 1, 12, 0);
                    break;
                case 'j': // day number of year
                    working = strp_atoi(s, tm->tm_yday, 1, 366, -1);
                    break;
                case 'm': // month number
                    working = strp_atoi(s, tm->tm_mon, 1, 12, -1);
                    break;
                case 'M': // minute
                    working = strp_atoi(s, tm->tm_min, 0, 59, 0);
                    break;
                case 'n': // arbitrary whitespace
                case 't':
                    while (isspace((int)*s)) 
                        ++s;
                    break;
                case 'p': // am / pm
                    if (!strnicmp(s, "am", 2))
                        { // the hour will be 1 -> 12 maps to 12 am, 1 am .. 11 am, 12 noon 12 pm .. 11 pm
                        if (tm->tm_hour == 12) // 12 am == 00 hours
                            tm->tm_hour = 0;
                        s += 2;
                        }
                    else if (!strnicmp(s, "pm", 2))
                        {
                        if (tm->tm_hour < 12) // 12 pm == 12 hours
                            tm->tm_hour += 12; // 1 pm -> 13 hours, 11 pm -> 23 hours
                        s += 2;
                        }
                    else
                        working = false;
                    break;
                case 'r': // 12 hour clock %I:%M:%S %p
                    {
                    const char * s_save = s;
                    working = strp_atoi(s, tm->tm_hour, 1, 12, 0);
                    if (working && *s == ':')
                        {
                        ++ s;
                        working = strp_atoi(s, tm->tm_min, 0, 59, 0);
                        if (working && *s == ':')
                            {
                            ++ s;
                            working = strp_atoi(s, tm->tm_sec, 0, 60, 0);
                            if (working && isspace((int)*s))
                                {
                                ++ s;
                                while (isspace((int)*s)) 
                                    ++s;
                                if (!strnicmp(s, "am", 2))
                                    { // the hour will be 1 -> 12 maps to 12 am, 1 am .. 11 am, 12 noon 12 pm .. 11 pm
                                    if (tm->tm_hour == 12) // 12 am == 00 hours
                                        tm->tm_hour = 0;
                                    }
                                else if (!strnicmp(s, "pm", 2))
                                    {
                                    if (tm->tm_hour < 12) // 12 pm == 12 hours
                                        tm->tm_hour += 12; // 1 pm -> 13 hours, 11 pm -> 23 hours
                                    }
                                else
                                    working = false;
                                }
                            }
                        }
                    if (!working)
                        s = s_save;
                    }
                    break;
                case 'R': // %H:%M
                    {
                    const char * s_save = s;
                    working = strp_atoi(s, tm->tm_hour, 0, 23, 0);
                    if (working && *s == ':')
                        {
                        ++ s;
                        working = strp_atoi(s, tm->tm_min, 0, 59, 0);
                        }
                    if (!working)
                        s = s_save;
                    }
                    break;
                case 'S': // seconds
                    working = strp_atoi(s, tm->tm_sec, 0, 60, 0);
                    break;
                case 'T': // %H:%M:%S
                    {
                    const char * s_save = s;
                    working = strp_atoi(s, tm->tm_hour, 0, 23, 0);
                    if (working && *s == ':')
                        {
                        ++ s;
                        working = strp_atoi(s, tm->tm_min, 0, 59, 0);
                        if (working && *s == ':')
                            {
                            ++ s;
                            working = strp_atoi(s, tm->tm_sec, 0, 60, 0);
                            }
                        }
                    if (!working)
                        s = s_save;
                    }
                    break;
                case 'w': // weekday number 0->6 sunday->saturday
                    working = strp_atoi(s, tm->tm_wday, 0, 6, 0);
                    break;
                case 'Y': // year
                    working = strp_atoi(s, tm->tm_year, 1900, 65535, -1900);
                    break;
                case 'y': // 2-digit year
                    working = strp_atoi(s, tm->tm_year, 0, 99, 0);
                    if (working && tm->tm_year < 69)
                        tm->tm_year += 100;
                    break;
                case '%': // escaped
                    if (*s != '%')
                        working = false;
                    ++s;
                    break;
                default:
                    working = false;
                    }
                }
                break;
            case ' ':
            case '\t':
            case '\r':
            case '\n':
            case '\f':
            case '\v':
                // zero or more whitespaces:
                while (isspace((int)*s))
                    ++ s;
                break;
            default:
                // match character
                if (*s != *format)
                    working = false;
                else
                    ++s;
                break;
                }
            ++format;
            }
        return (working?(char *)s:0);
        }
    #endif // _MSC_VER
    

    我在写这个函数的时候参考了在线手册页man strptime(3)

    【讨论】:

    • 我认为您的月份偏移量是错误的,tm-&gt;tm_mday 从 1 开始,而不是 0(在大小写 'd' 下)
    • 是的,strp_atoi(s, tm->tm_mday, 1, 31, -1) 调用应该是 strp_atoi(s, tm->tm_mday, 1, 31, 0);
    • 谢谢 jjv360 和 Arno Duvenhage。我已按照您的建议进行了更改,以修复我的错误。
    • 我点击了你的答案,因为它最终对我有用,但经过一些调试。特别是致电strp_atoi(...)。参数:const char * &amp; s, int &amp; result, 需要更改为:const char *s, int *result,
    • 感谢 Gebi Miguel 指出我的错误以防万一 'p' - 我已经修复了。
    猜你喜欢
    • 2010-09-24
    • 2021-09-11
    • 1970-01-01
    • 2010-10-03
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多