4

I can convert from an UTC timestamp e.g. 1417392000 to a local datetime object including daylight saving time, but when I try and convert the local datetime object to a "local timestamp" then I get the same UTC timestamp as before.

Am I thinking in circles and they are the same? I was supposed to save "local timestamp" from incoming UTC timestamp.

This is my code

    print("UTC timestamp %d" % hour[0])
    day = self.get_day(hour)
    month = self.get_month(hour)
    year = self.get_year(hour)
    tz = pytz.timezone('Europe/Stockholm')
    utc_dt = datetime.utcfromtimestamp(int(hour[0])).replace(tzinfo=pytz.utc)
    print("UTC datetime %s" % utc_dt)
    dt = tz.normalize(utc_dt.astimezone(tz))
    print("STO datetime %s" % dt.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
    print("STO ts %d" % int(time.mktime(dt.timetuple())))
    print("STO timestamp %d" % utc_dt.astimezone(tz).timestamp())
    day = int(dt.strftime('%d'))
    month = int(dt.strftime('%m'))
    year = int(dt.strftime('%Y'))

Output

UTC timestamp 1417395600
UTC datetime 2014-12-01 01:00:00+00:00
STO datetime 2014-12-01 02:00:00 CET+0100
STO ts 1417395600
STO timestamp 1417395600

All "timestamps" (i.e. integer representations of the time) are the same. Is it possible to make a "local timestamp" ? The data type is supposed to be a timestamp that is a number and in local time.

Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424

2 Answers2

7

As per Wikipedia

Unix time (also known as POSIX time or Epoch time) is a system for describing instants in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970

So regardless of what timezone you're on, the epoch will always be calculated in the UTC timezone.

For display purposes, you can convert the timestamp to a local time, but as otherwise the internal representation of the epoch will always be in the UTC timezone

pragman
  • 1,564
  • 16
  • 19
1

Formal timestamps

The Unix timestamp typically refers to the number of seconds since the epoch in UTC. This value is invariant to timezone. It allows global event ordering but loses timezone information.

Preserving timezone

To preserve timezone information, a standardized format is RFC3339, Date and Time on the Internet: Timestamps. This is just a standardized formatting that encodes date+time+timezone. Some examples:

1985-04-12T23:20:50.52Z
1996-12-19T16:39:57-08:00
1990-12-31T23:59:60Z
1990-12-31T15:59:60-08:00

Normalizing for timezone without preservation of timezone

However, it may depend on your requirements. I once wanted to record some events relative to local-time-of-day and did not mind losing timezone information. I normalized the timestamp with respect to 1970-01-01T00:00:00 in the local timezone. I am a little sheepish about this now as I think it may too easily cause confusion.

import time

# Number of seconds since 1970-01-01T00:00+LTZ (current timezone).
# unix timestamp - timezone offset in seconds
timestamp_localized = time.time() - time.mktime(time.gmtime(0))

However this syntax can be simplified, perhaps at the loss of clarity, by noticing that Python has some localtime and UTC specific functions.

import time
import calendar

# Number of seconds since 1970-01-01T00:00+LTZ (current timezone).
# Interpret local date and time as if it were UTC
timestamp_localized = calendar.timegm(time.localtime())

The difference between these two is that calendar conversion truncates to the second while the difference calculation includes a fractional second.

user650881
  • 2,214
  • 19
  • 31