Best practise for passing time as arguments

Marko Rauhamaa marko at pacujo.net
Sat Oct 14 12:40:03 EDT 2017


Andrew Z <formisc at gmail.com>:

>  I wonder what are the "best practises" for passing "time" parameters to
> functions?
> I noticed that sometimes i pass "time" as a str and then start "massaging"
> it into delta or i need this time format or that format. Thats quite
> annoying and inconsistent.

I do the same thing. It's not all that bad because the choice depends on
the needs of the application.

It's a slight bit annoying that I have to write parsing functions
myself, but it's not all that bad:

   def parse_date(date):
       return datetime.date(*map(int, date.split("-")))

   def parse_time(time):
       return datetime.time(*map(int, time.split(":")))

where "date" is expressed as "2017-10-14" and "time" [of day] as "19:39".


Marko



More information about the Python-list mailing list