Day of the week with seconds from the era

I am trying to calculate the day of the week from seconds from the point in time. Of time.hI can use gmtime(), but it increases the program from 1.3kB, possibly because it gmtime()also calculates the date.

So, I am wondering what would be wrong in using something like:

(timestamp / (24*3600)) % 7

The only thing I could think of was leaps of seconds, so something like 00:00:02 could be called the wrong day.

edit This is for embedded programming, 1.3kB is an essential part of the 64kB program / firmware. Also, I do not expect to do anything with time zones or dates after that.

+4
source share
2 answers

, , , (, ), , (, ). . 8601 . , 24*3600, 16- int/unsigned .

(timestamp / (24*3600)) % 7  // OP code

: , 3 (: 0, : 3). : , .

#define EPOCH_DOW 3
#define SECS_PER_DAY 86400 
dow = ((timestamp / SECS_PER_DAY) + EPOCH_DOW) % 7;

timestamp , , [0...6].

dow = (dow + 7) % 7;
// or 
if (dow < 0) dow += 7;  

, . , , , , - .

0

4, 1 1970 , % . ( - , , , 7.) ; , glibc __offtime, gmtime. ( , ).

; Unix- .

( ) , gmtime #if 0, , / .

+1

All Articles