Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To convert a datetime with timezone to epoch (POSIX timestamp):</p> <pre><code>from datetime import datetime import pytz tz = pytz.timezone('CST6CDT') # a datetime with timezone dt_with_tz = tz.localize(datetime(2012, 8, 28, 19, 33, 50), is_dst=None) # get timestamp ts = (dt_with_tz - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds() # -&gt; 1346200430.0 </code></pre> <p>It is how <code>datetime.timestamp</code> method is implemented for timezone-aware <code>datetime</code> objects in Python 3.</p> <p>To get "now epoch":</p> <pre><code>from datetime import datetime now_epoch = (datetime.utcnow() - datetime(1970, 1, 1)).total_seconds() </code></pre> <p>Or (assuming <code>time</code> uses POSIX epoch):</p> <pre><code>import time now_epoch = time.time() </code></pre> <p>Getting "beginning of current day epoch" is more complex because current day may be different in different timezones:</p> <pre><code>from datetime import datetime, time import pytz tz = pytz.timezone('CST6CDT') # get current date in given timezone today = datetime.now(tz).date() # -&gt; datetime.date(2013, 6, 22) # get beginning of current day in given timezone as a datetime with timezone midnight = tz.localize(datetime.combine(today, time(0, 0)), is_dst=None) # -&gt; datetime.datetime(2013, 6, 22, 0, 0, tzinfo=&lt;DstTzInfo 'CST6CDT'...&gt;) # get timestamp ts = (midnight - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds() # -&gt; 1371877200.0 </code></pre> <p>See <a href="https://stackoverflow.com/a/11236372/4279">How do I get the UTC time of “midnight” for a given timezone?</a>.</p> <p>To get "beginning of current day epoch" assuming UTC date:</p> <pre><code>from datetime import datetime, date # get current date in UTC utc_date = datetime.utcnow().date() # -&gt; datetime.date(2013, 6, 23) # get timestamp ts = (utc_date - date(1970, 1, 1)).days * 86400 # -&gt; 1371945600 </code></pre> <p>See <a href="https://stackoverflow.com/a/8778548/4279">Converting datetime.date/datetime.datetime to UTC timestamp in Python</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload