Question

To parse string date in natural language to python datetime , I am using parsedattime module.

   from parsedatetime import parsedatetime as pdt
   from parsedatetime import parsedatetime_consts as pdc
   from datetime import datetime

   str_date = '5minutes ago'
   c = pdc.Constants()
   p = pdt.Calendar(c)
   struct_date = p.parse(str_date)[0]
   ## format the result
   dt = datetime.fromtimestamp(mktime(struct_date))
   print dt.isoformat()

This will works well but the result changes in each execution , since it is :

  NOW() - '5minutes ago'

How should I modify this to parse 5minutes ago using a reference date to get always the same result and parse internally something like :

  REF_DATE - '5minutes ago'
Was it helpful?

Solution

Provide the sourceTime attribute.

p.parse(str_date, sourceTime=datetime(year=2000, month=1, day=1))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top