datetime_math.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 relative_time_to_time_delta(value):
  21. """Convert the double value representing days
  22. into a timedelta object.
  23. """
  24. days = int(value)
  25. seconds = value % 1
  26. seconds = round(seconds * 86400)
  27. return timedelta(days, seconds)
  28. ###############################################################################
  29. def time_delta_to_relative_time(delta):
  30. """Convert the time delta into a
  31. double value, representing days.
  32. """
  33. return float(delta.days) + float(delta.seconds/86400.0)
  34. ###############################################################################
  35. def increment_datetime_by_string(mydate, increment, mult = 1):
  36. """Return a new datetime object incremented with the provided relative dates specified as string.
  37. Additional a multiplier can be specified to multiply the increment bevor adding to the provided datetime object.
  38. @param mydate A datetime object to incremented
  39. @param increment A string providing increment information:
  40. The string may include comma separated values of type seconds, minutes, hours, days, weeks, months and years
  41. 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"
  42. will result in the datetime 2003-02-18 12:05:00
  43. @param mult A multiplier, default is 1
  44. """
  45. if increment:
  46. seconds = 0
  47. minutes = 0
  48. hours = 0
  49. days = 0
  50. weeks = 0
  51. months = 0
  52. years = 0
  53. inclist = []
  54. # Split the increment string
  55. incparts = increment.split(",")
  56. for incpart in incparts:
  57. inclist.append(incpart.strip().split(" "))
  58. for inc in inclist:
  59. if inc[1].find("seconds") >= 0:
  60. seconds = mult * int(inc[0])
  61. elif inc[1].find("minutes") >= 0:
  62. minutes = mult * int(inc[0])
  63. elif inc[1].find("hours") >= 0:
  64. hours = mult * int(inc[0])
  65. elif inc[1].find("days") >= 0:
  66. days = mult * int(inc[0])
  67. elif inc[1].find("weeks") >= 0:
  68. weeks = mult * int(inc[0])
  69. elif inc[1].find("months") >= 0:
  70. months = mult * int(inc[0])
  71. elif inc[1].find("years") >= 0:
  72. years = mult * int(inc[0])
  73. else:
  74. core.error(_("Wrong increment format: %s") % (increment))
  75. return None
  76. return increment_datetime(mydate, years, months, weeks, days, hours, minutes, seconds)
  77. return mydate
  78. ###############################################################################
  79. def increment_datetime(mydate, years=0, months=0, weeks=0, days=0, hours=0, minutes=0, seconds=0):
  80. """Return a new datetime object incremented with the provided relative dates and times"""
  81. tdelta_seconds = timedelta(seconds=seconds)
  82. tdelta_minutes = timedelta(minutes=minutes)
  83. tdelta_hours = timedelta(hours=hours)
  84. tdelta_days = timedelta(days=days)
  85. tdelta_weeks = timedelta(weeks=weeks)
  86. tdelta_months = timedelta(0)
  87. tdelta_years = timedelta(0)
  88. if months > 0:
  89. # Compute the actual number of days in the month to add as timedelta
  90. year = mydate.year
  91. month = mydate.month
  92. all_months = int(months) + int(month)
  93. years_to_add = int(all_months/12.001)
  94. residual_months = all_months - (years_to_add * 12)
  95. # Make a deep copy of the datetime object
  96. dt1 = copy.copy(mydate)
  97. # Make sure the montha starts with a 1
  98. if residual_months == 0:
  99. residual_months = 1
  100. dt1 = dt1.replace(year = year + years_to_add, month = residual_months)
  101. tdelta_months = dt1 - mydate
  102. if years > 0:
  103. # Make a deep copy of the datetime object
  104. dt1 = copy.copy(mydate)
  105. # Compute the number of days
  106. dt1 = dt1.replace(year=mydate.year + int(years))
  107. tdelta_years = dt1 - mydate
  108. return mydate + tdelta_seconds + tdelta_minutes + tdelta_hours + \
  109. tdelta_days + tdelta_weeks + tdelta_months + tdelta_years
  110. ###############################################################################
  111. def adjust_datetime_to_granularity(mydate, granularity):
  112. """Mofiy the datetime object to fit the given granularity """
  113. if granularity:
  114. has_seconds = False
  115. has_minutes = False
  116. has_hours = False
  117. has_days = False
  118. has_weeks = False
  119. has_months = False
  120. has_years = False
  121. seconds = mydate.second
  122. minutes = mydate.minute
  123. hours = mydate.hour
  124. days = mydate.day
  125. weekday = mydate.weekday()
  126. months = mydate.month
  127. years = mydate.year
  128. granlist = []
  129. # Split the increment string
  130. granparts = granularity.split(",")
  131. for granpart in granparts:
  132. granlist.append(granpart.strip().split(" "))
  133. for inc in granlist:
  134. if inc[1].find("seconds") >= 0:
  135. has_seconds = True
  136. elif inc[1].find("minutes") >= 0:
  137. has_minutes = True
  138. elif inc[1].find("hours") >= 0:
  139. has_hours = True
  140. elif inc[1].find("days") >= 0:
  141. has_days = True
  142. elif inc[1].find("weeks") >= 0:
  143. has_weeks = True
  144. elif inc[1].find("months") >= 0:
  145. has_months = True
  146. elif inc[1].find("years") >= 0:
  147. has_years = True
  148. else:
  149. core.error(_("Wrong granularity format: %s") % (granularity))
  150. return None
  151. if has_seconds:
  152. pass
  153. elif has_minutes: # Start at 0 seconds
  154. seconds = 0
  155. elif has_hours: # Start at 0 minutes and seconds
  156. seconds = 0
  157. minutes = 0
  158. elif has_days: # Start at 0 hours, minuts and seconds
  159. seconds = 0
  160. minutes = 0
  161. hours = 0
  162. elif has_weeks: # Start at the first day of the week (monday) at 00:00:00
  163. seconds = 0
  164. minutes = 0
  165. hours = 0
  166. days = days - weekday
  167. elif has_months: # Start at the first day of the month at 00:00:00
  168. seconds = 0
  169. minutes = 0
  170. hours = 0
  171. days = 1
  172. elif has_years: # Start at the first day of the first month at 00:00:00
  173. seconds = 0
  174. minutes = 0
  175. hours = 0
  176. days = 1
  177. months = 1
  178. dt = copy.copy(mydate)
  179. result = dt.replace(year=years, month=months, day=days, hour=hours, minute=minutes, second=seconds)
  180. core.verbose(_("Adjust datetime from %s to %s with granularity %s") % (dt, result, granularity))
  181. return result
  182. ###############################################################################
  183. def compute_datetime_delta(start, end):
  184. """Return a dictionary with the accumulated delta in year, month, day, hour, minute and second
  185. @return A dictionary with year, month, day, hour, minute and second as keys()
  186. """
  187. comp = {}
  188. day_diff = (end - start).days
  189. comp["max_days"] = day_diff
  190. # Date
  191. # Count full years
  192. d = end.year - start.year
  193. comp["year"] = d
  194. # Count full months
  195. if start.month == 1 and end.month == 1:
  196. comp["month"] = 0
  197. elif start.day == 1 and end.day == 1:
  198. d = end.month - start.month
  199. if d < 0:
  200. d = d + 12 * comp["year"]
  201. elif d == 0:
  202. d = 12 * comp["year"]
  203. comp["month"] = d
  204. # Count full days
  205. if start.day == 1 and end.day == 1:
  206. comp["day"] = 0
  207. else:
  208. comp["day"] = day_diff
  209. # Time
  210. # Hours
  211. if start.hour == 0 and end.hour == 0:
  212. comp["hour"] = 0
  213. else:
  214. d = end.hour - start.hour
  215. if d < 0:
  216. d = d + 24 + 24 * day_diff
  217. else:
  218. d = d + 24 * day_diff
  219. comp["hour"] = d
  220. # Minutes
  221. if start.minute == 0 and end.minute == 0:
  222. comp["minute"] = 0
  223. else:
  224. d = end.minute - start.minute
  225. if d != 0:
  226. if comp["hour"]:
  227. d = d + 60 * comp["hour"]
  228. else:
  229. d = d + 24 * 60 * day_diff
  230. elif d == 0:
  231. if comp["hour"]:
  232. d = 60* comp["hour"]
  233. else:
  234. d = 24 * 60 * day_diff
  235. comp["minute"] = d
  236. # Seconds
  237. if start.second == 0 and end.second == 0:
  238. comp["second"] = 0
  239. else:
  240. d = end.second - start.second
  241. if d != 0:
  242. if comp["minute"]:
  243. d = d + 60* comp["minute"]
  244. elif comp["hour"]:
  245. d = d + 3600* comp["hour"]
  246. else:
  247. d = d + 24 * 60 * 60 * day_diff
  248. elif d == 0:
  249. if comp["minute"]:
  250. d = 60* comp["minute"]
  251. elif comp["hour"]:
  252. d = 3600 * comp["hour"]
  253. else:
  254. d = 24 * 60 * 60 * day_diff
  255. comp["second"] = d
  256. return comp