23942
Implement Unix Timestamp to Daytime
Given an unsigned integer that represents a timestamp since 1970/01/01 00:00:00 (which is Unix epoch time), output one of these to represent the daytime in GMT timezone:
- An array that stores
year, month, date, hour, minute, second
. - A string in format
YYYYMMDDHHmmss
. - Or whatever similar, as long as it complies with standard i/o rules.
Rules
- Implement the function or the full program from vanilla.
- So no built-ins nor libraries that has the fuctionality (see next section).
- Implement leap year, too; but don't leap second (although leap second is not supported).
- You just need to support up to 2038/01/19 (so input range shall be 0 to 2147483647 (inclusive)).
- Standard loopholes apply.
- Standard I/O rules apply.
- Input and output format should be consistent and not ambiguous.
- So, for example, if output is a string like
1970121000
, it's unqualified, as it can be recognized as1970/01/21 00:00:00
,1970/12/01 00:00:00
, or1970/01/02 10:00:00
.
- So, for example, if output is a string like
- Shortest code wins.
Examples of bad answers
- This Java answer uses
java.util.Date
andjava.text.SimpleDateFormat
. - This Japt answer has
ÐU s3
; it converts to date object, and then ISO string. - This ksh answer has... wtf?
Test cases
0
-> 1970/01/01 00:00:00
999999999
-> 2001/09/09 01:46:39
1145141919
-> 2006/04/15 22:58:39
1330500000
-> 2012/02/29 07:20:00
1633773293
-> 2021/10/09 09:54:53
2147483647
-> 2038/01/19 03:14:07
Hint
Here are implementations:
- minix-server/gmtime.c
- シェルスクリプトで時間計算を一人前にこなす: AWK implementation.
- Try it online! I translated comments.
Related problems
Meta
- I once posted here but it's closed.
- TODO. define what builtins are.
- The most problem is that that KSH answer has a function that directly converts to the objective string; should I prohibit it?
- Maybe I wanted to say no date objects allowed.
- Is what is so-called date object ambiguos?