/
ween-time.c
87 lines (72 loc) · 1.77 KB
/
ween-time.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include "time-utils.h"
#include "ween-time.h"
#define OCTOBER 9 /* 0 based month number */
#define WEEN_DAY 31
#define MINUTES(hour, minutes) ((hour)*60 + (minutes))
static bool
day_is_valid(struct tm *tm, int within_days_of)
{
if (within_days_of < 0) return true;
if (tm->tm_mon != OCTOBER) return false;
return tm->tm_mday >= WEEN_DAY - within_days_of;
}
int
ween_time_is_valid(ween_time_constraint_t *c, size_t n)
{
time_t now = time(NULL);
struct tm tm;
size_t i;
if (! localtime_r(&now, &tm)) {
return true;
}
for (i = 0; i < n; i++) {
if (day_is_valid(&tm, c[i].within_days_of)) {
unsigned now = MINUTES(tm.tm_hour, tm.tm_min);
unsigned start = MINUTES(c[i].start_h, c[i].start_m);
unsigned end = MINUTES(c[i].end_h, c[i].end_m);
if (start <= now && now < end) {
return true;
}
}
}
return false;
}
static void
print_constraints(ween_time_constraint_t *c, size_t n)
{
size_t i;
for (i = 0; i < n; i++) {
fprintf(stderr, " %d %d:%02d .. %d:%02d\n", c[i].within_days_of, c[i].start_h, c[i].start_m, c[i].end_h, c[i].end_m);
}
}
void
ween_time_wait_until_valid(ween_time_constraint_t *c, size_t n)
{
int logged = false;
while (! ween_time_is_valid(c, n)) {
if (! logged) {
fprintf(stderr, "Waiting for a valid time:\n");
print_constraints(c, n);
logged = true;
}
ms_sleep(60*1000);
}
if (logged) {
fprintf(stderr, "Ween time constraint met\n");
}
}
bool
ween_time_is_halloween()
{
ween_time_constraint_t c = { 30, 0, 0, 23, 59 };
return ween_time_is_valid(&c, 1);
}
bool
ween_time_is_trick_or_treating()
{
ween_time_constraint_t c = { 0, 16, 0, 21, 00 };
return ween_time_is_valid(&c, 1);
}