I have an UNIX Timestamp in my Arduino Sketch and would like to get the day of the week of this timestamp (1-7).
How can I convert the timestamp to get the day of the week?
Chetan Bhargava
3551 gold badge4 silver badges16 bronze badges
1 Answer 1
The simplest solution to that problem is to download the Time library and use the weekday() function:
int weekday(time_t t); // the weekday for the given time
Cheers!
answered Feb 13, 2016 at 23:52
-
1One caveat, the
breakTime()
code at line 168 of Time.cpp computesWday = (t/(60*60*24) + 4) % 7) + 1
, ie, it apparently ignores leap seconds [26 in number since 1972] so for 26 seconds before every midnight the day-of-the-week calc will be off by 1.James Waldby - jwpat7– James Waldby - jwpat72016年02月14日 00:12:32 +00:00Commented Feb 14, 2016 at 0:12 -
2Ignore previous comment. As noted in wikipedia's Unix time article, Unix timestamps don't count leap seconds, so they are locally correct and calculations near midnight will be ok, except during a leap second itself. Also see Eric Raymond's Time, Clock, and Calendar Programming In C article, and the article Unix leap seconds.James Waldby - jwpat7– James Waldby - jwpat72016年02月14日 00:58:12 +00:00Commented Feb 14, 2016 at 0:58
-
UNIX timestamps are also kept in GMT/UTC time. So you may run into problems with it being some hours out. If so look to the functions gmtime() Vs locatime(). You may need to set your time-zone. Oh and watch out for daylight savings time if it matters for your project.Kingsley– Kingsley2016年02月15日 03:30:00 +00:00Commented Feb 15, 2016 at 3:30
date +%s
says 1455407637 (for Sat Feb 13 16:53:57 MST 2016),date -d@1455407637 +%u
says 6.