temporal_granularity.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. """
  2. Functions to compute the temporal granularity of a map list
  3. Usage:
  4. .. code-block:: python
  5. import grass.temporal as tgis
  6. tgis.compute_relative_time_granularity(maps)
  7. (C) 2012-2013 by the GRASS Development Team
  8. This program is free software under the GNU General Public
  9. License (>=v2). Read the file COPYING that comes with GRASS
  10. for details.
  11. :authors: Soeren Gebbert
  12. """
  13. from abstract_dataset import *
  14. from datetime_math import *
  15. ###############################################################################
  16. def check_granularity_string(granularity, temporal_type):
  17. """Check if the granularity string is valid
  18. :param granularity: The granularity string
  19. :param temporal_type: The temporal type of the granularity relative or
  20. absolute
  21. :return: True if valid, False if invalid
  22. .. code-block:: python
  23. >>> check_granularity_string("1 year", "absolute")
  24. True
  25. >>> check_granularity_string("1 month", "absolute")
  26. True
  27. >>> check_granularity_string("1 day", "absolute")
  28. True
  29. >>> check_granularity_string("1 minute", "absolute")
  30. True
  31. >>> check_granularity_string("1 hour", "absolute")
  32. True
  33. >>> check_granularity_string("1 second", "absolute")
  34. True
  35. >>> check_granularity_string("5 months", "absolute")
  36. True
  37. >>> check_granularity_string("5 days", "absolute")
  38. True
  39. >>> check_granularity_string("5 minutes", "absolute")
  40. True
  41. >>> check_granularity_string("5 years", "absolute")
  42. True
  43. >>> check_granularity_string("5 hours", "absolute")
  44. True
  45. >>> check_granularity_string("2 seconds", "absolute")
  46. True
  47. >>> check_granularity_string("1 secondo", "absolute")
  48. False
  49. >>> check_granularity_string("bla second", "absolute")
  50. False
  51. >>> check_granularity_string("bla", "absolute")
  52. False
  53. >>> check_granularity_string(1, "relative")
  54. True
  55. >>> check_granularity_string("bla", "relative")
  56. False
  57. """
  58. temporal_type
  59. if granularity is None:
  60. return False
  61. if temporal_type == "absolute":
  62. try:
  63. num, unit = granularity.split(" ")
  64. except:
  65. return False
  66. if unit not in ["second", "seconds", "minute", "minutes", "hour",
  67. "hours", "day", "days", "week", "weeks", "month",
  68. "months", "year", "years"]:
  69. return False
  70. try:
  71. integer = int(num)
  72. except:
  73. return False
  74. elif temporal_type == "relative":
  75. try:
  76. integer = int(granularity)
  77. except:
  78. return False
  79. else:
  80. return False
  81. return True
  82. ###############################################################################
  83. def compute_relative_time_granularity(maps):
  84. """Compute the relative time granularity
  85. Attention: The computation of the granularity
  86. is only correct in case of not overlapping intervals.
  87. Hence a correct temporal topology is required for computation.
  88. :param maps: a ordered by start_time list of map objects
  89. :return: An integer
  90. .. code-block:: python
  91. >>> import grass.temporal as tgis
  92. >>> tgis.init()
  93. >>> maps = []
  94. >>> for i in range(5):
  95. ... map = tgis.RasterDataset("a%i@P"%i)
  96. ... check = map.set_relative_time(i,i + 1,"seconds")
  97. ... if check:
  98. ... maps.append(map)
  99. >>> tgis.compute_relative_time_granularity(maps)
  100. 1
  101. >>> maps = []
  102. >>> count = 0
  103. >>> timelist = ((0,3), (3,6), (6,9))
  104. >>> for t in timelist:
  105. ... map = tgis.RasterDataset("a%i@P"%count)
  106. ... check = map.set_relative_time(t[0],t[1],"years")
  107. ... if check:
  108. ... maps.append(map)
  109. ... count += 1
  110. >>> tgis.compute_relative_time_granularity(maps)
  111. 3
  112. >>> maps = []
  113. >>> count = 0
  114. >>> timelist = ((0,3), (4,6), (8,11))
  115. >>> for t in timelist:
  116. ... map = tgis.RasterDataset("a%i@P"%count)
  117. ... check = map.set_relative_time(t[0],t[1],"years")
  118. ... if check:
  119. ... maps.append(map)
  120. ... count += 1
  121. >>> tgis.compute_relative_time_granularity(maps)
  122. 1
  123. >>> maps = []
  124. >>> count = 0
  125. >>> timelist = ((0,8), (2,6), (5,9))
  126. >>> for t in timelist:
  127. ... map = tgis.RasterDataset("a%i@P"%count)
  128. ... check = map.set_relative_time(t[0],t[1],"months")
  129. ... if check:
  130. ... maps.append(map)
  131. ... count += 1
  132. >>> tgis.compute_relative_time_granularity(maps)
  133. 4
  134. >>> maps = []
  135. >>> count = 0
  136. >>> timelist = ((0,8), (8,12), (12,18))
  137. >>> for t in timelist:
  138. ... map = tgis.RasterDataset("a%i@P"%count)
  139. ... check = map.set_relative_time(t[0],t[1],"days")
  140. ... if check:
  141. ... maps.append(map)
  142. ... count += 1
  143. >>> tgis.compute_relative_time_granularity(maps)
  144. 2
  145. >>> maps = []
  146. >>> count = 0
  147. >>> timelist = ((0,None), (8,None), (12,None), (24,None))
  148. >>> for t in timelist:
  149. ... map = tgis.RasterDataset("a%i@P"%count)
  150. ... check = map.set_relative_time(t[0],t[1],"minutes")
  151. ... if check:
  152. ... maps.append(map)
  153. ... count += 1
  154. >>> tgis.compute_relative_time_granularity(maps)
  155. 4
  156. >>> maps = []
  157. >>> count = 0
  158. >>> timelist = ((0,None), (8,14), (18,None), (24,None))
  159. >>> for t in timelist:
  160. ... map = tgis.RasterDataset("a%i@P"%count)
  161. ... check = map.set_relative_time(t[0],t[1],"hours")
  162. ... if check:
  163. ... maps.append(map)
  164. ... count += 1
  165. >>> tgis.compute_relative_time_granularity(maps)
  166. 2
  167. >>> maps = []
  168. >>> count = 0
  169. >>> timelist = ((0,21),)
  170. >>> for t in timelist:
  171. ... map = tgis.RasterDataset("a%i@P"%count)
  172. ... check = map.set_relative_time(t[0],t[1],"hours")
  173. ... if check:
  174. ... maps.append(map)
  175. ... count += 1
  176. >>> tgis.compute_relative_time_granularity(maps)
  177. 21
  178. """
  179. # The interval time must be scaled to days resolution
  180. granularity = None
  181. delta = []
  182. # First we compute the timedelta of the intervals
  183. for map in maps:
  184. start, end = map.get_temporal_extent_as_tuple()
  185. if (start == 0 or start) and end:
  186. t = abs(end - start)
  187. delta.append(int(t))
  188. # Compute the timedelta of the gaps
  189. for i in range(len(maps)):
  190. if i < len(maps) - 1:
  191. relation = maps[i + 1].temporal_relation(maps[i])
  192. if relation == "after":
  193. start1, end1 = maps[i].get_temporal_extent_as_tuple()
  194. start2, end2 = maps[i + 1].get_temporal_extent_as_tuple()
  195. # Gaps are between intervals, intervals and
  196. # points, points and points
  197. if end1 and start2:
  198. t = abs(end1 - start2)
  199. delta.append(int(t))
  200. if not end1 and start2:
  201. t = abs(start1 - start2)
  202. delta.append(int(t))
  203. delta.sort()
  204. ulist = list(set(delta))
  205. if len(ulist) > 1:
  206. # Find greatest common divisor
  207. granularity = gcd_list(ulist)
  208. elif len(ulist) == 1:
  209. granularity = ulist[0]
  210. else:
  211. granularity = 0
  212. return granularity
  213. ###############################################################################
  214. def compute_absolute_time_granularity(maps):
  215. """Compute the absolute time granularity
  216. Attention: The computation of the granularity
  217. is only correct in case of not overlapping intervals.
  218. Hence a correct temporal topology is required for computation.
  219. The computed granularity is returned as number of seconds or minutes
  220. or hours or days or months or years.
  221. :param maps: a ordered by start_time list of map objects
  222. :return: The temporal topology as string "integer unit"
  223. .. code-block:: python
  224. >>> import grass.temporal as tgis
  225. >>> import datetime
  226. >>> dt = datetime.datetime
  227. >>> tgis.init()
  228. >>> maps = []
  229. >>> count = 0
  230. >>> timelist = ((dt(2000,01,01),None), (dt(2000,02,01),None))
  231. >>> for t in timelist:
  232. ... map = tgis.RasterDataset("a%i@P"%count)
  233. ... check = map.set_absolute_time(t[0],t[1])
  234. ... if check:
  235. ... maps.append(map)
  236. ... count += 1
  237. >>> tgis.compute_absolute_time_granularity(maps)
  238. '1 month'
  239. >>> maps = []
  240. >>> count = 0
  241. >>> timelist = ((dt(2000,01,01),None), (dt(2000,01,02),None), (dt(2000,01,03),None))
  242. >>> for t in timelist:
  243. ... map = tgis.RasterDataset("a%i@P"%count)
  244. ... check = map.set_absolute_time(t[0],t[1])
  245. ... if check:
  246. ... maps.append(map)
  247. ... count += 1
  248. >>> tgis.compute_absolute_time_granularity(maps)
  249. '1 day'
  250. >>> maps = []
  251. >>> count = 0
  252. >>> timelist = ((dt(2000,01,01),None), (dt(2000,01,02),None), (dt(2000,05,04,0,5,30),None))
  253. >>> for t in timelist:
  254. ... map = tgis.RasterDataset("a%i@P"%count)
  255. ... check = map.set_absolute_time(t[0],t[1])
  256. ... if check:
  257. ... maps.append(map)
  258. ... count += 1
  259. >>> tgis.compute_absolute_time_granularity(maps)
  260. '30 seconds'
  261. >>> maps = []
  262. >>> count = 0
  263. >>> timelist = ((dt(2000,01,01),dt(2000,05,02)), (dt(2000,05,04,2),None))
  264. >>> for t in timelist:
  265. ... map = tgis.RasterDataset("a%i@P"%count)
  266. ... check = map.set_absolute_time(t[0],t[1])
  267. ... if check:
  268. ... maps.append(map)
  269. ... count += 1
  270. >>> tgis.compute_absolute_time_granularity(maps)
  271. '2 hours'
  272. >>> maps = []
  273. >>> count = 0
  274. >>> timelist = ((dt(2000,01,01),dt(2000,02,01)), (dt(2005,05,04,12),dt(2007,05,20,6)))
  275. >>> for t in timelist:
  276. ... map = tgis.RasterDataset("a%i@P"%count)
  277. ... check = map.set_absolute_time(t[0],t[1])
  278. ... if check:
  279. ... maps.append(map)
  280. ... count += 1
  281. >>> tgis.compute_absolute_time_granularity(maps)
  282. '6 hours'
  283. """
  284. has_seconds = False
  285. has_minutes = False
  286. has_hours = False
  287. has_days = False
  288. has_months = False
  289. has_years = False
  290. use_seconds = False
  291. use_minutes = False
  292. use_hours = False
  293. use_days = False
  294. use_months = False
  295. use_years = False
  296. delta = []
  297. datetime_delta = []
  298. # First we compute the timedelta of the intervals
  299. for map in maps:
  300. start, end = map.get_temporal_extent_as_tuple()
  301. if start and end:
  302. delta.append(end - start)
  303. datetime_delta.append(compute_datetime_delta(start, end))
  304. # Compute the timedelta of the gaps
  305. for i in range(len(maps)):
  306. if i < len(maps) - 1:
  307. relation = maps[i + 1].temporal_relation(maps[i])
  308. if relation == "after":
  309. start1, end1 = maps[i].get_temporal_extent_as_tuple()
  310. start2, end2 = maps[i + 1].get_temporal_extent_as_tuple()
  311. # Gaps are between intervals, intervals and
  312. # points, points and points
  313. if end1 and start2:
  314. delta.append(end1 - start2)
  315. datetime_delta.append(compute_datetime_delta(end1, start2))
  316. if not end1 and start2:
  317. delta.append(start2 - start1)
  318. datetime_delta.append(compute_datetime_delta(
  319. start1, start2))
  320. # Check what changed
  321. dlist = []
  322. for d in datetime_delta:
  323. if "second" in d and d["second"] > 0:
  324. has_seconds = True
  325. #print "has second"
  326. if "minute" in d and d["minute"] > 0:
  327. has_minutes = True
  328. #print "has minute"
  329. if "hour" in d and d["hour"] > 0:
  330. has_hours = True
  331. #print "has hour"
  332. if "day" in d and d["day"] > 0:
  333. has_days = True
  334. #print "has day"
  335. if "month" in d and d["month"] > 0:
  336. has_months = True
  337. #print "has month"
  338. if "year" in d and d["year"] > 0:
  339. has_years = True
  340. #print "has year"
  341. # Create a list with a single time unit only
  342. if has_seconds:
  343. for d in datetime_delta:
  344. if "second" in d and d["second"] > 0:
  345. dlist.append(d["second"])
  346. elif "minute" in d and d["minute"] > 0:
  347. dlist.append(d["minute"] * 60)
  348. elif "hour" in d and d["hour"] > 0:
  349. dlist.append(d["hour"] * 3600)
  350. elif "day" in d and d["day"] > 0:
  351. dlist.append(d["day"] * 24 * 3600)
  352. else:
  353. dlist.append(d["max_days"] * 24 * 3600)
  354. use_seconds = True
  355. elif has_minutes:
  356. for d in datetime_delta:
  357. if "minute" in d and d["minute"] > 0:
  358. dlist.append(d["minute"])
  359. elif "hour" in d and d["hour"] > 0:
  360. dlist.append(d["hour"] * 60)
  361. elif "day" in d:
  362. dlist.append(d["day"] * 24 * 60)
  363. else:
  364. dlist.append(d["max_days"] * 24 * 60)
  365. use_minutes = True
  366. elif has_hours:
  367. for d in datetime_delta:
  368. if "hour" in d and d["hour"] > 0:
  369. dlist.append(d["hour"])
  370. elif "day" in d and d["day"] > 0:
  371. dlist.append(d["day"] * 24)
  372. else:
  373. dlist.append(d["max_days"] * 24)
  374. use_hours = True
  375. elif has_days:
  376. for d in datetime_delta:
  377. if "day" in d and d["day"] > 0:
  378. dlist.append(d["day"])
  379. else:
  380. dlist.append(d["max_days"])
  381. use_days = True
  382. elif has_months:
  383. for d in datetime_delta:
  384. if "month" in d and d["month"] > 0:
  385. dlist.append(d["month"])
  386. elif "year" in d and d["year"] > 0:
  387. dlist.append(d["year"] * 12)
  388. use_months = True
  389. elif has_years:
  390. for d in datetime_delta:
  391. if "year" in d:
  392. dlist.append(d["year"])
  393. use_years = True
  394. dlist.sort()
  395. ulist = list(set(dlist))
  396. if len(ulist) == 0:
  397. return None
  398. if len(ulist) > 1:
  399. # Find greatest common divisor
  400. granularity = gcd_list(ulist)
  401. else:
  402. granularity = ulist[0]
  403. if use_seconds:
  404. if granularity == 1:
  405. return "%i second" % granularity
  406. else:
  407. return "%i seconds" % granularity
  408. elif use_minutes:
  409. if granularity == 1:
  410. return "%i minute" % granularity
  411. else:
  412. return "%i minutes" % granularity
  413. elif use_hours:
  414. if granularity == 1:
  415. return "%i hour" % granularity
  416. else:
  417. return "%i hours" % granularity
  418. elif use_days:
  419. if granularity == 1:
  420. return "%i day" % granularity
  421. else:
  422. return "%i days" % granularity
  423. elif use_months:
  424. if granularity == 1:
  425. return "%i month" % granularity
  426. else:
  427. return "%i months" % granularity
  428. elif use_years:
  429. if granularity == 1:
  430. return "%i year" % granularity
  431. else:
  432. return "%i years" % granularity
  433. return None
  434. ###############################################################################
  435. def compute_common_relative_time_granularity(gran_list):
  436. """Compute the greatest common granule from a list of relative time granules
  437. .. code-block:: python
  438. >>> import grass.temporal as tgis
  439. >>> tgis.init()
  440. >>> grans = [1,2,30]
  441. >>> tgis.compute_common_relative_time_granularity(grans)
  442. 1
  443. >>> import grass.temporal as tgis
  444. >>> tgis.init()
  445. >>> grans = [10,20,30]
  446. >>> tgis.compute_common_relative_time_granularity(grans)
  447. 10
  448. """
  449. return gcd_list(gran_list)
  450. ###############################################################################
  451. def compute_common_absolute_time_granularity(gran_list):
  452. """Compute the greatest common granule from a list of absolute time granules
  453. .. code-block:: python
  454. >>> import grass.temporal as tgis
  455. >>> tgis.init()
  456. >>> grans = ["1 second", "2 seconds", "30 seconds"]
  457. >>> tgis.compute_common_absolute_time_granularity(grans)
  458. '1 seconds'
  459. >>> grans = ["3 second", "6 seconds", "30 seconds"]
  460. >>> tgis.compute_common_absolute_time_granularity(grans)
  461. '3 seconds'
  462. >>> grans = ["12 second", "18 seconds", "30 seconds", "10 minutes"]
  463. >>> tgis.compute_common_absolute_time_granularity(grans)
  464. '6 seconds'
  465. >>> grans = ["20 second", "10 minutes", "2 hours"]
  466. >>> tgis.compute_common_absolute_time_granularity(grans)
  467. '20 seconds'
  468. >>> grans = ["7200 second", "240 minutes", "1 year"]
  469. >>> tgis.compute_common_absolute_time_granularity(grans)
  470. '7200 seconds'
  471. >>> grans = ["7200 second", "89 minutes", "1 year"]
  472. >>> tgis.compute_common_absolute_time_granularity(grans)
  473. '60 seconds'
  474. >>> grans = ["10 minutes", "20 minutes", "30 minutes", "40 minutes", "2 hours"]
  475. >>> tgis.compute_common_absolute_time_granularity(grans)
  476. '10 minutes'
  477. >>> grans = ["120 minutes", "2 hours"]
  478. >>> tgis.compute_common_absolute_time_granularity(grans)
  479. '120 minutes'
  480. >>> grans = ["360 minutes", "3 hours"]
  481. >>> tgis.compute_common_absolute_time_granularity(grans)
  482. '180 minutes'
  483. >>> grans = ["2 hours", "4 hours", "8 hours"]
  484. >>> tgis.compute_common_absolute_time_granularity(grans)
  485. '2 hours'
  486. >>> grans = ["8 hours", "2 days"]
  487. >>> tgis.compute_common_absolute_time_granularity(grans)
  488. '8 hours'
  489. >>> grans = ["48 hours", "1 month"]
  490. >>> tgis.compute_common_absolute_time_granularity(grans)
  491. '24 hours'
  492. >>> grans = ["48 hours", "1 year"]
  493. >>> tgis.compute_common_absolute_time_granularity(grans)
  494. '24 hours'
  495. >>> grans = ["2 months", "4 months", "1 year"]
  496. >>> tgis.compute_common_absolute_time_granularity(grans)
  497. '2 months'
  498. >>> grans = ["120 months", "360 months", "4 years"]
  499. >>> tgis.compute_common_absolute_time_granularity(grans)
  500. '24 months'
  501. >>> grans = ["120 months", "361 months", "4 years"]
  502. >>> tgis.compute_common_absolute_time_granularity(grans)
  503. '1 months'
  504. >>> grans = ["2 years", "3 years", "4 years"]
  505. >>> tgis.compute_common_absolute_time_granularity(grans)
  506. '1 years'
  507. """
  508. has_seconds = False # 0
  509. has_minutes = False # 1
  510. has_hours = False # 2
  511. has_days = False # 3
  512. has_months = False # 4
  513. has_years = False # 5
  514. seconds = []
  515. minutes = []
  516. hours = []
  517. days = []
  518. months = []
  519. years = []
  520. min_gran = 6
  521. max_gran = -1
  522. for entry in gran_list:
  523. if not check_granularity_string(entry, "absolute"):
  524. return False
  525. num, gran = entry.split()
  526. if gran in ["seconds", "second"]:
  527. has_seconds = True
  528. if min_gran > 0:
  529. min_gran = 0
  530. if max_gran < 0:
  531. max_gran = 0
  532. seconds.append(int(num))
  533. if gran in ["minutes", "minute"]:
  534. has_minutes = True
  535. if min_gran > 1:
  536. min_gran = 1
  537. if max_gran < 1:
  538. max_gran = 1
  539. minutes.append(int(num))
  540. if gran in ["hours", "hour"]:
  541. has_hours = True
  542. if min_gran > 2:
  543. min_gran = 2
  544. if max_gran < 2:
  545. max_gran = 2
  546. hours.append(int(num))
  547. if gran in ["days", "day"]:
  548. has_days = True
  549. if min_gran > 3:
  550. min_gran = 3
  551. if max_gran < 3:
  552. max_gran = 3
  553. days.append(int(num))
  554. if gran in ["months", "month"]:
  555. has_months = True
  556. if min_gran > 4:
  557. min_gran = 4
  558. if max_gran < 4:
  559. max_gran = 4
  560. months.append(int(num))
  561. if gran in ["years", "year"]:
  562. has_years = True
  563. if min_gran > 5:
  564. min_gran = 5
  565. if max_gran < 5:
  566. max_gran = 5
  567. years.append(int(num))
  568. if has_seconds:
  569. if has_minutes:
  570. minutes.sort()
  571. seconds.append(minutes[0]*60)
  572. if has_hours:
  573. hours.sort()
  574. seconds.append(hours[0]*60*60)
  575. if has_days:
  576. days.sort()
  577. seconds.append(days[0]*60*60*24)
  578. if has_months:
  579. months.sort()
  580. seconds.append(months[0]*60*60*24*28)
  581. seconds.append(months[0]*60*60*24*29)
  582. seconds.append(months[0]*60*60*24*30)
  583. seconds.append(months[0]*60*60*24*31)
  584. if has_years:
  585. years.sort()
  586. seconds.append(years[0]*60*60*24*365)
  587. seconds.append(years[0]*60*60*24*366)
  588. num = gcd_list(seconds)
  589. return "%i %s"%(num, "seconds")
  590. elif has_minutes:
  591. if has_hours:
  592. hours.sort()
  593. minutes.append(hours[0]*60)
  594. if has_days:
  595. days.sort()
  596. minutes.append(days[0]*60*24)
  597. if has_months:
  598. months.sort()
  599. minutes.append(months[0]*60*24*28)
  600. minutes.append(months[0]*60*24*29)
  601. minutes.append(months[0]*60*24*30)
  602. minutes.append(months[0]*60*24*31)
  603. if has_years:
  604. years.sort()
  605. minutes.append(years[0]*60*24*365)
  606. minutes.append(years[0]*60*24*366)
  607. num = gcd_list(minutes)
  608. return "%i %s"%(num, "minutes")
  609. elif has_hours:
  610. if has_days:
  611. days.sort()
  612. hours.append(days[0]*24)
  613. if has_months:
  614. months.sort()
  615. hours.append(months[0]*24*28)
  616. hours.append(months[0]*24*29)
  617. hours.append(months[0]*24*30)
  618. hours.append(months[0]*24*31)
  619. if has_years:
  620. years.sort()
  621. hours.append(years[0]*24*365)
  622. hours.append(years[0]*24*366)
  623. num = gcd_list(hours)
  624. return "%i %s"%(num, "hours")
  625. elif has_days:
  626. if has_months:
  627. months.sort()
  628. days.append(months[0]*28)
  629. days.append(months[0]*29)
  630. days.append(months[0]*30)
  631. days.append(months[0]*31)
  632. if has_years:
  633. years.sort()
  634. days.append(years[0]*365)
  635. days.append(years[0]*366)
  636. num = gcd_list(days)
  637. return "%i %s"%(num, "days")
  638. elif has_months:
  639. if has_years:
  640. years.sort()
  641. months.append(years[0]*12)
  642. num = gcd_list(months)
  643. return "%i %s"%(num, "months")
  644. elif has_years:
  645. num = gcd_list(years)
  646. return "%i %s"%(num, "years")
  647. ###############################################################################
  648. # http://akiscode.com/articles/gcd_of_a_list.shtml
  649. # Copyright (c) 2010 Stephen Akiki
  650. # MIT License (Means you can do whatever you want with this)
  651. # See http://www.opensource.org/licenses/mit-license.php
  652. # Error Codes:
  653. # None
  654. def gcd(a, b):
  655. """The Euclidean Algorithm """
  656. a = abs(a)
  657. b = abs(b)
  658. while a:
  659. a, b = b % a, a
  660. return b
  661. ###############################################################################
  662. def gcd_list(list):
  663. """Finds the GCD of numbers in a list.
  664. :param list: List of numbers you want to find the GCD of
  665. E.g. [8, 24, 12]
  666. :return: GCD of all numbers
  667. """
  668. return reduce(gcd, list)
  669. ###############################################################################
  670. if __name__ == "__main__":
  671. import doctest
  672. doctest.testmod()