Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8364569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:27:30+00:00 2026-06-09T12:27:30+00:00

I want to find the last mid-night time stamp (only input is current timestamp).

  • 0

I want to find the last mid-night time stamp (only input is current timestamp). What is the best way?

I am writing python script for a global mobile application. The user request with the current timestamp, and in server side I want to find the last mid-night timestamp of the user with out affect time zone parameters.

I searched for it, I got a solution

import time
etime = int(time.time())
midnight = (etime - (etime % 86400)) + time.altzon

Its worked for me. But I am confused with time.altzon function, Is it create any problem for the users in different timezones.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:27:32+00:00Added an answer on June 9, 2026 at 12:27 pm

    To get the midnight timestamp of the client(mobile) you need to know the client’s timezone.

    from datetime import datetime
    import pytz # pip install pytz
    
    fmt = '%Y-%m-%d %H:%M:%S %Z%z'
    tz = pytz.timezone("America/New_York") # supply client's timezone here
    
    # Get correct date for the midnight using given timezone.
    
    # due to we are interested only in midnight we can:
    
    # 1. ignore ambiguity when local time repeats itself during DST change e.g.,
    # 2012-04-01 02:30:00 EST+1100 and
    # 2012-04-01 02:30:00 EST+1000
    # otherwise we should have started with UTC time
    
    # 2. rely on .now(tz) to choose timezone correctly (dst/no dst)
    now = datetime.now(tz)
    print(now.strftime(fmt))
    
    # Get midnight in the correct timezone (taking into account DST)
    midnight = tz.localize(now.replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=None),
                           is_dst=None)
    print(midnight.strftime(fmt))
    
    # Convert to UTC (no need to call `tz.normalize()` due to UTC has no DST transitions)
    dt = midnight.astimezone(pytz.utc)
    print(dt.strftime(fmt))
    
    # Get POSIX timestamp
    print((dt - datetime(1970,1,1, tzinfo=pytz.utc)).total_seconds())
    

    Output

    2012-08-09 08:46:29 EDT-0400
    2012-08-09 00:00:00 EDT-0400
    2012-08-09 04:00:00 UTC+0000
    1344484800.0
    

    Note: on my machine @phihag’s answer produces 1344470400.0 that is different from the above (my machine is not in New York).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to find out the last login time for a user in my
I want to find the last modified file that contains a string using python.
i have field data_clockin in table.what i want to find only last entry in
When I want to find the last used cell value, I use: Dim LastRow
i have a string %/O^/O%/O . I want to find the last / to
I want to find first 4 words and last 4 words after the particular
I want to find out which tables have been modified in the last hour
Suppose you want to find the last record entered into the database (highest ID)
Using STL, I want to find the last instance of a certain value in
I want to find an index of the last / character, but the problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.