How can i calculate day number from a unix-timestamp, in a mathematical way and without using any functions and in simple math formula.
1313905026 --> 8 (Today 08/21/2011)
How can i calculate day number from a unix-timestamp, in a mathematical way and without using any functions and in simple math formula.
1313905026 --> 8 (Today 08/21/2011)
A unix timestamp doesn't include leap seconds, so we don't have to worry about that. Here is a branch-less1, loop-less algorithm for getting the y/m/d fields from a unix timestamp:
This outputs:
As you're not interested in
y
andm
(only ind
), you can eliminate the last couple of lines from the above computation.This algorithm is described in excruciating detail here. The link includes a complete derivation, and unit tests spanning millions of years (which is overkill).
1 Branch-less: What looks like small branches in the algorithm above are optimized away by clang at -O3 on macOS:
1970 started with a Thursday so, we can calculate the day of the week:
If Sunday is day 0. If you want Sunday to be day 1 just add 1.
Now, let's find out how many days we are into the year in question.
Finally, we find the month and day of the month.
This assumes Boolean values of TRUE = 1, FALSE = 0, NOT TRUE = 0, and NOT FALSE = 1.
Now we have the year, month, day of the month, hour, minute, and second calculated with adjustments for leap years.
There is no simple formula to do this. You would need to subtract the number of years (accounting for leap years) since the epoch, which would probably require a loop or a discrete calculation of some kind. Then use some type of loop to subtract out the number of seconds in each month for the current year. What you are left with is the number of seconds currently into the month.
I would do something like this.
Edit
I recommend using date functions for simplicity, but here is a possible non-loopy alternative answer in case anyone needs it, or would care to develop it further.
First let t be the current time in seconds since the epoch.
Let F be the number of seconds in four years. That is three regular years and one leap year. That should be: 126230400.
Now if you take away all of the time contributed by F, you will get a remainder: y.
So y = n % F.
There are several cases now: 1. y is less that one year 2. y is less than two years 3. y is less than three years and less than two months 4. y is less than three years and greater than two months 5. y is less than four years
Note that 1972 was a leap year, so if you count up by four from 1970, wherever you left off will be a leap year in two years.
let jan, feb, febLY, mar, may, ..., dec be the number of seconds in each month (you'd need to calculate it out).
d represents the day number of the current month and D represents the number of seconds in a day (86400). y represents the number of seconds in a regular year, and yLY represents the number of seconds in a leap year.
Not tested. Also, since the Earth doesn't spin at EXACTLY 1 rotation / 24 hours, they've occasionally made adjustments to time. You need to do a bit of research factor that in.