get weekday out of date.

Do anyone of you guys have a function that takes a date and returns the day of the week.
as there are mathematical rules to what day it is, there should be possible to make such a function, right?

I have seen that some of the clock libraries does this, but i would like a pure function for it. Made a pathetic test myself and i figured that someone out there has already done this:)

//given unix time t, returns day of week Sun-Sat as an integer 0-6
uint8_t dow(unsigned long t)
{
    return ((t / 86400) + 4) % 7;
}

Nice! but how do i convert my string(or a bunch of Int:s contining hours, minutes, seconds, days, year etc) to unix time.. im back at using the library for time for this and that feels like a way around the problem? or is it the only solution?

Yamat0:
Nice! but how do i convert my string(or a bunch of Int:s contining hours, minutes, seconds, days, year etc) to unix time.. im back at using the library for time for this and that feels like a way around the problem? or is it the only solution?

Ah! Well you didn't specify, how could a person know?

I'd probably just use the Time library unless I had a specific reason not to. It may not be The Best Way (seeing that most things can be improved upon) and it's certainly not the only solution.

Do you have code you could post?

I wouldn't disagree that this maybe seems like a lot of work and perhaps brute-force-ish, but it works fine. I use the Time library a lot and haven't felt a need to reinvent that wheel.

#include <Time.h>           //http://www.arduino.cc/playground/Code/Time  

void setup(void)
{
    tmElements_t tm;
    time_t t;

    delay(2000);
    Serial.begin(9600);
    
    tm.Hour = 12;             //set the tm structure to 12h34m56s on 01Sep2013
    tm.Minute = 34;
    tm.Second = 56;
    tm.Year = 2013 - 1970;    //tmElements_t.Year is the offset from 1970.
    tm.Month = 9;
    tm.Day = 1;
    
    t = makeTime(tm);
    Serial.print(dayStr(weekday(t)));
    Serial.print(' ');
    printI00(day(t), 0);
    Serial.print(monthShortStr(month(t)));
    Serial.print(year(t));
    Serial.print(' ');
    printI00(hour(t), ':');
    printI00(minute(t), ':');
    printI00(second(t), ' ');
    Serial.print("weekday=");
    Serial.print(weekday(t));    //Sun=1, Mon=2, ... Sat=7
}

void loop(void)
{
}

//Print an integer in "00" format (with leading zero),
//followed by a delimiter character to Serial.
//Input value assumed to be between 0 and 99.
void printI00(int val, char delim)
{
    if (val < 10) Serial.print('0');
    Serial.print(val);
    if (delim > 0) Serial.print(delim);
    return;
}

Jack: I will try your approach, even if think there must be a simpler way.. i dont know how big the Time library is, but it feels like im including it for one small feature...

Some fiddling with days ==> this code Warning: not tested thoroughly, use at own risk

//    FILE: dayOfWeek.ino
//  AUTHOR: Rob Tillaart
// VERSION: 2013-09-01
// PURPOSE: experimental day of week code (hardly tested)
// Released to the public domain

void setup()
{
  Serial.begin(9600);
  Serial.println(dayOfWeek(2013,9,1));
}

void loop(){}

#define LEAP_YEAR(Y)     ( (Y>0) && !(Y%4) && ( (Y%100) || !(Y%400) ))     // from time-lib

int dayOfWeek(uint16_t year, uint8_t month, uint8_t day)
{
  uint16_t months[] = {
    0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365         };   // days until 1st of month

  uint32_t days = year * 365;        // days until year 
  for (uint16_t i = 4; i < year; i += 4) if (LEAP_YEAR(i) ) days++;     // adjust leap years, test only multiple of 4 of course

  days += months[month-1] + day;    // add the days of this year
  if ((month > 2) && LEAP_YEAR(year)) days++;  // adjust 1 if this year is a leap year, but only after febr

  return days % 7;   // remove all multiples of 7
}

Thank you robtillaart!! this is exactly what i was looking for:)

I will try to use this and see if I get it to work in my application!!

Cheers to all of you who help out here! you are awesome!

you see that the code is quite simple, it just adds up days in known quantities.
The corrections for leap years/day are the difficult part.

Near one year after...Thank You!!!
Will try on my Yùn because the time library doesnt compile on it :slight_smile:

let us know if it works on the yun

Hi there!
I have a little problem with weekday() function.
From the current day, i would like to scroll the next days pushing a button.
Something like this:

if (btn03Pressed) { 
RTC.hour++;
 // dopo 23 torna a 0 
if (RTC.hour > 23) RTC.hour = 0;
 // scrivi nell'RTC l'ora impostata 
RTC.writeRTC(); 
// dopo aver gestito l'evento azzero il flag
 btn03Pressed = false; }

I'm using Wire and DS1307 RTC library.
Can anyone help me?
Thanks very much.

Regards