datetime_math.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. """!@package grass.temporal
  2. @brief GRASS Python scripting module (temporal GIS functions)
  3. Temporal GIS datetime math functions to be used in Python sripts.
  4. Usage:
  5. @code
  6. import grass.temporal as tgis
  7. tgis.increment_datetime_by_string(mydate, "3 month, 2 hours")
  8. ...
  9. @endcode
  10. (C) 2008-2011 by the GRASS Development Team
  11. This program is free software under the GNU General Public
  12. License (>=v2). Read the file COPYING that comes with GRASS
  13. for details.
  14. @author Soeren Gebbert
  15. """
  16. from datetime import datetime, date, time, timedelta
  17. import grass.script.core as core
  18. import copy
  19. ###############################################################################
  20. def datetime_delta_to_double(dt1, dt2):
  21. """Compute the the dfference dt2 - dt1 and convert the time delta into a
  22. double value, representing days.
  23. """
  24. delta = dt2 - dt1
  25. return float(delta.days) + float(delta.seconds/86400.0)
  26. ###############################################################################
  27. def increment_datetime_by_string(mydate, increment, mult = 1):
  28. """Return a new datetime object incremented with the provided relative dates specified as string.
  29. Additional a multiplier can be specified to multiply the increment bevor adding to the provided datetime object.
  30. @param mydate A datetime object to incremented
  31. @param increment A string providing increment information:
  32. The string may include comma separated values of type seconds, minutes, hours, days, weeks, months and years
  33. Example: Increment the datetime 2001-01-01 00:00:00 with "60 seconds, 4 minutes, 12 hours, 10 days, 1 weeks, 5 months, 1 years"
  34. will result in the datetime 2003-02-18 12:05:00
  35. @param mult A multiplier, default is 1
  36. """
  37. if increment:
  38. seconds = 0
  39. minutes = 0
  40. hours = 0
  41. days = 0
  42. weeks = 0
  43. months = 0
  44. years = 0
  45. inclist = []
  46. # Split the increment string
  47. incparts = increment.split(",")
  48. for incpart in incparts:
  49. inclist.append(incpart.strip().split(" "))
  50. for inc in inclist:
  51. if inc[1].find("seconds") >= 0:
  52. seconds = mult * int(inc[0])
  53. elif inc[1].find("minutes") >= 0:
  54. minutes = mult * int(inc[0])
  55. elif inc[1].find("hours") >= 0:
  56. hours = mult * int(inc[0])
  57. elif inc[1].find("days") >= 0:
  58. days = mult * int(inc[0])
  59. elif inc[1].find("weeks") >= 0:
  60. weeks = mult * int(inc[0])
  61. elif inc[1].find("months") >= 0:
  62. months = mult * int(inc[0])
  63. elif inc[1].find("years") >= 0:
  64. years = mult * int(inc[0])
  65. else:
  66. core.error(_("Wrong increment format: %s") % (increment))
  67. return None
  68. return increment_datetime(mydate, years, months, weeks, days, hours, minutes, seconds)
  69. return mydate
  70. ###############################################################################
  71. def test_increment_datetime_by_string():
  72. # First test
  73. print "# Test 1"
  74. dt = datetime(2001, 9, 1, 0, 0, 0)
  75. string = "60 seconds, 4 minutes, 12 hours, 10 days, 1 weeks, 5 months, 1 years"
  76. dt1 = datetime(2003,2,18,12,5,0)
  77. dt2 = increment_datetime_by_string(dt, string)
  78. print dt
  79. print dt2
  80. delta = dt1 -dt2
  81. if delta.days != 0 or delta.seconds != 0:
  82. core.error("increment computation is wrong %s" % (delta))
  83. # Second test
  84. print "# Test 2"
  85. dt = datetime(2001, 11, 1, 0, 0, 0)
  86. string = "1 months"
  87. dt1 = datetime(2001,12,1)
  88. dt2 = increment_datetime_by_string(dt, string)
  89. print dt
  90. print dt2
  91. delta = dt1 -dt2
  92. if delta.days != 0 or delta.seconds != 0:
  93. core.error("increment computation is wrong %s" % (delta))
  94. # Third test
  95. print "# Test 3"
  96. dt = datetime(2001, 11, 1, 0, 0, 0)
  97. string = "13 months"
  98. dt1 = datetime(2002,12,1)
  99. dt2 = increment_datetime_by_string(dt, string)
  100. print dt
  101. print dt2
  102. delta = dt1 -dt2
  103. if delta.days != 0 or delta.seconds != 0:
  104. core.error("increment computation is wrong %s" % (delta))
  105. # 4. test
  106. print "# Test 4"
  107. dt = datetime(2001, 1, 1, 0, 0, 0)
  108. string = "72 months"
  109. dt1 = datetime(2007,1,1)
  110. dt2 = increment_datetime_by_string(dt, string)
  111. print dt
  112. print dt2
  113. delta = dt1 -dt2
  114. if delta.days != 0 or delta.seconds != 0:
  115. core.error("increment computation is wrong %s" % (delta))
  116. ###############################################################################
  117. def increment_datetime(mydate, years=0, months=0, weeks=0, days=0, hours=0, minutes=0, seconds=0):
  118. """Return a new datetime object incremented with the provided relative dates and times"""
  119. tdelta_seconds = timedelta(seconds=seconds)
  120. tdelta_minutes = timedelta(minutes=minutes)
  121. tdelta_hours = timedelta(hours=hours)
  122. tdelta_days = timedelta(days=days)
  123. tdelta_weeks = timedelta(weeks=weeks)
  124. tdelta_months = timedelta(0)
  125. tdelta_years = timedelta(0)
  126. if months > 0:
  127. # Compute the actual number of days in the month to add as timedelta
  128. year = mydate.year
  129. month = mydate.month
  130. all_months = int(months) + int(month)
  131. years_to_add = int(all_months/12.001)
  132. residual_months = all_months - (years_to_add * 12)
  133. # Make a deep copy of the datetime object
  134. dt1 = copy.copy(mydate)
  135. # Make sure the montha starts with a 1
  136. if residual_months == 0:
  137. residual_months = 1
  138. dt1 = dt1.replace(year = year + years_to_add, month = residual_months)
  139. tdelta_months = dt1 - mydate
  140. if years > 0:
  141. # Make a deep copy of the datetime object
  142. dt1 = copy.copy(mydate)
  143. # Compute the number of days
  144. dt1 = dt1.replace(year=mydate.year + int(years))
  145. tdelta_years = dt1 - mydate
  146. return mydate + tdelta_seconds + tdelta_minutes + tdelta_hours + \
  147. tdelta_days + tdelta_weeks + tdelta_months + tdelta_years
  148. ###############################################################################
  149. def adjust_datetime_to_granularity(mydate, granularity):
  150. """Mofiy the datetime object to fit the given granularity """
  151. if granularity:
  152. has_seconds = False
  153. has_minutes = False
  154. has_hours = False
  155. has_days = False
  156. has_weeks = False
  157. has_months = False
  158. has_years = False
  159. seconds = mydate.second
  160. minutes = mydate.minute
  161. hours = mydate.hour
  162. days = mydate.day
  163. weekday = mydate.weekday()
  164. months = mydate.month
  165. years = mydate.year
  166. granlist = []
  167. # Split the increment string
  168. granparts = granularity.split(",")
  169. for granpart in granparts:
  170. granlist.append(granpart.strip().split(" "))
  171. for inc in granlist:
  172. if inc[1].find("seconds") >= 0:
  173. has_seconds = True
  174. elif inc[1].find("minutes") >= 0:
  175. has_minutes = True
  176. elif inc[1].find("hours") >= 0:
  177. has_hours = True
  178. elif inc[1].find("days") >= 0:
  179. has_days = True
  180. elif inc[1].find("weeks") >= 0:
  181. has_weeks = True
  182. elif inc[1].find("months") >= 0:
  183. has_months = True
  184. elif inc[1].find("years") >= 0:
  185. has_years = True
  186. else:
  187. core.error(_("Wrong granularity format: %s") % (granularity))
  188. return None
  189. if has_seconds:
  190. pass
  191. elif has_minutes: # Start at 0 seconds
  192. seconds = 0
  193. elif has_hours: # Start at 0 minutes and seconds
  194. seconds = 0
  195. minutes = 0
  196. elif has_days: # Start at 0 hours, minuts and seconds
  197. seconds = 0
  198. minutes = 0
  199. hours = 0
  200. elif has_weeks: # Start at the first day of the week (monday) at 00:00:00
  201. seconds = 0
  202. minutes = 0
  203. hours = 0
  204. days = days - weekday
  205. elif has_months: # Start at the first day of the month at 00:00:00
  206. seconds = 0
  207. minutes = 0
  208. hours = 0
  209. days = 1
  210. elif has_years: # Start at the first day of the first month at 00:00:00
  211. seconds = 0
  212. minutes = 0
  213. hours = 0
  214. days = 1
  215. months = 1
  216. dt = copy.copy(mydate)
  217. result = dt.replace(year=years, month=months, day=days, hour=hours, minute=minutes, second=seconds)
  218. core.verbose(_("Adjust datetime from %s to %s with granularity %s") % (dt, result, granularity))
  219. return result
  220. def test_adjust_datetime_to_granularity():
  221. # First test
  222. print "Test 1"
  223. dt = datetime(2001, 8, 8, 12,30,30)
  224. result = adjust_datetime_to_granularity(dt, "5 seconds")
  225. correct = datetime(2001, 8, 8, 12,30,30)
  226. delta = correct - result
  227. if delta.days != 0 or delta.seconds != 0:
  228. core.error("Granularity adjustment computation is wrong %s" % (delta))
  229. # Second test
  230. print "Test 2"
  231. result = adjust_datetime_to_granularity(dt, "20 minutes")
  232. correct = datetime(2001, 8, 8, 12,30,00)
  233. delta = correct - result
  234. if delta.days != 0 or delta.seconds != 0:
  235. core.error("Granularity adjustment computation is wrong %s" % (delta))
  236. # Third test
  237. print "Test 2"
  238. result = adjust_datetime_to_granularity(dt, "20 minutes")
  239. correct = datetime(2001, 8, 8, 12,30,00)
  240. delta = correct - result
  241. if delta.days != 0 or delta.seconds != 0:
  242. core.error("Granularity adjustment computation is wrong %s" % (delta))
  243. # 4. test
  244. print "Test 4"
  245. result = adjust_datetime_to_granularity(dt, "3 hours")
  246. correct = datetime(2001, 8, 8, 12,00,00)
  247. delta = correct - result
  248. if delta.days != 0 or delta.seconds != 0:
  249. core.error("Granularity adjustment computation is wrong %s" % (delta))
  250. # 5. test
  251. print "Test 5"
  252. result = adjust_datetime_to_granularity(dt, "5 days")
  253. correct = datetime(2001, 8, 8, 00,00,00)
  254. delta = correct - result
  255. if delta.days != 0 or delta.seconds != 0:
  256. core.error("Granularity adjustment computation is wrong %s" % (delta))
  257. # 6. test
  258. print "Test 6"
  259. result = adjust_datetime_to_granularity(dt, "2 weeks")
  260. correct = datetime(2001, 8, 6, 00,00,00)
  261. delta = correct - result
  262. if delta.days != 0 or delta.seconds != 0:
  263. core.error("Granularity adjustment computation is wrong %s" % (delta))
  264. # 7. test
  265. print "Test 7"
  266. result = adjust_datetime_to_granularity(dt, "6 months")
  267. correct = datetime(2001, 8, 1, 00,00,00)
  268. delta = correct - result
  269. if delta.days != 0 or delta.seconds != 0:
  270. core.error("Granularity adjustment computation is wrong %s" % (delta))
  271. # 8. test
  272. print "Test 8"
  273. result = adjust_datetime_to_granularity(dt, "2 years")
  274. correct = datetime(2001, 1, 1, 00,00,00)
  275. delta = correct - result
  276. if delta.days != 0 or delta.seconds != 0:
  277. core.error("Granularity adjustment computation is wrong %s" % (delta))
  278. # 9. test
  279. print "Test 9"
  280. result = adjust_datetime_to_granularity(dt, "2 years, 3 months, 5 days, 3 hours, 3 minutes, 2 seconds")
  281. correct = datetime(2001, 8, 8, 12,30,30)
  282. delta = correct - result
  283. if delta.days != 0 or delta.seconds != 0:
  284. core.error("Granularity adjustment computation is wrong %s" % (delta))
  285. # 10. test
  286. print "Test 10"
  287. result = adjust_datetime_to_granularity(dt, "3 months, 5 days, 3 minutes")
  288. correct = datetime(2001, 8, 8, 12,30,00)
  289. delta = correct - result
  290. if delta.days != 0 or delta.seconds != 0:
  291. core.error("Granularity adjustment computation is wrong %s" % (delta))
  292. # 11. test
  293. print "Test 11"
  294. result = adjust_datetime_to_granularity(dt, "3 weeks, 5 days")
  295. correct = datetime(2001, 8, 8, 00,00,00)
  296. delta = correct - result
  297. if delta.days != 0 or delta.seconds != 0:
  298. core.error("Granularity adjustment computation is wrong %s" % (delta))