datetime_math.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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) 2008-2011 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. from dateutil import parser
  14. DAY_IN_SECONDS = 86400
  15. SECOND_AS_DAY = 1.1574074074074073e-05
  16. ###############################################################################
  17. def relative_time_to_time_delta(value):
  18. """!Convert the double value representing days
  19. into a timedelta object.
  20. """
  21. days = int(value)
  22. seconds = value % 1
  23. seconds = round(seconds * DAY_IN_SECONDS)
  24. return timedelta(days, seconds)
  25. ###############################################################################
  26. def time_delta_to_relative_time(delta):
  27. """!Convert the time delta into a
  28. double value, representing days.
  29. """
  30. return float(delta.days) + float(delta.seconds * SECOND_AS_DAY)
  31. ###############################################################################
  32. def increment_datetime_by_string(mydate, increment, mult=1):
  33. """!Return a new datetime object incremented with the provided
  34. relative dates specified as string.
  35. Additional a multiplier can be specified to multiply the increment
  36. before adding to the provided datetime object.
  37. Usage:
  38. @code
  39. >>> dt = datetime(2001, 9, 1, 0, 0, 0)
  40. >>> string = "60 seconds, 4 minutes, 12 hours, 10 days, 1 weeks, 5 months, 1 years"
  41. >>> increment_datetime_by_string(dt, string)
  42. datetime.datetime(2003, 2, 18, 12, 5)
  43. >>> dt = datetime(2001, 11, 1, 0, 0, 0)
  44. >>> string = "1 months"
  45. >>> increment_datetime_by_string(dt, string)
  46. datetime.datetime(2001, 12, 1, 0, 0)
  47. >>> dt = datetime(2001, 11, 1, 0, 0, 0)
  48. >>> string = "13 months"
  49. >>> increment_datetime_by_string(dt, string)
  50. datetime.datetime(2002, 12, 1, 0, 0)
  51. >>> dt = datetime(2001, 1, 1, 0, 0, 0)
  52. >>> string = "72 months"
  53. >>> increment_datetime_by_string(dt, string)
  54. datetime.datetime(2007, 1, 1, 0, 0)
  55. >>> dt = datetime(2001, 1, 1, 0, 0, 0)
  56. >>> string = "72 months"
  57. >>> increment_datetime_by_string(dt, string)
  58. datetime.datetime(2007, 1, 1, 0, 0)
  59. >>> dt = datetime(2001, 1, 1, 0, 0, 0)
  60. >>> string = "5 minutes"
  61. >>> increment_datetime_by_string(dt, string)
  62. datetime.datetime(2001, 1, 1, 0, 5)
  63. >>> dt = datetime(2001, 1, 1, 0, 0, 0)
  64. >>> string = "49 hours"
  65. >>> increment_datetime_by_string(dt, string)
  66. datetime.datetime(2001, 1, 3, 1, 0)
  67. >>> dt = datetime(2001, 1, 1, 0, 0, 0)
  68. >>> string = "3600 seconds"
  69. >>> increment_datetime_by_string(dt, string)
  70. datetime.datetime(2001, 1, 1, 1, 0)
  71. >>> dt = datetime(2001, 1, 1, 0, 0, 0)
  72. >>> string = "30 days"
  73. >>> increment_datetime_by_string(dt, string)
  74. datetime.datetime(2001, 1, 31, 0, 0)
  75. @endcode
  76. @param mydate A datetime object to incremented
  77. @param increment A string providing increment information:
  78. The string may include comma separated values of type seconds,
  79. minutes, hours, days, weeks, months and years
  80. Example: Increment the datetime 2001-01-01 00:00:00
  81. with "60 seconds, 4 minutes, 12 hours, 10 days, 1 weeks, 5 months, 1 years"
  82. will result in the datetime 2003-02-18 12:05:00
  83. @param mult A multiplier, default is 1
  84. """
  85. if increment:
  86. seconds = 0
  87. minutes = 0
  88. hours = 0
  89. days = 0
  90. weeks = 0
  91. months = 0
  92. years = 0
  93. inclist = []
  94. # Split the increment string
  95. incparts = increment.split(",")
  96. for incpart in incparts:
  97. inclist.append(incpart.strip().split(" "))
  98. for inc in inclist:
  99. if len(inc) < 2:
  100. core.error(_("Wrong increment format: %s") % (increment))
  101. return None
  102. if inc[1].find("seconds") >= 0:
  103. seconds = mult * int(inc[0])
  104. elif inc[1].find("minutes") >= 0:
  105. minutes = mult * int(inc[0])
  106. elif inc[1].find("hours") >= 0:
  107. hours = mult * int(inc[0])
  108. elif inc[1].find("days") >= 0:
  109. days = mult * int(inc[0])
  110. elif inc[1].find("weeks") >= 0:
  111. weeks = mult * int(inc[0])
  112. elif inc[1].find("months") >= 0:
  113. months = mult * int(inc[0])
  114. elif inc[1].find("years") >= 0:
  115. years = mult * int(inc[0])
  116. else:
  117. core.error(_("Wrong increment format: %s") % (increment))
  118. return None
  119. return increment_datetime(mydate, years, months, weeks, days, hours, minutes, seconds)
  120. return mydate
  121. ###############################################################################
  122. def increment_datetime(mydate, years=0, months=0, weeks=0, days=0, hours=0,
  123. minutes=0, seconds=0):
  124. """!Return a new datetime object incremented with the provided
  125. relative dates and times"""
  126. tdelta_seconds = timedelta(seconds=seconds)
  127. tdelta_minutes = timedelta(minutes=minutes)
  128. tdelta_hours = timedelta(hours=hours)
  129. tdelta_days = timedelta(days=days)
  130. tdelta_weeks = timedelta(weeks=weeks)
  131. tdelta_months = timedelta(0)
  132. tdelta_years = timedelta(0)
  133. if months > 0:
  134. # Compute the actual number of days in the month to add as timedelta
  135. year = mydate.year
  136. month = mydate.month
  137. all_months = int(months) + int(month)
  138. years_to_add = int(all_months / 12.001)
  139. residual_months = all_months - (years_to_add * 12)
  140. # Make a deep copy of the datetime object
  141. dt1 = copy.copy(mydate)
  142. # Make sure the month starts with a 1
  143. if residual_months == 0:
  144. residual_months = 1
  145. dt1 = dt1.replace(year=year + years_to_add, month=residual_months)
  146. tdelta_months = dt1 - mydate
  147. if years > 0:
  148. # Make a deep copy of the datetime object
  149. dt1 = copy.copy(mydate)
  150. # Compute the number of days
  151. dt1 = dt1.replace(year=mydate.year + int(years))
  152. tdelta_years = dt1 - mydate
  153. return mydate + tdelta_seconds + tdelta_minutes + tdelta_hours + \
  154. tdelta_days + tdelta_weeks + tdelta_months + tdelta_years
  155. ###############################################################################
  156. def adjust_datetime_to_granularity(mydate, granularity):
  157. """!Modify the datetime object to fit the given granularity
  158. * Years will start at the first of Januar
  159. * Months will start at the first day of the month
  160. * Days will start at the first Hour of the day
  161. * Hours will start at the first minute of an hour
  162. * Minutes will start at the first second of a minute
  163. Usage:
  164. @code
  165. >>> dt = datetime(2001, 8, 8, 12,30,30)
  166. >>> adjust_datetime_to_granularity(dt, "5 seconds")
  167. datetime.datetime(2001, 8, 8, 12, 30, 30)
  168. >>> adjust_datetime_to_granularity(dt, "20 minutes")
  169. datetime.datetime(2001, 8, 8, 12, 30)
  170. >>> adjust_datetime_to_granularity(dt, "20 minutes")
  171. datetime.datetime(2001, 8, 8, 12, 30)
  172. >>> adjust_datetime_to_granularity(dt, "3 hours")
  173. datetime.datetime(2001, 8, 8, 12, 0)
  174. >>> adjust_datetime_to_granularity(dt, "5 days")
  175. datetime.datetime(2001, 8, 8, 0, 0)
  176. >>> adjust_datetime_to_granularity(dt, "2 weeks")
  177. datetime.datetime(2001, 8, 6, 0, 0)
  178. >>> adjust_datetime_to_granularity(dt, "6 months")
  179. datetime.datetime(2001, 8, 1, 0, 0)
  180. >>> adjust_datetime_to_granularity(dt, "2 years")
  181. datetime.datetime(2001, 1, 1, 0, 0)
  182. >>> adjust_datetime_to_granularity(dt, "2 years, 3 months, 5 days, 3 hours, 3 minutes, 2 seconds")
  183. datetime.datetime(2001, 8, 8, 12, 30, 30)
  184. >>> adjust_datetime_to_granularity(dt, "3 months, 5 days, 3 minutes")
  185. datetime.datetime(2001, 8, 8, 12, 30)
  186. >>> adjust_datetime_to_granularity(dt, "3 weeks, 5 days")
  187. datetime.datetime(2001, 8, 8, 0, 0)
  188. @endcode
  189. """
  190. if granularity:
  191. has_seconds = False
  192. has_minutes = False
  193. has_hours = False
  194. has_days = False
  195. has_weeks = False
  196. has_months = False
  197. has_years = False
  198. seconds = mydate.second
  199. minutes = mydate.minute
  200. hours = mydate.hour
  201. days = mydate.day
  202. weekday = mydate.weekday()
  203. months = mydate.month
  204. years = mydate.year
  205. granlist = []
  206. # Split the increment string
  207. granparts = granularity.split(",")
  208. for granpart in granparts:
  209. granlist.append(granpart.strip().split(" "))
  210. for inc in granlist:
  211. if inc[1].find("seconds") >= 0:
  212. has_seconds = True
  213. elif inc[1].find("minutes") >= 0:
  214. has_minutes = True
  215. elif inc[1].find("hours") >= 0:
  216. has_hours = True
  217. elif inc[1].find("days") >= 0:
  218. has_days = True
  219. elif inc[1].find("weeks") >= 0:
  220. has_weeks = True
  221. elif inc[1].find("months") >= 0:
  222. has_months = True
  223. elif inc[1].find("years") >= 0:
  224. has_years = True
  225. else:
  226. core.error(_("Wrong granularity format: %s") % (granularity))
  227. return None
  228. if has_seconds:
  229. pass
  230. elif has_minutes: # Start at 0 seconds
  231. seconds = 0
  232. elif has_hours: # Start at 0 minutes and seconds
  233. seconds = 0
  234. minutes = 0
  235. elif has_days: # Start at 0 hours, minutes and seconds
  236. seconds = 0
  237. minutes = 0
  238. hours = 0
  239. elif has_weeks: # Start at the first day of the week (Monday) at 00:00:00
  240. seconds = 0
  241. minutes = 0
  242. hours = 0
  243. if days > weekday:
  244. days = days - weekday # this needs to be fixed
  245. else:
  246. days = days + weekday # this needs to be fixed
  247. elif has_months: # Start at the first day of the month at 00:00:00
  248. seconds = 0
  249. minutes = 0
  250. hours = 0
  251. days = 1
  252. elif has_years: # Start at the first day of the first month at 00:00:00
  253. seconds = 0
  254. minutes = 0
  255. hours = 0
  256. days = 1
  257. months = 1
  258. dt = copy.copy(mydate)
  259. return dt.replace(year=years, month=months, day=days,
  260. hour=hours, minute=minutes, second=seconds)
  261. ###############################################################################
  262. def compute_datetime_delta(start, end):
  263. """!Return a dictionary with the accumulated delta in year, month, day,
  264. hour, minute and second
  265. Usage:
  266. @code
  267. >>> start = datetime(2001, 1, 1, 00,00,00)
  268. >>> end = datetime(2001, 1, 1, 00,00,00)
  269. >>> compute_datetime_delta(start, end)
  270. {'hour': 0, 'month': 0, 'second': 0, 'max_days': 0, 'year': 0, 'day': 0, 'minute': 0}
  271. >>> start = datetime(2001, 1, 1, 00,00,14)
  272. >>> end = datetime(2001, 1, 1, 00,00,44)
  273. >>> compute_datetime_delta(start, end)
  274. {'hour': 0, 'month': 0, 'second': 30, 'max_days': 0, 'year': 0, 'day': 0, 'minute': 0}
  275. >>> start = datetime(2001, 1, 1, 00,00,44)
  276. >>> end = datetime(2001, 1, 1, 00,01,14)
  277. >>> compute_datetime_delta(start, end)
  278. {'hour': 0, 'month': 0, 'second': 30, 'max_days': 0, 'year': 0, 'day': 0, 'minute': 1}
  279. >>> start = datetime(2001, 1, 1, 00,00,30)
  280. >>> end = datetime(2001, 1, 1, 00,05,30)
  281. >>> compute_datetime_delta(start, end)
  282. {'hour': 0, 'month': 0, 'second': 300, 'max_days': 0, 'year': 0, 'day': 0, 'minute': 5}
  283. >>> start = datetime(2001, 1, 1, 00,00,00)
  284. >>> end = datetime(2001, 1, 1, 00,01,00)
  285. >>> compute_datetime_delta(start, end)
  286. {'hour': 0, 'month': 0, 'second': 0, 'max_days': 0, 'year': 0, 'day': 0, 'minute': 1}
  287. >>> start = datetime(2011,10,31, 00,45,00)
  288. >>> end = datetime(2011,10,31, 01,45,00)
  289. >>> compute_datetime_delta(start, end)
  290. {'hour': 1, 'second': 0, 'max_days': 0, 'year': 0, 'day': 0, 'minute': 60}
  291. >>> start = datetime(2011,10,31, 00,45,00)
  292. >>> end = datetime(2011,10,31, 01,15,00)
  293. >>> compute_datetime_delta(start, end)
  294. {'hour': 1, 'second': 0, 'max_days': 0, 'year': 0, 'day': 0, 'minute': 30}
  295. >>> start = datetime(2011,10,31, 00,45,00)
  296. >>> end = datetime(2011,10,31, 12,15,00)
  297. >>> compute_datetime_delta(start, end)
  298. {'hour': 12, 'second': 0, 'max_days': 0, 'year': 0, 'day': 0, 'minute': 690}
  299. >>> start = datetime(2011,10,31, 00,00,00)
  300. >>> end = datetime(2011,10,31, 01,00,00)
  301. >>> compute_datetime_delta(start, end)
  302. {'hour': 1, 'second': 0, 'max_days': 0, 'year': 0, 'day': 0, 'minute': 0}
  303. >>> start = datetime(2011,10,31, 00,00,00)
  304. >>> end = datetime(2011,11,01, 01,00,00)
  305. >>> compute_datetime_delta(start, end)
  306. {'hour': 25, 'second': 0, 'max_days': 1, 'year': 0, 'day': 1, 'minute': 0}
  307. >>> start = datetime(2011,10,31, 12,00,00)
  308. >>> end = datetime(2011,11,01, 06,00,00)
  309. >>> compute_datetime_delta(start, end)
  310. {'hour': 18, 'second': 0, 'max_days': 0, 'year': 0, 'day': 0, 'minute': 0}
  311. >>> start = datetime(2011,11,01, 00,00,00)
  312. >>> end = datetime(2011,12,01, 01,00,00)
  313. >>> compute_datetime_delta(start, end)
  314. {'hour': 721, 'month': 1, 'second': 0, 'max_days': 30, 'year': 0, 'day': 0, 'minute': 0}
  315. >>> start = datetime(2011,11,01, 00,00,00)
  316. >>> end = datetime(2011,11,05, 00,00,00)
  317. >>> compute_datetime_delta(start, end)
  318. {'hour': 0, 'second': 0, 'max_days': 4, 'year': 0, 'day': 4, 'minute': 0}
  319. >>> start = datetime(2011,10,06, 00,00,00)
  320. >>> end = datetime(2011,11,05, 00,00,00)
  321. >>> compute_datetime_delta(start, end)
  322. {'hour': 0, 'second': 0, 'max_days': 30, 'year': 0, 'day': 30, 'minute': 0}
  323. >>> start = datetime(2011,12,02, 00,00,00)
  324. >>> end = datetime(2012,01,01, 00,00,00)
  325. >>> compute_datetime_delta(start, end)
  326. {'hour': 0, 'second': 0, 'max_days': 30, 'year': 1, 'day': 30, 'minute': 0}
  327. >>> start = datetime(2011,01,01, 00,00,00)
  328. >>> end = datetime(2011,02,01, 00,00,00)
  329. >>> compute_datetime_delta(start, end)
  330. {'hour': 0, 'month': 1, 'second': 0, 'max_days': 31, 'year': 0, 'day': 0, 'minute': 0}
  331. >>> start = datetime(2011,12,01, 00,00,00)
  332. >>> end = datetime(2012,01,01, 00,00,00)
  333. >>> compute_datetime_delta(start, end)
  334. {'hour': 0, 'month': 1, 'second': 0, 'max_days': 31, 'year': 1, 'day': 0, 'minute': 0}
  335. >>> start = datetime(2011,12,01, 00,00,00)
  336. >>> end = datetime(2012,06,01, 00,00,00)
  337. >>> compute_datetime_delta(start, end)
  338. {'hour': 0, 'month': 6, 'second': 0, 'max_days': 183, 'year': 1, 'day': 0, 'minute': 0}
  339. >>> start = datetime(2011,06,01, 00,00,00)
  340. >>> end = datetime(2021,06,01, 00,00,00)
  341. >>> compute_datetime_delta(start, end)
  342. {'hour': 0, 'month': 120, 'second': 0, 'max_days': 3653, 'year': 10, 'day': 0, 'minute': 0}
  343. >>> start = datetime(2011,06,01, 00,00,00)
  344. >>> end = datetime(2012,06,01, 12,00,00)
  345. >>> compute_datetime_delta(start, end)
  346. {'hour': 8796, 'month': 12, 'second': 0, 'max_days': 366, 'year': 1, 'day': 0, 'minute': 0}
  347. >>> start = datetime(2011,06,01, 00,00,00)
  348. >>> end = datetime(2012,06,01, 12,30,00)
  349. >>> compute_datetime_delta(start, end)
  350. {'hour': 8796, 'month': 12, 'second': 0, 'max_days': 366, 'year': 1, 'day': 0, 'minute': 527790}
  351. >>> start = datetime(2011,06,01, 00,00,00)
  352. >>> end = datetime(2012,06,01, 12,00,05)
  353. >>> compute_datetime_delta(start, end)
  354. {'hour': 8796, 'month': 12, 'second': 31665605, 'max_days': 366, 'year': 1, 'day': 0, 'minute': 0}
  355. >>> start = datetime(2011,06,01, 00,00,00)
  356. >>> end = datetime(2012,06,01, 00,30,00)
  357. >>> compute_datetime_delta(start, end)
  358. {'hour': 0, 'month': 12, 'second': 0, 'max_days': 366, 'year': 1, 'day': 0, 'minute': 527070}
  359. >>> start = datetime(2011,06,01, 00,00,00)
  360. >>> end = datetime(2012,06,01, 00,00,05)
  361. >>> compute_datetime_delta(start, end)
  362. {'hour': 0, 'month': 12, 'second': 31622405, 'max_days': 366, 'year': 1, 'day': 0, 'minute': 0}
  363. @endcode
  364. @return A dictionary with year, month, day, hour, minute and second as keys()
  365. """
  366. comp = {}
  367. day_diff = (end - start).days
  368. comp["max_days"] = day_diff
  369. # Date
  370. # Count full years
  371. d = end.year - start.year
  372. comp["year"] = d
  373. # Count full months
  374. if start.month == 1 and end.month == 1:
  375. comp["month"] = 0
  376. elif start.day == 1 and end.day == 1:
  377. d = end.month - start.month
  378. if d < 0:
  379. d = d + 12 * comp["year"]
  380. elif d == 0:
  381. d = 12 * comp["year"]
  382. comp["month"] = d
  383. # Count full days
  384. if start.day == 1 and end.day == 1:
  385. comp["day"] = 0
  386. else:
  387. comp["day"] = day_diff
  388. # Time
  389. # Hours
  390. if start.hour == 0 and end.hour == 0:
  391. comp["hour"] = 0
  392. else:
  393. d = end.hour - start.hour
  394. if d < 0:
  395. d = d + 24 + 24 * day_diff
  396. else:
  397. d = d + 24 * day_diff
  398. comp["hour"] = d
  399. # Minutes
  400. if start.minute == 0 and end.minute == 0:
  401. comp["minute"] = 0
  402. else:
  403. d = end.minute - start.minute
  404. if d != 0:
  405. if comp["hour"]:
  406. d = d + 60 * comp["hour"]
  407. else:
  408. d = d + 24 * 60 * day_diff
  409. elif d == 0:
  410. if comp["hour"]:
  411. d = 60 * comp["hour"]
  412. else:
  413. d = 24 * 60 * day_diff
  414. comp["minute"] = d
  415. # Seconds
  416. if start.second == 0 and end.second == 0:
  417. comp["second"] = 0
  418. else:
  419. d = end.second - start.second
  420. if d != 0:
  421. if comp["minute"]:
  422. d = d + 60 * comp["minute"]
  423. elif comp["hour"]:
  424. d = d + 3600 * comp["hour"]
  425. else:
  426. d = d + 24 * 60 * 60 * day_diff
  427. elif d == 0:
  428. if comp["minute"]:
  429. d = 60 * comp["minute"]
  430. elif comp["hour"]:
  431. d = 3600 * comp["hour"]
  432. else:
  433. d = 24 * 60 * 60 * day_diff
  434. comp["second"] = d
  435. return comp
  436. ###############################################################################
  437. def string_to_datetime(time_string):
  438. """!Convert a string into a datetime object using the dateutil parser.
  439. Return None in case of failure"""
  440. # BC is not supported
  441. if time_string.find("bc") > 0:
  442. core.error("Dates Before Christ are not supported "
  443. "in the temporal database")
  444. return None
  445. try:
  446. dt = parser.parse(time_string)
  447. return dt
  448. except:
  449. return None
  450. ###############################################################################
  451. def datetime_to_grass_datetime_string(dt):
  452. """!Convert a python datetime object into a GRASS datetime string"""
  453. # GRASS datetime month names
  454. month_names = ["", "jan", "feb", "mar", "apr", "may", "jun",
  455. "jul", "aug", "sep", "oct", "nov", "dec"]
  456. # Check for time zone info in the datetime object
  457. if dt.tzinfo is not None:
  458. string = "%.2i %s %.2i %.2i:%.2i:%.2i %+.4i" % (dt.day,
  459. month_names[dt.month], dt.year,
  460. dt.hour, dt.minute, dt.second, dt.tzinfo._offset.seconds / 60)
  461. else:
  462. string = "%.2i %s %.4i %.2i:%.2i:%.2i" % (dt.day, month_names[
  463. dt.month], dt.year, dt.hour, dt.minute, dt.second)
  464. return string
  465. ###############################################################################
  466. if __name__ == "__main__":
  467. import doctest
  468. doctest.testmod()