用wxpython写了个小程序,打开程序后要把工作日期往前倒退一天,看wx.DateTime的说明时,发现这个控件有强大的自动分析功能。官方原文如下:
The date formatting and parsing functions convert wx.DateTime objects to and from text. The conversions to text are mostly trivial: you can either do it using the default date and time representations for the current locale ( FormatDate and FormatTime), using the international standard representation defined by ISO 8601 ( FormatISODate, FormatISOTime and FormatISOCombined) or by specifying any format at all and using Format directly. The conversions from text are more interesting, as there are much more possibilities to care about. The simplest cases can be taken care of with ParseFormat which can parse any date in the given (rigid) format. ParseRfc822Date is another function for parsing dates in predefined format – the one of RFC 822 which (still…) defines the format of email messages on the Internet. This format cannot be described with strptime(3)-like format strings used by Format, hence the need for a separate function. But the most interesting functions are ParseTime, ParseDate and ParseDateTime. They try to parse the date and time (or only one of them) in ‘free’ format, i.e. allow them to be specified in any of possible ways. These functions will usually be used to parse the (interactive) user input which is not bound to be in any predefined format. As an example, ParseDate can parse the strings such as “tomorrow”, “March first” and even “next Sunday”. Finally notice that each of the parsing functions is available in several overloads: if the input string is a narrow ( char ) string, then a narrow pointer is returned. If the input string is a wide string, a wide int pointer is returned. Finally, if the input parameter is a String , a narrow int pointer is also returned for backwards compatibility but there is also an additional argument of String.const_iterator type in which, if it is not None, an iterator pointing to the end of the scanned string part is returned.
于是用ParseDate(‘Yesterday’)就可以简简单单的自动往前一天了。
代码及结果如下图: