Guest User

Untitled

a guest
Mar 16th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. day_count = (end_date - start_date).days + 1
  2. for single_date in [d for d in (start_date + timedelta(n) for n in range(day_count)) if d <= end_date]:
  3. print strftime("%Y-%m-%d", single_date.timetuple())
  4.  
  5. 2009-05-30
  6. 2009-05-31
  7. 2009-06-01
  8. 2009-06-02
  9. 2009-06-03
  10. 2009-06-04
  11. 2009-06-05
  12. 2009-06-06
  13. 2009-06-07
  14. 2009-06-08
  15. 2009-06-09
  16.  
  17. for single_date in (start_date + timedelta(n) for n in range(day_count)):
  18. print ...
  19.  
  20. def daterange(start_date, end_date):
  21. for n in range((end_date - start_date).days):
  22. yield start_date + timedelta(n)
  23.  
  24. for single_date in daterange(start_date, end_date):
  25. print strftime("%Y-%m-%d", single_date.timetuple())
  26.  
  27. d = start_date
  28. delta = datetime.timedelta(days=1)
  29. while d <= end_date:
  30. print d.strftime("%Y-%m-%d")
  31. d += delta
  32.  
  33. from datetime import date
  34. from dateutil.rrule import rrule, DAILY
  35. a = date(2009, 5, 30)
  36. b = date(2009, 6, 9)
  37. for dt in rrule(DAILY, dtstart=a, until=b):
  38. print dt.strftime("%Y-%m-%d")
  39.  
  40. import datetime
  41.  
  42. def daterange(start, stop, step=datetime.timedelta(days=1), inclusive=False):
  43. # inclusive=False to behave like range by default
  44. if step.days > 0:
  45. while start < stop:
  46. yield start
  47. start = start + step
  48. # not +=! don't modify object passed in if it's mutable
  49. # since this function is not restricted to
  50. # only types from datetime module
  51. elif step.days < 0:
  52. while start > stop:
  53. yield start
  54. start = start + step
  55. if inclusive and start == stop:
  56. yield start
  57.  
  58. # ...
  59.  
  60. for date in daterange(start_date, end_date, inclusive=True):
  61. print strftime("%Y-%m-%d", date.timetuple())
  62.  
  63. from dateutil.rrule import rrule, DAILY
  64. rr = rrule(DAILY, dtstart=start_date, until=end_date)
  65. print list(rr)
  66.  
  67. import datetime
  68.  
  69. def daterange(start, stop, step_days=1):
  70. current = start
  71. step = datetime.timedelta(step_days)
  72. if step_days > 0:
  73. while current < stop:
  74. yield current
  75. current += step
  76. elif step_days < 0:
  77. while current > stop:
  78. yield current
  79. current += step
  80. else:
  81. raise ValueError("daterange() step_days argument must not be zero")
  82.  
  83. if __name__ == "__main__":
  84. from pprint import pprint as pp
  85. lo = datetime.date(2008, 12, 27)
  86. hi = datetime.date(2009, 1, 5)
  87. pp(list(daterange(lo, hi)))
  88. pp(list(daterange(hi, lo, -1)))
  89. pp(list(daterange(lo, hi, 7)))
  90. pp(list(daterange(hi, lo, -7)))
  91. assert not list(daterange(lo, hi, -1))
  92. assert not list(daterange(hi, lo))
  93. assert not list(daterange(lo, hi, -7))
  94. assert not list(daterange(hi, lo, 7))
  95.  
  96. for d in map( lambda x: startDate+datetime.timedelta(days=x), xrange( (stopDate-startDate).days ) ):
  97. # Do stuff here
  98.  
  99. for d in map( lambda x: startTime+x*stepTime, xrange( (stopTime-startTime).total_seconds() / stepTime.total_seconds() ) ):
  100. # Do stuff here
  101.  
  102. def total_seconds( td ):
  103. return float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
Add Comment
Please, Sign In to add comment