datetime - Python: Effective way to convert ambiguous hours into time object? -


I have a list of strings, which is as follows:

  4: 00- 5: 00PM 11: 00-2: 00 PM 12: 00-1: 00 PM 11: 00-1: 00AM   

And I try to find an effective way of creating the object twice (I think this is the only way to keep track of a limit of time, which I will later add to a date object). It is clear to humans what we mean 11: 00-1: 00 AM, but considering what is an effective way to change it:

  datetime.time (23, 0 ) Datetime.time (1, 0)   

Take my current view for the first time, and prepare a PM and AM version, take timeliness with the end time (which is specified) , And less than two differences are correct.

This is a simple implementation.

  & gt; & Gt; & Gt; Def Timelines: ... T1, T2 = Timestar Split ("-") ... Timeformat = "% I:% M% p" ... t1AM = datetime.datetime.strptime (t1 + " AM ", time format) ... t1PM = datetime.datetime.strptime (t1 +" PM ", time format) ... t2 = datetime.datetime.strptime (T2, time format) ... ... if ( T2 - T1AM) .seconds & lt; (T2-t1PM) .seconds: ... return t1AM.time (), t2.time () ... else: ... return t1PM.time (), t2.time ()> & gt; & Gt; & Gt; Timrange ("11: 00-2: 00 pm") (datetime.time (11, 0), datetime.time (14, 0)) gt; & Gt; & Gt; Timrange ("4: 00-5: 00 pm") (datetime.time (16, 0), datetime.time (17, 0)) gt; & Gt; & Gt; Time range ("11: 00-1: 00AM") (datetime.time (23, 0), datetime.time (1, 0)) gt; & Gt; & Gt; Timrange ("11: 00-2: 00 pm") (datetime.time (11, 0), datetime.time (14, 0)) gt; & Gt; & Gt; Timeline ("12: 00-1: 00 pm") (datetime.time (12, 0), datetime.time (13, 0))   

this time Object, but if you need it, then you can roll it into the Detach object.

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -