What is time in the Python world
Not only in the Python world, but also in the computer world, the representation of time is chaotic.
First, there are Unix timestamps, then UTC(though only a standard), GMT, etc., and not only that, but also the concept of time zones (there are hundreds of time zones in the world, like the United States, where states define different time zones). Then there’s that weird thing called Daylight saving Time (DST), which the twee Americans define.
How a disorderly word get.
Time-dependent standard library & Tripartite library
In Python’s standard library, the time-related standard libraries are:
- time
- datetime
- calendar
Third party libraries for processing time are:
- Dateutil (recommended)
- pytz
The standard library provides a plethora of confusing, inconsistent apis that require a temporary document search every time they are used, causing pain every time they are used.
Processing time (the idea of metadata)
What is metadata?
The purest, cleanest data. For example, in Excel, what do you think is the most important thing? It’s got to be clean lines of data. And with them, we can make all sorts of beautiful charts and do all sorts of analyses on top of all this data. What are the characteristics of such data? One is clean, should be pure, without other decoration; Second, the granularity is fine enough, can not be split down.
To represent a time in Python, you can use Unix timestamps, you can use datetime, which is object-oriented, or you can use “human-readable string form.” Corresponding to the above example, who is our choice?
Unix timestamps, of course. Because it has the following characteristics:
- Pure enough (number of seconds since 1970/1/1, and absolute offset)
- No extra meaning (just an absolute offset)
Use timestamps to avoid all time zone issues in the first place because it is absolute. Plus, dealing with time stamps is a convenient and easy thing to do.
In particular, when storing databases, you can save them directly as timestamps (bitints can be done), no longer need to deal with various datetimes, and avoid a lot of trouble.
How do you use it?
- In a program, time stamps are used;
- When needed to display to the user, converted to a string;
- In other cases, it is converted to objects such as datetime
Let’s take a look at the standard library apis, summarize the clutter, and tease out the important ones.
Def now_timestamp(): Return int(time.time() * 1000) # def timestamp2str(ts, FMT =None): if FMT is None: fmt = '%Y/%m/%d %H:%M:%S' timetuple = time.localtime(ts) return time.strftime(fmt, timetuple) def datetime2timestamp(dt, is_utc=True): if is_utc: ts = time.mktime(dt.utctimetuple()) else: Def datetime2ISO8601 (dt) def datetime2ISO8601 (dt): return dt.isoformat() def timestamp2iso8601(ts): dt = timestamp2datetime(ts) return datetime2iso8601(dt) def timestamp2datetime(ts): return datetime.fromtimestamp(ts) def datetime2str(dt, fmt): return dt.strftime(fmt) def timestr2datetime(time_str, fmt='%Y-%m-%dT%H:%M:%S'): return datetime.strptime(time_str, fmt) def timestr2timestamp(time_str): dt = timestr2datetime(time_str) return datetime2timestamp(dt)Copy the code
What can a third-party library do
In third-party libraries, only Dateutil is recommended (mainly because of familiarity) for the following reasons:
- Powerful (It takes care of everything you think about)
- The API is simple and consistent, and takes a lot of mental weight off of people
Anyone can write an API, but the law of API has always been that it is not easy to write an API that is easy to use, intuitive, extensible, and abstract. What could be more satisfying than a few apis that can satisfy all your needs?
If using standard library doesn’t work, use Dateutil instead.
The official documentation on the use of Dateutil provides a very detailed example that will not be covered here.