C++ windows中的strtime?
我编写了这个非常好的应用程序,在Linux中运行得很好 它使用strtime()。Windows没有这个 有Windows的替代方案吗 我的同事需要使用此应用程序C++ windows中的strtime?,c++,c,windows,visual-studio,linux,C++,C,Windows,Visual Studio,Linux,我编写了这个非常好的应用程序,在Linux中运行得很好 它使用strtime()。Windows没有这个 有Windows的替代方案吗 我的同事需要使用此应用程序 (我试着用谷歌搜索它,但没有结果)我已经编写了一个strtime,我在Windows上使用它来弥补它在Microsoft C运行时库中的缺失。 这是: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ti
(我试着用谷歌搜索它,但没有结果)我已经编写了一个strtime,我在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
#包括
#包括
#包括
#包括
#包括
#ifdef硕士学位
常量字符*strp_工作日[]=
{“星期日”、“星期一”、“星期二”、“星期三”、“星期四”、“星期五”、“星期六”};
常量字符*strp_monthnames[]=
{“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”};
bool strp_atoi(常量字符*&s、整数和结果、整数低、整数高、整数偏移)
{
布尔工作=假;
字符*结束;
无符号long num=strtoul(s,&end,10);
如果(num>=(无符号长)低&&num tm_wday=-1;
工作=假;
对于(尺寸i=0;i<7;++i)
{
尺寸=标准长度(标准工作日[i]);
如果(!strnicmp(strp_平日[i],s,len))
{
tm->tm_wday=i;
s+=len;
工作=真实;
打破
}
否则如果(!strnicmp(strp_平日[i],s,3))
{
tm->tm_wday=i;
s+=3;
工作=真实;
打破
}
}
打破
案例“b”:
案例“B”:
案例'h'://月份名称
tm->tm_mon=-1;
工作=假;
对于(尺寸i=0;i<12;++i)
{
尺寸=标准长度(标准长度[i]);
如果(!strnicmp(strp_monthnames[i],s,len))
{
tm->tm_mon=i;
s+=len;
工作=真实;
打破
}
如果(!strnicmp(strp_monthnames[i],s,3]),则为其他情况
{
tm->tm_mon=i;
s+=3;
工作=真实;
打破
}
}
打破
案例“d”:
案例“e”://月日编号
工作=strp_atoi(s,tm->tm_mday,1,31,0);
打破
案例“D”://%m/%D/%y
{
const char*s_save=s;
工作=strp_atoi(s,tm->tm_mon,1,12,-1);
如果(工作&&*s=='/'))
{
++s;
工作=strp_atoi(s,tm->tm_mday,1,31,0);
如果(工作&&*s=='/'))
{
++s;
工作=strp_原子(s,tm->tm_年,0,99,0);
如果(工作和维护->维护年<69)
tm->tm_年+=100;
}
}
如果(!正在工作)
s=s_保存;
}
打破
案例'H'://小时
工作=strp_atoi(s,tm->tm_小时,0,23,0);
打破
案例I://小时12小时钟
工作=strp_atoi(s,tm->tm_小时,1,12,0);
打破
案例'j'://年份的天数
工作=strp_atoi(s,tm->tm_yday,1366,-1);
打破
案例'm'://月数
工作=strp_atoi(s,tm->tm_mon,1,12,-1);
打破
案例'M'://分钟
工作=strp_atoi(s,tm->tm_min,0,59,0);
打破
大小写“n”://任意空格
案例“t”:
while(isspace((int)*s))
++s;
打破
案例'p'://上午/下午
如果(!strnicmp(s,“am”,2))
{//时间为凌晨1点->12点至凌晨12点、凌晨1点至上午11点、中午12点至下午12点至晚上11点
如果(tm->tm_hour==12)//12 am==00小时
tm->tm_小时=0;
s+=2;
}
否则如果(!strnicmp(s,“pm”,2))
{
如果(tm->tm_hour<12)//12 pm==12小时
tm->tm_hour+=12;//下午1点->13小时,晚上11点->23小时
s+=2;
}
其他的
工作=假;
打破
案例'r'://12小时时钟%I:%M:%S%p
{
const char*s_save=s;
工作=strp_atoi(s,tm->tm_小时,1,12,0);
如果(工作&&*s==':')
{
++s;
工作=strp_atoi(s,tm->tm_min,0,59,0);
如果(工作&&*s==':')
{
++s;
工作=strp_原子(s,tm->tm_秒,0,60,0);
if(工作空间((int)*s))
{
++s;
while(isspace((int)*s))
++s;
如果(!strnicmp(s,“am”,2))
{//时间为凌晨1点->12点至凌晨12点、凌晨1点至上午11点、中午12点至下午12点至晚上11点
如果(tm->tm_hour==12)//12 am==00小时
tm->tm_小时=0;
}
如果(!str