On Sat, Oct 6, 2012 at 10:37 PM, Richard D. Moores <rdmoo...@gmail.com> wrote: > On Sat, Oct 6, 2012 at 5:22 PM, eryksun <eryk...@gmail.com> wrote: > >> >>> from datetime import date >> >>> date(2014, 2, 18).strftime("%A") >> 'Tuesday' >> >> http://docs.python.org/library/datetime#strftime-and-strptime-behavior > > I remain bewildered. Where did these strangely named things come from, > strftime and strptime? I see that
These are named for the C standard library functions for [f]ormatting and [p]arsing broken-down [time] structures to and from [str]ings. For examples, see the GNU libc docs: http://www.gnu.org/software/libc/manual/html_node/Calendar-Time.html I tend to agree that in Python the names seem... oddly wonkish, and the reliance on terse formatting codes seems... oddly low-level. You just have to get used to it, like working with regular expressions and string formatting. Or use (or write your own) higher-level APIs such as calendar.day_name, which uses datetime.date.strftime: class _localized_day: # January 1, 2001, was a Monday. _days = [datetime.date(2001, 1, i+1).strftime for i in range(7)] def __init__(self, format): self.format = format def __getitem__(self, i): funcs = self._days[i] if isinstance(i, slice): return [f(self.format) for f in funcs] else: return funcs(self.format) def __len__(self): return 7 day_name = _localized_day('%A') Notice day_name is initialized with the "%A" format code, and _localized_day has a class attribute "_days" that's a list of 7 strftime bound methods. It uses these in __getitem__ to format a single day or a slice of several days: >>> calendar.day_name[:3] ['Monday', 'Tuesday', 'Wednesday'] > "date, datetime, and time objects all support a strftime(format) > method, to create a string representing the time under the control of > an explicit format string. Broadly speaking, d.strftime(fmt) acts like > the time module’s time.strftime(fmt, d.timetuple()) although not all > objects support a timetuple() method." > > Total gibberish. I feel like I've hit a brick wall. Where can I go to > learn to understand it? I need some very basic, specific information. They're comparing the datetime methods to the similarly-named methods in the time module, such as time.strftime: http://docs.python.org/library/time#time.strftime which formats an instance of time.struct_time: http://docs.python.org/library/time#time.struct_time datetime.date.timetuple() and datetime.datetime.timetuple() both return an instance of time.struct_time: http://docs.python.org/library/datetime#datetime.date.timetuple http://docs.python.org/library/datetime#datetime.datetime.timetuple Compare struct_time to a libc broken-down time structure: http://www.gnu.org/software/libc/manual/html_node/Broken_002ddown-Time.html _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor