我正在尝试编写regex来捕获
的输出 datetime.datetime.now().ctime()
Mon May 25 20:20:41 2015
我想知道什么是星期和月的缩写可能的输出?
以下是ctime
生成的字符串格式的一些信息。基本上,它总是一个固定长度的字符串,具有基本格式
Python datetime
类不调用C ctime()
函数,即,即使在不提供C ctime()
(相当于asctime(localtime(clock))
)的平台上,它也可以工作。
CPython源代码说格式是:
static PyObject *
format_ctime(PyDateTime_Date *date, int hours, int minutes, int seconds)
{
static const char *DayNames[] = {
"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
};
static const char *MonthNames[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
int wday = weekday(GET_YEAR(date), GET_MONTH(date), GET_DAY(date));
return PyUnicode_FromFormat("%s %s %2d %02d:%02d:%02d %04d",
DayNames[wday], MonthNames[GET_MONTH(date)-1],
GET_DAY(date), hours, minutes, seconds,
GET_YEAR(date));
}
您可以使用C语言环境中的datetime.strptime(input_ctime_string, "%a %b %d %H:%M:%S %Y")
来解析它。
以上(几乎)与相应的asctime()
定义相同:
char *asctime(const struct tm *timeptr)
{
static char wday_name[7][3] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
static char mon_name[12][3] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
static char result[26];
sprintf(result, "%.3s %.3s%3d %.2d:%.2d:%.2d %dn",
wday_name[timeptr->tm_wday],
mon_name[timeptr->tm_mon],
timeptr->tm_mday, timeptr->tm_hour,
timeptr->tm_min, timeptr->tm_sec,
1900 + timeptr->tm_year);
return result;
}