datetime_math.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. """!@package grass.temporal
  2. @brief GRASS Python scripting module (temporal GIS functions)
  3. Temporal GIS datetime math functions to be used in Python scripts.
  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. from dateutil import parser
  20. DAY_IN_SECONDS = 86400
  21. SECOND_AS_DAY = 1.1574074074074073e-05
  22. ###############################################################################
  23. def relative_time_to_time_delta(value):
  24. """!Convert the double value representing days
  25. into a timedelta object.
  26. """
  27. days = int(value)
  28. seconds = value % 1
  29. seconds = round(seconds * DAY_IN_SECONDS)
  30. return timedelta(days, seconds)
  31. ###############################################################################
  32. def time_delta_to_relative_time(delta):
  33. """!Convert the time delta into a
  34. double value, representing days.
  35. """
  36. return float(delta.days) + float(delta.seconds * SECOND_AS_DAY)
  37. ###############################################################################
  38. def increment_datetime_by_string(mydate, increment, mult = 1):
  39. """!Return a new datetime object incremented with the provided
  40. relative dates specified as string.
  41. Additional a multiplier can be specified to multiply the increment
  42. before adding to the provided datetime object.
  43. @param mydate A datetime object to incremented
  44. @param increment A string providing increment information:
  45. The string may include comma separated values of type seconds,
  46. minutes, hours, days, weeks, months and years
  47. Example: Increment the datetime 2001-01-01 00:00:00
  48. with "60 seconds, 4 minutes, 12 hours, 10 days, 1 weeks, 5 months, 1 years"
  49. will result in the datetime 2003-02-18 12:05:00
  50. @param mult A multiplier, default is 1
  51. """
  52. if increment:
  53. seconds = 0
  54. minutes = 0
  55. hours = 0
  56. days = 0
  57. weeks = 0
  58. months = 0
  59. years = 0
  60. inclist = []
  61. # Split the increment string
  62. incparts = increment.split(",")
  63. for incpart in incparts:
  64. inclist.append(incpart.strip().split(" "))
  65. for inc in inclist:
  66. if len(inc) < 2:
  67. core.error(_("Wrong increment format: %s") % (increment))
  68. return None
  69. if inc[1].find("seconds") >= 0:
  70. seconds = mult * int(inc[0])
  71. elif inc[1].find("minutes") >= 0:
  72. minutes = mult * int(inc[0])
  73. elif inc[1].find("hours") >= 0:
  74. hours = mult * int(inc[0])
  75. elif inc[1].find("days") >= 0:
  76. days = mult * int(inc[0])
  77. elif inc[1].find("weeks") >= 0:
  78. weeks = mult * int(inc[0])
  79. elif inc[1].find("months") >= 0:
  80. months = mult * int(inc[0])
  81. elif inc[1].find("years") >= 0:
  82. years = mult * int(inc[0])
  83. else:
  84. core.error(_("Wrong increment format: %s") % (increment))
  85. return None
  86. return increment_datetime(mydate, years, months, weeks, days, hours, minutes, seconds)
  87. return mydate
  88. ###############################################################################
  89. def increment_datetime(mydate, years=0, months=0, weeks=0, days=0, hours=0, minutes=0, seconds=0):
  90. """!Return a new datetime object incremented with the provided relative dates and times"""
  91. tdelta_seconds = timedelta(seconds=seconds)
  92. tdelta_minutes = timedelta(minutes=minutes)
  93. tdelta_hours = timedelta(hours=hours)
  94. tdelta_days = timedelta(days=days)
  95. tdelta_weeks = timedelta(weeks=weeks)
  96. tdelta_months = timedelta(0)
  97. tdelta_years = timedelta(0)
  98. if months > 0:
  99. # Compute the actual number of days in the month to add as timedelta
  100. year = mydate.year
  101. month = mydate.month
  102. all_months = int(months) + int(month)
  103. years_to_add = int(all_months/12.001)
  104. residual_months = all_months - (years_to_add * 12)
  105. # Make a deep copy of the datetime object
  106. dt1 = copy.copy(mydate)
  107. # Make sure the month starts with a 1
  108. if residual_months == 0:
  109. residual_months = 1
  110. dt1 = dt1.replace(year = year + years_to_add, month = residual_months)
  111. tdelta_months = dt1 - mydate
  112. if years > 0:
  113. # Make a deep copy of the datetime object
  114. dt1 = copy.copy(mydate)
  115. # Compute the number of days
  116. dt1 = dt1.replace(year=mydate.year + int(years))
  117. tdelta_years = dt1 - mydate
  118. return mydate + tdelta_seconds + tdelta_minutes + tdelta_hours + \
  119. tdelta_days + tdelta_weeks + tdelta_months + tdelta_years
  120. ###############################################################################
  121. def adjust_datetime_to_granularity(mydate, granularity):
  122. """!Mofiy the datetime object to fit the given granularity """
  123. if granularity:
  124. has_seconds = False
  125. has_minutes = False
  126. has_hours = False
  127. has_days = False
  128. has_weeks = False
  129. has_months = False
  130. has_years = False
  131. seconds = mydate.second
  132. minutes = mydate.minute
  133. hours = mydate.hour
  134. days = mydate.day
  135. weekday = mydate.weekday()
  136. months = mydate.month
  137. years = mydate.year
  138. granlist = []
  139. # Split the increment string
  140. granparts = granularity.split(",")
  141. for granpart in granparts:
  142. granlist.append(granpart.strip().split(" "))
  143. for inc in granlist:
  144. if inc[1].find("seconds") >= 0:
  145. has_seconds = True
  146. elif inc[1].find("minutes") >= 0:
  147. has_minutes = True
  148. elif inc[1].find("hours") >= 0:
  149. has_hours = True
  150. elif inc[1].find("days") >= 0:
  151. has_days = True
  152. elif inc[1].find("weeks") >= 0:
  153. has_weeks = True
  154. elif inc[1].find("months") >= 0:
  155. has_months = True
  156. elif inc[1].find("years") >= 0:
  157. has_years = True
  158. else:
  159. core.error(_("Wrong granularity format: %s") % (granularity))
  160. return None
  161. if has_seconds:
  162. pass
  163. elif has_minutes: # Start at 0 seconds
  164. seconds = 0
  165. elif has_hours: # Start at 0 minutes and seconds
  166. seconds = 0
  167. minutes = 0
  168. elif has_days: # Start at 0 hours, minutes and seconds
  169. seconds = 0
  170. minutes = 0
  171. hours = 0
  172. elif has_weeks: # Start at the first day of the week (Monday) at 00:00:00
  173. seconds = 0
  174. minutes = 0
  175. hours = 0
  176. if days > weekday:
  177. days = days - weekday # this needs to be fixed
  178. else:
  179. days = days + weekday # this needs to be fixed
  180. elif has_months: # Start at the first day of the month at 00:00:00
  181. seconds = 0
  182. minutes = 0
  183. hours = 0
  184. days = 1
  185. elif has_years: # Start at the first day of the first month at 00:00:00
  186. seconds = 0
  187. minutes = 0
  188. hours = 0
  189. days = 1
  190. months = 1
  191. dt = copy.copy(mydate)
  192. result = dt.replace(year=years, month=months, day=days, hour=hours, minute=minutes, second=seconds)
  193. core.verbose(_("Adjust datetime from %s to %s with granularity %s") % (dt, result, granularity))
  194. return result
  195. ###############################################################################
  196. def compute_datetime_delta(start, end):
  197. """!Return a dictionary with the accumulated delta in year, month, day, hour, minute and second
  198. @return A dictionary with year, month, day, hour, minute and second as keys()
  199. """
  200. comp = {}
  201. day_diff = (end - start).days
  202. comp["max_days"] = day_diff
  203. # Date
  204. # Count full years
  205. d = end.year - start.year
  206. comp["year"] = d
  207. # Count full months
  208. if start.month == 1 and end.month == 1:
  209. comp["month"] = 0
  210. elif start.day == 1 and end.day == 1:
  211. d = end.month - start.month
  212. if d < 0:
  213. d = d + 12 * comp["year"]
  214. elif d == 0:
  215. d = 12 * comp["year"]
  216. comp["month"] = d
  217. # Count full days
  218. if start.day == 1 and end.day == 1:
  219. comp["day"] = 0
  220. else:
  221. comp["day"] = day_diff
  222. # Time
  223. # Hours
  224. if start.hour == 0 and end.hour == 0:
  225. comp["hour"] = 0
  226. else:
  227. d = end.hour - start.hour
  228. if d < 0:
  229. d = d + 24 + 24 * day_diff
  230. else:
  231. d = d + 24 * day_diff
  232. comp["hour"] = d
  233. # Minutes
  234. if start.minute == 0 and end.minute == 0:
  235. comp["minute"] = 0
  236. else:
  237. d = end.minute - start.minute
  238. if d != 0:
  239. if comp["hour"]:
  240. d = d + 60 * comp["hour"]
  241. else:
  242. d = d + 24 * 60 * day_diff
  243. elif d == 0:
  244. if comp["hour"]:
  245. d = 60* comp["hour"]
  246. else:
  247. d = 24 * 60 * day_diff
  248. comp["minute"] = d
  249. # Seconds
  250. if start.second == 0 and end.second == 0:
  251. comp["second"] = 0
  252. else:
  253. d = end.second - start.second
  254. if d != 0:
  255. if comp["minute"]:
  256. d = d + 60* comp["minute"]
  257. elif comp["hour"]:
  258. d = d + 3600* comp["hour"]
  259. else:
  260. d = d + 24 * 60 * 60 * day_diff
  261. elif d == 0:
  262. if comp["minute"]:
  263. d = 60* comp["minute"]
  264. elif comp["hour"]:
  265. d = 3600 * comp["hour"]
  266. else:
  267. d = 24 * 60 * 60 * day_diff
  268. comp["second"] = d
  269. return comp
  270. ###############################################################################
  271. def string_to_datetime(time_string):
  272. """!Convert a string into a datetime object using the dateutil parser. Return None in case of failure"""
  273. # BC is not supported
  274. if time_string.find("bc") > 0:
  275. core.error("Dates Before Christ are not supported in the temporal database")
  276. return None
  277. try:
  278. dt = parser.parse(time_string)
  279. return dt
  280. except:
  281. return None
  282. ###############################################################################
  283. def datetime_to_grass_datetime_string(dt):
  284. """!Convert a python datetime object into a GRASS datetime string"""
  285. # GRASS datetime month names
  286. month_names = ["", "jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"]
  287. # Check for time zone info in the datetime object
  288. if dt.tzinfo != None:
  289. string = "%.2i %s %.2i %.2i:%.2i:%.2i %+.4i"%(dt.day, month_names[dt.month], dt.year, \
  290. dt.hour, dt.minute, dt.second, dt.tzinfo._offset.seconds/60)
  291. else:
  292. string = "%.2i %s %.4i %.2i:%.2i:%.2i"%(dt.day, month_names[dt.month], dt.year, dt.hour, dt.minute, dt.second)
  293. return string