Guest User

Untitled

a guest
Oct 16th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. 00:59:00
  2. 01:01:00
  3.  
  4. 00:59:00 -> 3540 seconds
  5. 01:01:00 -> 3660 seconds
  6. ============
  7. average: 3600 seconds converted to HH:MM:SS -> 01:00:00
  8.  
  9. from datetime import timedelta
  10.  
  11. times = ['00:58:00','00:59:00','01:00:00','01:01:00','01:02:00']
  12.  
  13. print(str(timedelta(seconds=sum(map(lambda f: int(f[0])*3600 + int(f[1])*60 + int(f[2]), map(lambda f: f.split(':'), times)))/len(times))))
  14.  
  15. import time
  16.  
  17.  
  18. a = time.strptime("2000:11:12:13","%Y:%H:%M:%S")
  19. b = time.strptime("2000:11:14:13","%Y:%H:%M:%S")
  20.  
  21. avg_time = time.localtime(((time.mktime(a)+time.mktime(b))/2))
  22.  
  23. >> time.struct_time(tm_year=2000, tm_mon=1, tm_mday=1, tm_hour=11, tm_min=13, tm_sec=13, tm_wday=5, tm_yday=1, tm_isdst=0)
  24.  
  25. import datetime
  26. import math
  27. import numpy
  28.  
  29. def datetime_to_radians(x):
  30. # radians are calculated using a 24-hour circle, not 12-hour, starting at north and moving clockwise
  31. time_of_day = x.time()
  32. seconds_from_midnight = 3600 * time_of_day.hour + 60 * time_of_day.minute + time_of_day.second
  33. radians = float(seconds_from_midnight) / float(12 * 60 * 60) * 2.0 * math.pi
  34. return radians
  35.  
  36. def average_angle(angles):
  37. # angles measured in radians
  38. x_sum = numpy.sum([math.sin(x) for x in angles])
  39. y_sum = numpy.sum([math.cos(x) for x in angles])
  40. x_mean = x_sum / float(len(angles))
  41. y_mean = y_sum / float(len(angles))
  42. return numpy.arctan2(x_mean, y_mean)
  43.  
  44. def radians_to_time_of_day(x):
  45. # radians are measured clockwise from north and represent time in a 24-hour circle
  46. seconds_from_midnight = int(float(x) / (2.0 * math.pi) * 12.0 * 60.0 * 60.0)
  47. hour = seconds_from_midnight / 3600
  48. minute = (seconds_from_midnight % 3600) / 60
  49. second = seconds_from_midnight % 60
  50. return datetime.time(hour, minute, second)
  51.  
  52. def average_times_of_day(x):
  53. # input datetime.datetime array and output datetime.time value
  54. angles = [datetime_to_radians(y) for y in x]
  55. avg_angle = average_angle(angles)
  56. return radians_to_time_of_day(avg_angle)
  57.  
  58. average_times_of_day([datetime.datetime(2017, 6, 9, 0, 10), datetime.datetime(2017, 6, 9, 0, 20)])
  59. # datetime.time(0, 15)
  60.  
  61. average_times_of_day([datetime.datetime(2017, 6, 9, 23, 50), datetime.datetime(2017, 6, 9, 0, 10)])
  62. # datetime.time(0, 0)
  63.  
  64. time_list = map(lambda s: int(s[6:8]) + 60*(int(s[3:5]) + 60*int(s[0:2])), mylist)
  65. average = sum(time_list)/len(time_list)
  66. bigmins, secs = divmod(average, 60)
  67. hours, mins = divmod(bigmins, 60)
  68. print "%02d:%02d:%02d" % (hours, mins, secs)
  69.  
  70. from cmath import rect, phase
  71. from math import radians, degrees
  72.  
  73. def meanAngle(deg):
  74. complexDegree = sum(rect(1, radians(d)) for d in deg) / len(deg)
  75. argument = phase(complexDegree)
  76. meanAngle = degrees(argument)
  77. return meanAngle
  78.  
  79. def meanTime(times):
  80. t = (time.split(':') for time in times)
  81. seconds = ((float(s) + int(m) * 60 + int(h) * 3600)
  82. for h, m, s in t)
  83. day = 24 * 60 * 60
  84. to_angles = [s * 360. / day for s in seconds]
  85. mean_as_angle = mean_angle(to_angles)
  86. mean_seconds = mean_as_angle * day / 360.
  87. if mean_seconds < 0:
  88. mean_seconds += day
  89. h, m = divmod(mean_seconds, 3600)
  90. m, s = divmod(m, 60)
  91. return '%02i:%02i:%02i' % (h, m, s)
  92.  
  93.  
  94. print(meanTime(["15:00:00", "21:00:00"]))
  95. # 18:00:00
  96. print(meanTime(["23:00:00", "01:00:00"]))
  97. # 00:00:00
Add Comment
Please, Sign In to add comment