H
H
Hocok_B_KapMaHe2014-05-17 23:31:31
*nix-like systems
Hocok_B_KapMaHe, 2014-05-17 23:31:31

How to output date from unixtime (algorithm?)?

Something the Internet does not at all give an answer to an obvious question that suggests itself.
You can take seconds modulo 86400 and get the number of days, you can take modulo (30 days * seconds\days) and get months ...
But what about February 28, 30.31 days in a month, etc. etc ?
Now it is necessary to get the year \ month \ day from unixtime natively (without using ready-made functions)
I'm afraid to imagine how the code can turn out, given all the nuances.
Maybe I don't understand something, and it's much easier to get a human-readable date from Unix time?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
H
Hocok_B_KapMaHe, 2014-10-22
@Hocok_B_KapMaHe

No, it's not easier.
This is done by specialized libraries.

This is what happens when hello world is not written without a 300MB framework.
Looking through my old questions, I will still leave the answer to my own question (found on a foreign resource)
Ok, I managed to get something accurate. Dang leap years!
In the steps below I am using a timestamp of 1203161493 which corresponds to a date/time of 2-15-2008 11:31:33. I'm ignoring timezones and whatnot, it isn't necessary for this.
Steps:
1. Unix timestamp / hours in a year to get years from 1970 to timestamp
ex: 1203161493 / 31436000 = 38.152... years since 1970. Ignoring the decimals, thats 38 years + 1970 = 2008.
2. Determine the number of leap years from 1970 to year found in step 1 (extra days).
ex: (2008-1969)/4 = 9.75. Again, ignore the decimal, and we have 9 extra days (we ignore this year's leap day until later)
3. Determine the number of days since the epoch.
ex: 1203161493 / 86400 = 13925.480... days since epoch. Ignore the decimals again.
4. Subtract leap days from number of days since epoch.
ex: 13925 - 9 = 13916.
5. Modulo the number above by the number of days in a year to find the days passed in the current year.
ex: 13916% 365 = 46 days this year.
6. We go through each month and subtract it until the days left are less than the month's total days. If this year is a leap year and your days in this year found in step 5 was greater than 59 (31+28), we would add one.
ex: 46 - 31 days in Jan = 15 days (in 2nd month) = Feb 15.
7. Find the number of seconds in the current day. Subtract the days since epoch found in step 3 from the timestamp.
ex: 1203161493 - (13925 * 86400) = 41493 seconds
8. Figure out how many hours the seconds found in step 6 is.
ex: 41493 / 3600 = 11.5283... hours. Ignore the decimal again.
9. Find the number of minutes left. Subtract the hours you found in the previous step from the seconds in step 6, then divide by 60.
ex: 41493 - (11 * 3600) = 1893
1893 / 60 = 31.55 minutes. Ignore the decimal
10. Find the number of seconds left. Subtract the minutes in step 8 from the seconds in step 8
ex: 1893 - (31 * 60) = 33 seconds.
Put it all together:
Year: 2008
Month: 2
Day: 15
Hour: 11
Minute: 31
Second: 33

V
Vyacheslav Slinko, 2014-05-18
@KeepYourMind

No, it's not easier.
This is done by specialized libraries, which contain all the nuances for all regions of the world.

J
jcmvbkbc, 2014-05-18
@jcmvbkbc

Here are a couple of algorithms for you:
git.uclibc.org/uClibc/tree/libc/misc/time/time.c#n2164
https://sourceware.org/git/?p=glibc.git;a=blob;f=t ...

A
Arct, 2017-05-16
@Arct

Point 6 is repeated 2 times, I searched for the algorithm, found it, started to format it as a code and hung ... Fix it as much as possible.
Z.Y. Thank you)

K
kerberous, 2019-02-20
@kerberous

The year is not calculated correctly after 5 points, you need to calculate it like this:
13916 / 365 + 1970

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question