一个月和一天的缩写

时间:2015-05-26 03:37:13

标签: python datetime

我正在尝试编写正则表达式以捕获

的输出

datetime.datetime.now().ctime()

Mon May 25 20:20:41 2015

我想知道星期几和月份缩写的可能输出是什么?

2 个答案:

答案 0 :(得分:3)

Here's some information关于字符串ctime生成的格式。基本上它始终是具有基本格式的固定长度字符串。

答案 1 :(得分:2)

Python datetime class does not invoke C ctime() function即使在不提供C ctime()the equivalent of asctime(localtime(clock)))的平台上也能正常工作。

CPython source code表示格式为:

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() definition

相同
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 %d\n",
        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;
}