1

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
asked Feb 13, 2016 at 23:23
1
  • Do you mean, your program will be given a UNIX Timestamp number as data and should produce a day-of-the-week number? Or that you have a timestamp number and want to know its day-of-the-week number? If the latter, the Unix date command can tell you, via format code +%u. Eg, if date +%s says 1455407637 (for Sat Feb 13 16:53:57 MST 2016), date -d@1455407637 +%u says 6. Commented Feb 13, 2016 at 23:58

1 Answer 1

2

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
3
  • 1
    One caveat, the breakTime() code at line 168 of Time.cpp computes Wday = (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. Commented Feb 14, 2016 at 0:12
  • 2
    Ignore 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. Commented 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. Commented Feb 15, 2016 at 3:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.