Python Forum
Writing a function to calculate time range in Unix Epoch Time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing a function to calculate time range in Unix Epoch Time
#1
A program I am writing has the requirement to calculate the past 24 hours (as a time range) in unix epoch time. I would like two objects... the current time, and 24 hours before the current time, in unix epoch time.

My application would use these 2 objects as parameters in an api request to "check for updates in the past 24 hours".

If my thought pattern is correct, I would need to first calculate the current time in my time zone, subtract 1 day (24 hours) from that time, then convert both of those values to unix epoch time to be passed in the request.

So far, I was able to write a small program to determine the current time in my time zone, and print it.

I would like assistance in the best way to calculate the second time I'm looking for (24 hours prior), and then convert both of those to unix epoch time to be passed to the api.

Here is what I have so far (which I derived from the datetime docs, so not much of this was done with my own brain power):

#Class to define Eastern Standard Time
class EST5EDT(datetime.tzinfo):
    
    def utcoffset(self, dt):
        return datetime.timedelta(hours=-5) + self.dst(dt)

    def dst(self, dt):
        d = datetime.datetime(dt.year, 3, 8)        #2nd Sunday in March
        self.dston = d + datetime.timedelta(days=6-d.weekday())
        d = datetime.datetime(dt.year, 11, 1)       #1st Sunday in Nov
        self.dstoff = d + datetime.timedelta(days=6-d.weekday())
        if self.dston <= dt.replace(tzinfo=None) < self.dstoff:
            return datetime.timedelta(hours=1)
        else:
            return datetime.timedelta(0)

    def tzname(self, dt):
        return 'EST5EDT'
    
dt = datetime.datetime.now(tz=EST5EDT())
fmt = "%Y-%m-%d %H:%M:%S %Z%z"

def setTime():
    global dt, fmt
    print('The time is now: ')
    print(dt.strftime(fmt))
Reply
#2
Epoch time is based on UTC, so timezones don't matter.
>>> import time
>>> now = time.time()
>>> then = now - 24*60*60
>>> print(f"current epoch time {now}, 24 hours ago was {then}")
current epoch time 1594755817.479174, 24 hours ago was 1594669417.479174
Reply
#3
awesome, thank you for that example
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Date Time Series Help...Please spra8560 2 353 Feb-01-2024, 01:38 PM
Last Post: spra8560
  Python date format changes to date & time 1418 4 589 Jan-20-2024, 04:45 AM
Last Post: 1418
  time difference bettwenn logs enkliy 14 967 Nov-21-2023, 04:51 PM
Last Post: rob101
Question Need Help with Vehicle Routing Problem with Time Windows (VRPTW) in Python kasper321421312 1 554 Nov-10-2023, 08:19 PM
Last Post: snippsat
  How do I stream and record at the same time with arducam? traderjoe 0 456 Oct-23-2023, 12:01 AM
Last Post: traderjoe
  i tried to install python for the first time today and pretty certain im being remote brianlj 2 540 Oct-03-2023, 11:15 AM
Last Post: snippsat
  [Python 2.7] Why can't I press [ESC] a fourth time? Ashwood 3 656 Aug-27-2023, 02:01 PM
Last Post: deanhystad
  Hard time trying to figure out the difference between two strings carecavoador 2 675 Aug-16-2023, 04:53 PM
Last Post: carecavoador
  Function parameter not writing to variable Karp 5 938 Aug-07-2023, 05:58 PM
Last Post: Karp
  return next item each time a function is executed User3000 19 2,277 Aug-06-2023, 02:29 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020