aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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);