Programmer's Python Data - Naive Dates |
Written by Mike James | |||
Wednesday, 02 October 2024 | |||
Page 2 of 2
TimestampsYou can use the datetime object to record and work with dates and times in a human-oriented representation. This is far too wasteful a format for recording dates and times as part of the operating system. For example, every file has a creation date and time stored along with it and this needs to be compact to avoid wasting storage. These data and time records are generally called timestamps and we need to know how they work and how to convert them to a datetime object. The key idea in using a timestamp is that they generally record the date and time from some fixed point and usually as the number of seconds or milliseconds that have elapsed. Most operating systems follow Unix in recording the number of seconds from: Thursday Jan 1st 00:00:00 UTC 1970. Why this exact date in 1970? The reason is that this is the Unix epoch, i.e it is around the time when the Unix operating system was created. Keeping time in seconds from this date is usually called "Unix time", Epoch time or Posix time. At the moment Unix time is generally recorded using a 32-bit integer which means it will roll over in 2038, creating the so-called “year 2038 problem”. Upgrades to 64-bit integer representation is in progress and this won’t roll over until 292 billion years have elapsed. Also notice that Unix time is both positive and negative with negative second counts being taken to represent time before the epoch. It is also customary to supplement the Unix time by an additional 32-bit integer that records milliseconds or nanoseconds. In practice, the accuracy of timestamps is very much lower than this. It is also worth noting that Unix time is a rough approximation to UTC as it handles leap seconds differently. Most of the time you can ignore the differences unless accuracy to the second is essential. So how do you access a timestamp? The most direct way is to use the: fromTimestamp(timestamp) method which constructs a datetime object from the timestamp. Unless you specify a time zone as a second parameter, the local time of the system is used. If you need a UTC datetime object from the timestamp you can either specify the time zone explicitly or use: utcfromTimestamp(timestamp) Of course, you need an actual timestamp to convert to a datetime object and these can be read from data files or from the metadata associated with file or many other sources. The most direct way is to simply present an integer value of seconds since the epoch. For example: dt = datetime.datetime.fromtimestamp(0) myString = dt.isoformat() print(myString) displays: 1970-01-01T00:00:00 which is, of course, the start of the epoch. If you are converting timestamps derived from external sources you need to keep in mind that the value may be in milliseconds rather than seconds and you need to scale it. You can specify fractional seconds as part of a Python timestamp. You can also derive a timestamp from a datetime object using the timestamp() method which returns a timestamp corresponding to the current date and time. This method makes use of a system function call which converts the date and time from local time to UTC and then to a timestamp, see Chapter 16. The system function doesn’t support the same range of dates and times that the datetime object does and hence it is possible to trigger an exception for dates in the distant past and far future. You can also obtain a timestamp using the time method of the time object in the time module, see later. Today and NowOften you need the current time and date and there are some very simple methods that will read the system’s hardware and return the current time and date. The today() method returns the current date and time using the local timezone and it also sets the datetime object’s timezone. If you want nothing to do with time zones you can create a naive datetime object using: utcnow() which returns the UTC date and time. Notice that if you use this method the datetime object runs the risk of being treated as being in local time by other methods. In Chapter but not in this extract
Summary
Programmer's Python
|
|||
Last Updated ( Wednesday, 02 October 2024 ) |