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
  Check time within specific time ranges basvdm 3 610 Jan-20-2025, 05:10 PM
Last Post: Gribouillis
Question [SOLVED] [datetime.strptime] ValueError: time data 'foo' does not match format 'bar' Winfried 1 1,244 Jan-02-2025, 02:09 AM
Last Post: lyly19
  Help about a specific time selection QJZ 0 403 Dec-01-2024, 11:25 AM
Last Post: QJZ
  How to move an object over time rather than instantly? temlotresid6 3 1,689 Oct-23-2024, 11:20 AM
Last Post: temlotresid6
  Is there a difference between Python’s time.sleep and win32api.Sleep? phpjunkie 4 1,105 Sep-21-2024, 05:17 PM
Last Post: aakritiintelligence
  How to insert text time - into video frames? oxidian 0 1,193 Aug-25-2024, 04:51 PM
Last Post: oxidian
  Using RTC time in Rasberry Pi Programs sab201 1 898 Aug-18-2024, 05:50 PM
Last Post: snippsat
  Schedule exit a program at a specific time 4 am every day. chubbychub 3 1,483 May-17-2024, 03:45 PM
Last Post: chubbychub
  Filer and sort files by modification time in a directory tester_V 5 2,419 May-02-2024, 05:39 PM
Last Post: tester_V
Question Convert UTC now() to local time to compare to a strptime() Calab 2 2,101 Apr-29-2024, 07:24 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