More on timestamps

To add to previous post where I talk about using Unix timestamps for all things related to when I want to give the following addendum.

Arithmetic

In arithmetic or mathematics you can operate in different so called bases. Normally we operate in base 10 meaning the tenth digit will move you to the next up. Example would be from 9 to 10. Then from 99 to 100. Meaning you create a number by going from right to left and doing 10**0 * 5 + 10**1 * 5 to create 55. Enough to know for now that decimal and base 10 mean the same. There also exists base 2, which is binary and it is what CPU's are. Time however is base 60 when dealing with seconds and minutes, but suddenly 24 when dealing with days but 7 when weeks are concerned and variable when dealing with months and years. However since Unix timestamps are base 10 you can add a specific number all the time in order to advance the same way. This eliminates the call for parsing a date time string and updating the correct part.

Adding a day

So in order to add a day. We need to know how many seconds are in a day: 60 * 60 * 24 = 86400. Now whatever it is now in Unix timestamp you just add 86400 for one day and x amount of times that number for any other days.

Getting a duration

To get a duration you only need to subtract two numbers and the diff is in seconds, but that can converted to another representation if needed.

#devlife #thoughts