-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatetime.c
74 lines (59 loc) · 1.62 KB
/
datetime.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
/*
* Copyright (c) 2018 Carlin Bingham <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <err.h>
#include <pthread.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include "calmstatus.h"
void *
watch_for_datetime_changes(void *arg)
{
time_t clock;
struct tm *tp;
procinfo_t *info;
info = (procinfo_t *)arg;
if ((clock = time(NULL)) == -1)
err(1, "time");
if ((tp = localtime(&clock)) == NULL)
err(1, "localtime");
sleep(60 - tp->tm_sec);
for (;;) {
do_output(info);
sleep(60);
}
}
void
print_datetime()
{
time_t clock;
struct tm *tp;
char buf[32];
if ((clock = time(NULL)) == -1)
err(1, "time");
if ((tp = localtime(&clock)) == NULL)
errx(1, "localtime");
if (strftime(buf, sizeof(buf), "%a %d %b %H:%M", tp) == 0)
errx(1, "strftime");
printf("%s", buf);
}
int
datetime_main(procinfo_t *info)
{
pthread_t thr;
pthread_create(&thr, NULL, watch_for_datetime_changes, info);
return 0;
}