aboutsummaryrefslogtreecommitdiffstats
path: root/src/fsm
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2015-12-28 22:23:02 +0100
committerMatthias P. Braendli <matthias.braendli@mpb.li>2015-12-28 22:23:02 +0100
commited5bc75f9f46379f0eca7d91939164963a43ec37 (patch)
treec18f9d0a5b704c1b9174820cb8239df6c64e8d0e /src/fsm
parentc012d314f85e9facc7973e3fa4b05fb26abb5be9 (diff)
downloadglutte-o-matic-ed5bc75f9f46379f0eca7d91939164963a43ec37.tar.gz
glutte-o-matic-ed5bc75f9f46379f0eca7d91939164963a43ec37.tar.bz2
glutte-o-matic-ed5bc75f9f46379f0eca7d91939164963a43ec37.zip
Add dayofweek function
Diffstat (limited to 'src/fsm')
-rw-r--r--src/fsm/common.c17
-rw-r--r--src/fsm/common.h9
2 files changed, 26 insertions, 0 deletions
diff --git a/src/fsm/common.c b/src/fsm/common.c
index ca4c009..517eb80 100644
--- a/src/fsm/common.c
+++ b/src/fsm/common.c
@@ -26,6 +26,7 @@
#include "FreeRTOS.h"
#include "timers.h"
#include <stm32f4xx.h>
+#include <time.h>
static uint64_t common_timestamp = 0; // milliseconds since startup
static TimerHandle_t common_timer;
@@ -60,6 +61,22 @@ uint64_t timestamp_now(void)
return common_timestamp;
}
+int dayofweek(uint8_t day, uint8_t month, uint16_t year)
+{
+ /* Zeller's congruence for the Gregorian calendar.
+ * With 0=Monday, ... 5=Saturday, 6=Sunday
+ */
+ if (month < 3) {
+ month += 12;
+ year--;
+ }
+ int k = year % 100;
+ int j = year / 100;
+ int h = day + 13*(month+1)/5 + k + k/4 + j/4 + 5*j;
+ return (h + 5) % 7 + 1;
+}
+
+
// Return either 0 or 1, somewhat randomly
int random_bool(void)
{
diff --git a/src/fsm/common.h b/src/fsm/common.h
index b33f1f8..a2d20ab 100644
--- a/src/fsm/common.h
+++ b/src/fsm/common.h
@@ -37,6 +37,15 @@ void common_init(void);
// wall clock time.
uint64_t timestamp_now(void);
+/* Convert date to day of week.
+ * day: 1-31
+ * month: 1-12
+ * year: 1900 to 3000
+ *
+ * Returns 1=Monday, ... 6=Saturday, 7=Sunday
+ */
+int dayofweek(uint8_t day, uint8_t month, uint16_t year);
+
// Return either 0 or 1, somewhat randomly
int random_bool(void);