Question

In time.strftime(format[, t]) the first parameter is format and in time.strptime(string[, format]) it is second parameter. Why is that so? Sometimes i got confused and unintentionally used format as first parameter in time.strptime which raised an error.

Was it helpful?

Solution

The general principle is that you put required arguments before optional arguments (and indeed you can't put optional arguments before required arguments only if you were to use keyword arguments, which time.strftime and time.strptime don't support.) Since time.strftime(format) formats the current time, the optional time to use instead of the current time has to be the second argument. And likewise, since time.strptime(string) parses string according to the default format, the format has to be the second argument.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top