datetime_math.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. """!@package grass.temporal
  2. @brief GRASS Python scripting module (temporal GIS functions)
  3. Temporal GIS datetime math functions to be used in library functions and modules.
  4. (C) 2011-2013 by the GRASS Development Team
  5. This program is free software under the GNU General Public
  6. License (>=v2). Read the file COPYING that comes with GRASS
  7. for details.
  8. @author Soeren Gebbert
  9. """
  10. from datetime import datetime, date, time, timedelta
  11. import grass.script.core as core
  12. import copy
  13. DAY_IN_SECONDS = 86400
  14. SECOND_AS_DAY = 1.1574074074074073e-05
  15. ###############################################################################
  16. def relative_time_to_time_delta(value):
  17. """!Convert the double value representing days
  18. into a timedelta object.
  19. """
  20. days = int(value)
  21. seconds = value % 1
  22. seconds = round(seconds * DAY_IN_SECONDS)
  23. return timedelta(days, seconds)
  24. ###############################################################################
  25. def time_delta_to_relative_time(delta):
  26. """!Convert the time delta into a
  27. double value, representing days.
  28. """
  29. return float(delta.days) + float(delta.seconds * SECOND_AS_DAY)
  30. ###############################################################################
  31. def relative_time_to_time_delta_seconds(value):
  32. """!Convert the double value representing seconds
  33. into a timedelta object.
  34. """
  35. days = (value / 86400)
  36. seconds = int(value % 86400)
  37. return timedelta(days, seconds)
  38. ###############################################################################
  39. def time_delta_to_relative_time_seconds(delta):
  40. """!Convert the time delta into a
  41. double value, representing seconds.
  42. """
  43. return float(delta.days * DAY_IN_SECONDS) + float(delta.seconds)
  44. ###############################################################################
  45. def decrement_datetime_by_string(mydate, increment, mult=1):
  46. """!Return a new datetime object decremented with the provided
  47. relative dates specified as string.
  48. Additional a multiplier can be specified to multiply the increment
  49. before adding to the provided datetime object.
  50. Usage:
  51. @code
  52. >>> dt = datetime(2001, 1, 1, 0, 0, 0)
  53. >>> string = "31 days"
  54. >>> decrement_datetime_by_string(dt, string)
  55. datetime.datetime(2000, 12, 1, 0, 0)
  56. >>> dt = datetime(2001, 1, 1, 0, 0, 0)
  57. >>> string = "1 month"
  58. >>> decrement_datetime_by_string(dt, string)
  59. datetime.datetime(2000, 12, 1, 0, 0)
  60. >>> dt = datetime(2001, 1, 1, 0, 0, 0)
  61. >>> string = "2 month"
  62. >>> decrement_datetime_by_string(dt, string)
  63. datetime.datetime(2000, 11, 1, 0, 0)
  64. >>> dt = datetime(2001, 1, 1, 0, 0, 0)
  65. >>> string = "24 months"
  66. >>> decrement_datetime_by_string(dt, string)
  67. datetime.datetime(1999, 1, 1, 0, 0)
  68. >>> dt = datetime(2001, 1, 1, 0, 0, 0)
  69. >>> string = "48 months"
  70. >>> decrement_datetime_by_string(dt, string)
  71. datetime.datetime(1997, 1, 1, 0, 0)
  72. >>> dt = datetime(2001, 6, 1, 0, 0, 0)
  73. >>> string = "5 months"
  74. >>> decrement_datetime_by_string(dt, string)
  75. datetime.datetime(2001, 1, 1, 0, 0)
  76. >>> dt = datetime(2001, 6, 1, 0, 0, 0)
  77. >>> string = "7 months"
  78. >>> decrement_datetime_by_string(dt, string)
  79. datetime.datetime(2000, 11, 1, 0, 0)
  80. >>> dt = datetime(2001, 1, 1, 0, 0, 0)
  81. >>> string = "1 year"
  82. >>> decrement_datetime_by_string(dt, string)
  83. datetime.datetime(2000, 1, 1, 0, 0)
  84. @endcode
  85. @param mydate A datetime object to incremented
  86. @param increment A string providing increment information:
  87. The string may include comma separated values of type seconds,
  88. minutes, hours, days, weeks, months and years
  89. Example: Increment the datetime 2001-01-01 00:00:00
  90. with "60 seconds, 4 minutes, 12 hours, 10 days, 1 weeks, 5 months, 1 years"
  91. will result in the datetime 2003-02-18 12:05:00
  92. @param mult A multiplier, default is 1
  93. @return The new datetime object or none in case of an error
  94. """
  95. return modify_datetime_by_string(mydate, increment, mult, sign=int(-1))
  96. ###############################################################################
  97. def increment_datetime_by_string(mydate, increment, mult=1):
  98. """!Return a new datetime object incremented with the provided
  99. relative dates specified as string.
  100. Additional a multiplier can be specified to multiply the increment
  101. before adding to the provided datetime object.
  102. Usage:
  103. @code
  104. >>> dt = datetime(2001, 9, 1, 0, 0, 0)
  105. >>> string = "60 seconds, 4 minutes, 12 hours, 10 days, 1 weeks, 5 months, 1 years"
  106. >>> increment_datetime_by_string(dt, string)
  107. datetime.datetime(2003, 2, 18, 12, 5)
  108. >>> dt = datetime(2001, 11, 1, 0, 0, 0)
  109. >>> string = "1 months"
  110. >>> increment_datetime_by_string(dt, string)
  111. datetime.datetime(2001, 12, 1, 0, 0)
  112. >>> dt = datetime(2001, 11, 1, 0, 0, 0)
  113. >>> string = "13 months"
  114. >>> increment_datetime_by_string(dt, string)
  115. datetime.datetime(2002, 12, 1, 0, 0)
  116. >>> dt = datetime(2001, 1, 1, 0, 0, 0)
  117. >>> string = "72 months"
  118. >>> increment_datetime_by_string(dt, string)
  119. datetime.datetime(2007, 1, 1, 0, 0)
  120. >>> dt = datetime(2001, 1, 1, 0, 0, 0)
  121. >>> string = "72 months"
  122. >>> increment_datetime_by_string(dt, string)
  123. datetime.datetime(2007, 1, 1, 0, 0)
  124. >>> dt = datetime(2001, 1, 1, 0, 0, 0)
  125. >>> string = "5 minutes"
  126. >>> increment_datetime_by_string(dt, string)
  127. datetime.datetime(2001, 1, 1, 0, 5)
  128. >>> dt = datetime(2001, 1, 1, 0, 0, 0)
  129. >>> string = "49 hours"
  130. >>> increment_datetime_by_string(dt, string)
  131. datetime.datetime(2001, 1, 3, 1, 0)
  132. >>> dt = datetime(2001, 1, 1, 0, 0, 0)
  133. >>> string = "3600 seconds"
  134. >>> increment_datetime_by_string(dt, string)
  135. datetime.datetime(2001, 1, 1, 1, 0)
  136. >>> dt = datetime(2001, 1, 1, 0, 0, 0)
  137. >>> string = "30 days"
  138. >>> increment_datetime_by_string(dt, string)
  139. datetime.datetime(2001, 1, 31, 0, 0)
  140. @endcode
  141. @param mydate A datetime object to incremented
  142. @param increment A string providing increment information:
  143. The string may include comma separated values of type seconds,
  144. minutes, hours, days, weeks, months and years
  145. Example: Increment the datetime 2001-01-01 00:00:00
  146. with "60 seconds, 4 minutes, 12 hours, 10 days, 1 weeks, 5 months, 1 years"
  147. will result in the datetime 2003-02-18 12:05:00
  148. @param mult A multiplier, default is 1
  149. @return The new datetime object or none in case of an error
  150. """
  151. return modify_datetime_by_string(mydate, increment, mult, sign=int(1))
  152. ###############################################################################
  153. def modify_datetime_by_string(mydate, increment, mult=1, sign=1):
  154. """!Return a new datetime object incremented with the provided
  155. relative dates specified as string.
  156. Additional a multiplier can be specified to multiply the increment
  157. before adding to the provided datetime object.
  158. @param mydate A datetime object to incremented
  159. @param increment A string providing increment information:
  160. The string may include comma separated values of type seconds,
  161. minutes, hours, days, weeks, months and years
  162. Example: Increment the datetime 2001-01-01 00:00:00
  163. with "60 seconds, 4 minutes, 12 hours, 10 days, 1 weeks, 5 months, 1 years"
  164. will result in the datetime 2003-02-18 12:05:00
  165. @param mult A multiplier, default is 1
  166. @param sign Choose 1 for positive sign (incrementing) or -1 for negative
  167. sign (decrementing).
  168. @return The new datetime object or none in case of an error
  169. """
  170. sign = int(sign)
  171. if sign != 1 and sign != -1:
  172. return None
  173. if increment:
  174. seconds = 0
  175. minutes = 0
  176. hours = 0
  177. days = 0
  178. weeks = 0
  179. months = 0
  180. years = 0
  181. inclist = []
  182. # Split the increment string
  183. incparts = increment.split(",")
  184. for incpart in incparts:
  185. inclist.append(incpart.strip().split(" "))
  186. for inc in inclist:
  187. if len(inc) < 2:
  188. core.error(_("Wrong increment format: %s") % (increment))
  189. return None
  190. if inc[1].find("seconds") >= 0 or inc[1].find("second") >= 0:
  191. seconds = sign * mult * int(inc[0])
  192. elif inc[1].find("minutes") >= 0 or inc[1].find("minute") >= 0:
  193. minutes = sign * mult * int(inc[0])
  194. elif inc[1].find("hours") >= 0 or inc[1].find("hour") >= 0:
  195. hours = sign * mult * int(inc[0])
  196. elif inc[1].find("days") >= 0 or inc[1].find("day") >= 0:
  197. days = sign * mult * int(inc[0])
  198. elif inc[1].find("weeks") >= 0 or inc[1].find("week") >= 0:
  199. weeks = sign * mult * int(inc[0])
  200. elif inc[1].find("months") >= 0 or inc[1].find("month") >= 0:
  201. months = sign * mult * int(inc[0])
  202. elif inc[1].find("years") >= 0 or inc[1].find("year") >= 0:
  203. years = sign * mult * int(inc[0])
  204. else:
  205. core.error(_("Wrong increment format: %s") % (increment))
  206. return None
  207. return modify_datetime(mydate, years, months, weeks, days, hours, minutes, seconds)
  208. return mydate
  209. ###############################################################################
  210. def modify_datetime(mydate, years=0, months=0, weeks=0, days=0, hours=0,
  211. minutes=0, seconds=0):
  212. """!Return a new datetime object incremented with the provided
  213. relative dates and times"""
  214. tdelta_seconds = timedelta(seconds=seconds)
  215. tdelta_minutes = timedelta(minutes=minutes)
  216. tdelta_hours = timedelta(hours=hours)
  217. tdelta_days = timedelta(days=days)
  218. tdelta_weeks = timedelta(weeks=weeks)
  219. tdelta_months = timedelta(0)
  220. tdelta_years = timedelta(0)
  221. if months > 0:
  222. # Compute the actual number of days in the month to add as timedelta
  223. year = mydate.year
  224. month = mydate.month
  225. all_months = int(months) + int(month)
  226. years_to_add = int(all_months / 12.001)
  227. residual_months = all_months - (years_to_add * 12)
  228. # Make a deep copy of the datetime object
  229. dt1 = copy.copy(mydate)
  230. # Make sure the month starts with a 1
  231. if residual_months == 0:
  232. residual_months = 1
  233. try:
  234. dt1 = dt1.replace(year=year + years_to_add, month=residual_months)
  235. except:
  236. raise
  237. tdelta_months = dt1 - mydate
  238. elif months < 0:
  239. # Compute the actual number of days in the month to add as timedelta
  240. year = mydate.year
  241. month = mydate.month
  242. years_to_remove = 0
  243. all_months = int(months) + int(month)
  244. if all_months <= 0:
  245. years_to_remove = abs(int(all_months / 12.001))
  246. residual_months = all_months + (years_to_remove * 12)
  247. years_to_remove += 1
  248. else:
  249. residual_months = all_months
  250. # Make a deep copy of the datetime object
  251. dt1 = copy.copy(mydate)
  252. # Correct the months
  253. if residual_months <= 0:
  254. residual_months += 12
  255. try:
  256. dt1 = dt1.replace(year=year - years_to_remove, month=residual_months)
  257. except:
  258. raise
  259. tdelta_months = dt1 - mydate
  260. if years != 0:
  261. # Make a deep copy of the datetime object
  262. dt1 = copy.copy(mydate)
  263. # Compute the number of days
  264. dt1 = dt1.replace(year=mydate.year + int(years))
  265. tdelta_years = dt1 - mydate
  266. return mydate + tdelta_seconds + tdelta_minutes + tdelta_hours + \
  267. tdelta_days + tdelta_weeks + tdelta_months + tdelta_years
  268. ###############################################################################
  269. def adjust_datetime_to_granularity(mydate, granularity):
  270. """!Modify the datetime object to fit the given granularity
  271. - Years will start at the first of Januar
  272. - Months will start at the first day of the month
  273. - Days will start at the first Hour of the day
  274. - Hours will start at the first minute of an hour
  275. - Minutes will start at the first second of a minute
  276. Usage:
  277. @code
  278. >>> dt = datetime(2001, 8, 8, 12,30,30)
  279. >>> adjust_datetime_to_granularity(dt, "5 seconds")
  280. datetime.datetime(2001, 8, 8, 12, 30, 30)
  281. >>> adjust_datetime_to_granularity(dt, "20 minutes")
  282. datetime.datetime(2001, 8, 8, 12, 30)
  283. >>> adjust_datetime_to_granularity(dt, "20 minutes")
  284. datetime.datetime(2001, 8, 8, 12, 30)
  285. >>> adjust_datetime_to_granularity(dt, "3 hours")
  286. datetime.datetime(2001, 8, 8, 12, 0)
  287. >>> adjust_datetime_to_granularity(dt, "5 days")
  288. datetime.datetime(2001, 8, 8, 0, 0)
  289. >>> adjust_datetime_to_granularity(dt, "2 weeks")
  290. datetime.datetime(2001, 8, 6, 0, 0)
  291. >>> adjust_datetime_to_granularity(dt, "6 months")
  292. datetime.datetime(2001, 8, 1, 0, 0)
  293. >>> adjust_datetime_to_granularity(dt, "2 years")
  294. datetime.datetime(2001, 1, 1, 0, 0)
  295. >>> adjust_datetime_to_granularity(dt, "2 years, 3 months, 5 days, 3 hours, 3 minutes, 2 seconds")
  296. datetime.datetime(2001, 8, 8, 12, 30, 30)
  297. >>> adjust_datetime_to_granularity(dt, "3 months, 5 days, 3 minutes")
  298. datetime.datetime(2001, 8, 8, 12, 30)
  299. >>> adjust_datetime_to_granularity(dt, "3 weeks, 5 days")
  300. datetime.datetime(2001, 8, 8, 0, 0)
  301. @endcode
  302. """
  303. if granularity:
  304. has_seconds = False
  305. has_minutes = False
  306. has_hours = False
  307. has_days = False
  308. has_weeks = False
  309. has_months = False
  310. has_years = False
  311. seconds = mydate.second
  312. minutes = mydate.minute
  313. hours = mydate.hour
  314. days = mydate.day
  315. weekday = mydate.weekday()
  316. months = mydate.month
  317. years = mydate.year
  318. granlist = []
  319. # Split the increment string
  320. granparts = granularity.split(",")
  321. for granpart in granparts:
  322. granlist.append(granpart.strip().split(" "))
  323. for inc in granlist:
  324. if inc[1].find("seconds") >= 0 or inc[1].find("second") >= 0:
  325. has_seconds = True
  326. elif inc[1].find("minutes") >= 0 or inc[1].find("minute") >= 0:
  327. has_minutes = True
  328. elif inc[1].find("hours") >= 0 or inc[1].find("hour") >= 0:
  329. has_hours = True
  330. elif inc[1].find("days") >= 0 or inc[1].find("day") >= 0:
  331. has_days = True
  332. elif inc[1].find("weeks") >= 0 or inc[1].find("week") >= 0:
  333. has_weeks = True
  334. elif inc[1].find("months") >= 0 or inc[1].find("month") >= 0:
  335. has_months = True
  336. elif inc[1].find("years") >= 0 or inc[1].find("year") >= 0:
  337. has_years = True
  338. else:
  339. core.error(_("Wrong granularity format: %s") % (granularity))
  340. return None
  341. if has_seconds:
  342. pass
  343. elif has_minutes: # Start at 0 seconds
  344. seconds = 0
  345. elif has_hours: # Start at 0 minutes and seconds
  346. seconds = 0
  347. minutes = 0
  348. elif has_days: # Start at 0 hours, minutes and seconds
  349. seconds = 0
  350. minutes = 0
  351. hours = 0
  352. elif has_weeks: # Start at the first day of the week (Monday) at 00:00:00
  353. seconds = 0
  354. minutes = 0
  355. hours = 0
  356. if days > weekday:
  357. days = days - weekday # this needs to be fixed
  358. else:
  359. days = days + weekday # this needs to be fixed
  360. elif has_months: # Start at the first day of the month at 00:00:00
  361. seconds = 0
  362. minutes = 0
  363. hours = 0
  364. days = 1
  365. elif has_years: # Start at the first day of the first month at 00:00:00
  366. seconds = 0
  367. minutes = 0
  368. hours = 0
  369. days = 1
  370. months = 1
  371. dt = copy.copy(mydate)
  372. return dt.replace(year=years, month=months, day=days,
  373. hour=hours, minute=minutes, second=seconds)
  374. ###############################################################################
  375. def compute_datetime_delta(start, end):
  376. """!Return a dictionary with the accumulated delta in year, month, day,
  377. hour, minute and second
  378. Usage:
  379. @code
  380. >>> start = datetime(2001, 1, 1, 00,00,00)
  381. >>> end = datetime(2001, 1, 1, 00,00,00)
  382. >>> compute_datetime_delta(start, end)
  383. {'hour': 0, 'month': 0, 'second': 0, 'max_days': 0, 'year': 0, 'day': 0, 'minute': 0}
  384. >>> start = datetime(2001, 1, 1, 00,00,14)
  385. >>> end = datetime(2001, 1, 1, 00,00,44)
  386. >>> compute_datetime_delta(start, end)
  387. {'hour': 0, 'month': 0, 'second': 30, 'max_days': 0, 'year': 0, 'day': 0, 'minute': 0}
  388. >>> start = datetime(2001, 1, 1, 00,00,44)
  389. >>> end = datetime(2001, 1, 1, 00,01,14)
  390. >>> compute_datetime_delta(start, end)
  391. {'hour': 0, 'month': 0, 'second': 30, 'max_days': 0, 'year': 0, 'day': 0, 'minute': 1}
  392. >>> start = datetime(2001, 1, 1, 00,00,30)
  393. >>> end = datetime(2001, 1, 1, 00,05,30)
  394. >>> compute_datetime_delta(start, end)
  395. {'hour': 0, 'month': 0, 'second': 300, 'max_days': 0, 'year': 0, 'day': 0, 'minute': 5}
  396. >>> start = datetime(2001, 1, 1, 00,00,00)
  397. >>> end = datetime(2001, 1, 1, 00,01,00)
  398. >>> compute_datetime_delta(start, end)
  399. {'hour': 0, 'month': 0, 'second': 0, 'max_days': 0, 'year': 0, 'day': 0, 'minute': 1}
  400. >>> start = datetime(2011,10,31, 00,45,00)
  401. >>> end = datetime(2011,10,31, 01,45,00)
  402. >>> compute_datetime_delta(start, end)
  403. {'hour': 1, 'second': 0, 'max_days': 0, 'year': 0, 'day': 0, 'minute': 60}
  404. >>> start = datetime(2011,10,31, 00,45,00)
  405. >>> end = datetime(2011,10,31, 01,15,00)
  406. >>> compute_datetime_delta(start, end)
  407. {'hour': 1, 'second': 0, 'max_days': 0, 'year': 0, 'day': 0, 'minute': 30}
  408. >>> start = datetime(2011,10,31, 00,45,00)
  409. >>> end = datetime(2011,10,31, 12,15,00)
  410. >>> compute_datetime_delta(start, end)
  411. {'hour': 12, 'second': 0, 'max_days': 0, 'year': 0, 'day': 0, 'minute': 690}
  412. >>> start = datetime(2011,10,31, 00,00,00)
  413. >>> end = datetime(2011,10,31, 01,00,00)
  414. >>> compute_datetime_delta(start, end)
  415. {'hour': 1, 'second': 0, 'max_days': 0, 'year': 0, 'day': 0, 'minute': 0}
  416. >>> start = datetime(2011,10,31, 00,00,00)
  417. >>> end = datetime(2011,11,01, 01,00,00)
  418. >>> compute_datetime_delta(start, end)
  419. {'hour': 25, 'second': 0, 'max_days': 1, 'year': 0, 'day': 1, 'minute': 0}
  420. >>> start = datetime(2011,10,31, 12,00,00)
  421. >>> end = datetime(2011,11,01, 06,00,00)
  422. >>> compute_datetime_delta(start, end)
  423. {'hour': 18, 'second': 0, 'max_days': 0, 'year': 0, 'day': 0, 'minute': 0}
  424. >>> start = datetime(2011,11,01, 00,00,00)
  425. >>> end = datetime(2011,12,01, 01,00,00)
  426. >>> compute_datetime_delta(start, end)
  427. {'hour': 721, 'month': 1, 'second': 0, 'max_days': 30, 'year': 0, 'day': 0, 'minute': 0}
  428. >>> start = datetime(2011,11,01, 00,00,00)
  429. >>> end = datetime(2011,11,05, 00,00,00)
  430. >>> compute_datetime_delta(start, end)
  431. {'hour': 0, 'second': 0, 'max_days': 4, 'year': 0, 'day': 4, 'minute': 0}
  432. >>> start = datetime(2011,10,06, 00,00,00)
  433. >>> end = datetime(2011,11,05, 00,00,00)
  434. >>> compute_datetime_delta(start, end)
  435. {'hour': 0, 'second': 0, 'max_days': 30, 'year': 0, 'day': 30, 'minute': 0}
  436. >>> start = datetime(2011,12,02, 00,00,00)
  437. >>> end = datetime(2012,01,01, 00,00,00)
  438. >>> compute_datetime_delta(start, end)
  439. {'hour': 0, 'second': 0, 'max_days': 30, 'year': 1, 'day': 30, 'minute': 0}
  440. >>> start = datetime(2011,01,01, 00,00,00)
  441. >>> end = datetime(2011,02,01, 00,00,00)
  442. >>> compute_datetime_delta(start, end)
  443. {'hour': 0, 'month': 1, 'second': 0, 'max_days': 31, 'year': 0, 'day': 0, 'minute': 0}
  444. >>> start = datetime(2011,12,01, 00,00,00)
  445. >>> end = datetime(2012,01,01, 00,00,00)
  446. >>> compute_datetime_delta(start, end)
  447. {'hour': 0, 'month': 1, 'second': 0, 'max_days': 31, 'year': 1, 'day': 0, 'minute': 0}
  448. >>> start = datetime(2011,12,01, 00,00,00)
  449. >>> end = datetime(2012,06,01, 00,00,00)
  450. >>> compute_datetime_delta(start, end)
  451. {'hour': 0, 'month': 6, 'second': 0, 'max_days': 183, 'year': 1, 'day': 0, 'minute': 0}
  452. >>> start = datetime(2011,06,01, 00,00,00)
  453. >>> end = datetime(2021,06,01, 00,00,00)
  454. >>> compute_datetime_delta(start, end)
  455. {'hour': 0, 'month': 120, 'second': 0, 'max_days': 3653, 'year': 10, 'day': 0, 'minute': 0}
  456. >>> start = datetime(2011,06,01, 00,00,00)
  457. >>> end = datetime(2012,06,01, 12,00,00)
  458. >>> compute_datetime_delta(start, end)
  459. {'hour': 8796, 'month': 12, 'second': 0, 'max_days': 366, 'year': 1, 'day': 0, 'minute': 0}
  460. >>> start = datetime(2011,06,01, 00,00,00)
  461. >>> end = datetime(2012,06,01, 12,30,00)
  462. >>> compute_datetime_delta(start, end)
  463. {'hour': 8796, 'month': 12, 'second': 0, 'max_days': 366, 'year': 1, 'day': 0, 'minute': 527790}
  464. >>> start = datetime(2011,06,01, 00,00,00)
  465. >>> end = datetime(2012,06,01, 12,00,05)
  466. >>> compute_datetime_delta(start, end)
  467. {'hour': 8796, 'month': 12, 'second': 31665605, 'max_days': 366, 'year': 1, 'day': 0, 'minute': 0}
  468. >>> start = datetime(2011,06,01, 00,00,00)
  469. >>> end = datetime(2012,06,01, 00,30,00)
  470. >>> compute_datetime_delta(start, end)
  471. {'hour': 0, 'month': 12, 'second': 0, 'max_days': 366, 'year': 1, 'day': 0, 'minute': 527070}
  472. >>> start = datetime(2011,06,01, 00,00,00)
  473. >>> end = datetime(2012,06,01, 00,00,05)
  474. >>> compute_datetime_delta(start, end)
  475. {'hour': 0, 'month': 12, 'second': 31622405, 'max_days': 366, 'year': 1, 'day': 0, 'minute': 0}
  476. @endcode
  477. @return A dictionary with year, month, day, hour, minute and second as keys()
  478. """
  479. comp = {}
  480. day_diff = (end - start).days
  481. comp["max_days"] = day_diff
  482. # Date
  483. # Count full years
  484. d = end.year - start.year
  485. comp["year"] = d
  486. # Count full months
  487. if start.month == 1 and end.month == 1:
  488. comp["month"] = 0
  489. elif start.day == 1 and end.day == 1:
  490. d = end.month - start.month
  491. if d < 0:
  492. d = d + 12 * comp["year"]
  493. elif d == 0:
  494. d = 12 * comp["year"]
  495. comp["month"] = d
  496. # Count full days
  497. if start.day == 1 and end.day == 1:
  498. comp["day"] = 0
  499. else:
  500. comp["day"] = day_diff
  501. # Time
  502. # Hours
  503. if start.hour == 0 and end.hour == 0:
  504. comp["hour"] = 0
  505. else:
  506. d = end.hour - start.hour
  507. if d < 0:
  508. d = d + 24 + 24 * day_diff
  509. else:
  510. d = d + 24 * day_diff
  511. comp["hour"] = d
  512. # Minutes
  513. if start.minute == 0 and end.minute == 0:
  514. comp["minute"] = 0
  515. else:
  516. d = end.minute - start.minute
  517. if d != 0:
  518. if comp["hour"]:
  519. d = d + 60 * comp["hour"]
  520. else:
  521. d = d + 24 * 60 * day_diff
  522. elif d == 0:
  523. if comp["hour"]:
  524. d = 60 * comp["hour"]
  525. else:
  526. d = 24 * 60 * day_diff
  527. comp["minute"] = d
  528. # Seconds
  529. if start.second == 0 and end.second == 0:
  530. comp["second"] = 0
  531. else:
  532. d = end.second - start.second
  533. if d != 0:
  534. if comp["minute"]:
  535. d = d + 60 * comp["minute"]
  536. elif comp["hour"]:
  537. d = d + 3600 * comp["hour"]
  538. else:
  539. d = d + 24 * 60 * 60 * day_diff
  540. elif d == 0:
  541. if comp["minute"]:
  542. d = 60 * comp["minute"]
  543. elif comp["hour"]:
  544. d = 3600 * comp["hour"]
  545. else:
  546. d = 24 * 60 * 60 * day_diff
  547. comp["second"] = d
  548. return comp
  549. ###############################################################################
  550. def string_to_datetime(time_string):
  551. """!Convert a string into a datetime object
  552. Supported ISO string formats are:
  553. - YYYY-mm-dd
  554. - YYYY-mm-dd HH:MM:SS
  555. Time zones are not supported
  556. @param time_string The time string to convert
  557. @return datetime object or None in case of an error
  558. """
  559. # BC is not supported
  560. if time_string.find("bc") > 0:
  561. core.error("Dates Before Christ are not supported "
  562. "in the temporal database")
  563. return None
  564. # BC is not supported
  565. if time_string.find("+") > 0:
  566. core.error("Time zones are not supported "
  567. "in the temporal database")
  568. return None
  569. if time_string.find(":") > 0:
  570. time_format = "%Y-%m-%d %H:%M:%S"
  571. else:
  572. time_format = "%Y-%m-%d"
  573. try:
  574. return datetime.strptime(time_string, time_format)
  575. except:
  576. core.error("Unable to parse time string: %s"%time_string)
  577. return None
  578. ###############################################################################
  579. def datetime_to_grass_datetime_string(dt):
  580. """!Convert a python datetime object into a GRASS datetime string"""
  581. # GRASS datetime month names
  582. month_names = ["", "jan", "feb", "mar", "apr", "may", "jun",
  583. "jul", "aug", "sep", "oct", "nov", "dec"]
  584. # Check for time zone info in the datetime object
  585. if dt.tzinfo is not None:
  586. string = "%.2i %s %.2i %.2i:%.2i:%.2i %+.4i" % (dt.day,
  587. month_names[dt.month], dt.year,
  588. dt.hour, dt.minute, dt.second, dt.tzinfo._offset.seconds / 60)
  589. else:
  590. string = "%.2i %s %.4i %.2i:%.2i:%.2i" % (dt.day, month_names[
  591. dt.month], dt.year, dt.hour, dt.minute, dt.second)
  592. return string
  593. ###############################################################################
  594. if __name__ == "__main__":
  595. import doctest
  596. doctest.testmod()