Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>import datetime import re givenURL = 'http://site.com/?year=2011&amp;month=10&amp;day=5&amp;hour=11' givenTime = '08:53PM' print ' givenURL == ' + givenURL print 'givenTime == ' + givenTime regx = re.compile('year=(\d\d\d\d)&amp;month=(\d\d?)&amp;day=(\d\d?)&amp;hour=\d\d?') print '\nmap(int,regx.search(givenURL).groups()) ==',map(int,regx.search(givenURL).groups()) theDate = datetime.date(*map(int,regx.search(givenURL).groups())) theTime = datetime.datetime.strptime(givenTime, "%I:%M%p") print '\ntheDate ==',theDate,type(theDate) print '\ntheTime ==',theTime,type(theTime) theDateTime = theTime.replace(theDate.year,theDate.month,theDate.day) print '\ntheDateTime ==',theDateTime,type(theDateTime) </code></pre> <p>result</p> <pre><code> givenURL == http://site.com/?year=2011&amp;month=10&amp;day=5&amp;hour=11 givenTime == 08:53PM map(int,regx.search(givenURL).groups()) == [2011, 10, 5] theDate == 2011-10-05 &lt;type 'datetime.date'&gt; theTime == 1900-01-01 20:53:00 &lt;type 'datetime.datetime'&gt; theDateTime == 2011-10-05 20:53:00 &lt;type 'datetime.datetime'&gt; </code></pre> <h2>Edit 1</h2> <p>As <strong>strptime()</strong> is slow, I improved my code to eliminate it</p> <pre><code>from datetime import datetime import re from time import clock n = 10000 givenURL = 'http://site.com/?year=2011&amp;month=10&amp;day=5&amp;hour=11' givenTime = '08:53AM' # eyquem regx = re.compile('year=(\d\d\d\d)&amp;month=(\d\d?)&amp;day=(\d\d?)&amp;hour=\d\d? (\d\d?):(\d\d?)(PM|pm)?') t0 = clock() for i in xrange(n): given = givenURL + ' ' + givenTime mat = regx.search(given) grps = map(int,mat.group(1,2,3,4,5)) if mat.group(6): grps[3] += 12 # when it is PM/pm, the hour must be augmented with 12 theDateTime1 = datetime(*grps) print clock()-t0,"seconds eyquem's code" print theDateTime1 print # Artsiom Rudzenka dateandtimePattern = "http://site.com/?year=%Y&amp;month=%m&amp;day=%d&amp;time=%I:%M%p" t0 = clock() for i in xrange(n): theDateTime2 = datetime.strptime(givenURL.split('&amp;hour=')[0] + '&amp;time=' + givenTime, dateandtimePattern) print clock()-t0,"seconds Artsiom's code" print theDateTime2 print print theDateTime1 == theDateTime2 </code></pre> <p>result</p> <pre><code>0.460598763251 seconds eyquem's code 2011-10-05 08:53:00 2.10386180366 seconds Artsiom's code 2011-10-05 08:53:00 True </code></pre> <p>My code is 4.5 times faster. That may be interesting if there are a lot of such transformations to perform</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload