Tuesday, March 10, 2015

Facebook: All time ranges must be midnights - Not every day is like another, some days are 23h and some are 25h long

Facebook has funny thing about their API. They require timestamps used in their report to be on midnight boundary in your local timezone.

Originally I wrote this code:


facebook_time = DateTime.parse(@run_date.to_s).utc.in_time_zone('America/Los_Angeles').midnight.to_i
time_interval="{time_start:#{facebook_time}, time_stop:#{facebook_time+24*3600}}"

That worked great until daylight savings kicked in, and failed with "All time ranges must be midnights", so after a bit of debugging I changed it to this one:

facebook_time = DateTime.parse(@run_date.to_s).utc.in_time_zone('US/Pacific-New').midnight.to_i
facebook_time_end = DateTime.parse((@run_date+1).to_s).utc.in_time_zone('US/Pacific-New').midnight.to_i
time_interval="{time_start:#{facebook_time}, time_stop:#{facebook_time_end}}"
I hope this will help someone spend less time debugging.