Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3831

How to Find the number of times "3AM" was crossed since a certain datetime until now without a While loop?

$
0
0

Below you can find my code. I need to find the number of times a certain hour has passed since a given datetime. In my case, the datetime is 3 AM. I need to find the number of times 3 AM was crossed since the 'video_published_at' time. My colleagues suggested there had to be a more concise solution than this, even a one-liner. I thought my while loop that checks of every time adjusted_time.hour is 3 was the only way to do it, but could there be something easier on the eyes? Colleague says there must be a way without a while loop at all.

# Calculate the difference in hours since the video_published_at timetime_difference = todays_date - video_published_attime_difference_secs = time_difference.total_seconds()time_difference_hours = time_difference_secs / 3600# Feedback/debugprint('Time Difference in Seconds: '+ str(time_difference_secs))print('Time Differnce in Hours: '+ str(time_difference_hours))print('')# Calculate the number of times 3 AM CT was crossed during the 'time_difference_hours' by iterationloop_count = 0am_count = 0adjusted_time = video_published_at_ct# Loop through time differencewhile loop_count <= time_difference_hours:    adjusted_time = adjusted_time + timedelta(hours=1)    # Feedback/debug    print(str(loop_count) +', '+ str(time_difference_hours))    print(adjusted_time)    print(adjusted_time.hour)           # Increase am_count every time the hour value hits 03:00    if (adjusted_time.hour == 3):        am_count = am_count + 1    # Increment loop    loop_count = loop_count + 1 

Viewing all articles
Browse latest Browse all 3831

Trending Articles