Date, Time and Time Zones
While reading The Art of PostgreSQL, the section dealing with dates and time was very relevant to me. A lot of the data I deal with work deal with dates and time, sometimes without a time zone, sometimes with a time zone and in different formats. This article is a combination from the Art of PostgreSQL and a web post from Erik Naggum.
TLDR, always use timestamp with time zones. If you don’t have timezone, it will be very difficult to do it afterwards. For example you could assume that the timestamp is local but it could be based on UTC instead even though your company is only based in one time zone.
The Long Painful History of Time
This section is from Erik’s article.
The measurement of time has a very long history dating back thousands of years. The basic problem with time is that we need to express both time and place whenever we want to place some event in time and space, yet we tend to assume spatial coordinates even more than we assume temporal coordinates and in the case of time in ordinary communication, it is simply left out entirely.
Scientific Time
Scientific time is typically easy to deal with since the scientific community just need to agree. For example we have a world-wide standard for time, called the Coordinated Universal Time (UTC). Common Lisp starts at 0 seconds to 1900-01-01 while Unix 0 is 1970-01-01 UTC.
Political Time
Political time is closely related to territory, power and collective human irrationality. Normally we would say the political rotation takes 24 hours but one day out of the year it takes only 23 hours and another day out of the year it takes 25 hours thanks to the wonders of daylight saving time. Yet it is this political time that most people want their computers to produce when they ask for the date or the time of day.
Key Takeaways
Basically it can be difficult to understand time unless you have the location and some reference (time zone). Even with that it can be really complicated due to Political Time. There really should be no reason to figure out time zones on your own. Most programming languages will have a library/tool that will take care of this for you.
Time Intervals
The rest of the article are my notes from reading Dimitri’s book.
An interval describes a duration like a month or two weeks. Keep in mind from the earlier section, it is still important to know the location due to political time.
Date/Time Processing and Querying
I needed to get the data:
| |
I had to use tabs for PostgreSQL because I was having issues with , and |.
Then I need to create the table and load in the data.
| |
Finally load the data:
| |
I had an issue with PostgreSQL even with tab delimited so I took the 1st 25,000 rows which was able to load with no issues.
| |
Looking at the commit history, let’s look at how many commits each project had. This output is a pivot query.
We can also build a reporting on the repartition of commits by weekday from the beginning of the project in order to guess if contributors are working on the project on the job only or on the weekend.
| |
This is just a couple of things you can do using PostgreSQL and timestamps.
Key Takeaways
Basically dealing with dates, time and time zone can get complicated and it is easy to make mistakes. PostgreSQL deals with this for you really well. It is easier to ensure that the data is saved to PostgreSQL with proper timestamps with time zones instead of depending on your program language that is interacting with the database.
#Time #Date #Timezone #Utc #The Art of PostgreSQL #Sql #Postgresql #Time Zone