Standardize battery.c
[dwm-status.git] / clock.c
blob72325868d5a743a3b2334af557ef3080fbed0135
1 /*-
2 * "THE BEER-WARE LICENSE" (Revision 42):
3 * <tobias.rehbein@web.de> wrote this file. As long as you retain this notice
4 * you can do whatever you want with this stuff. If we meet some day, and you
5 * think this stuff is worth it, you can buy me a beer in return.
6 * Tobias Rehbein
7 */
9 #include <assert.h>
10 #include <err.h>
11 #include <stdlib.h>
12 #include <sysexits.h>
13 #include <time.h>
15 #include "buffers.h"
16 #include "clock.h"
18 struct clock_context {
19 char clock_str[CLOCK_BUFFLEN];
22 struct clock_context *
23 clock_context_open()
25 struct clock_context *ctx;
27 if ((ctx = malloc(sizeof(*ctx))) == NULL)
28 err(EX_SOFTWARE, "malloc(%d) clock_context", sizeof(struct clock_context));
30 return (ctx);
33 void
34 clock_context_close(struct clock_context *ctx)
36 assert(ctx != NULL);
38 free(ctx);
41 char *
42 clock_str(struct clock_context *ctx)
44 time_t t;
45 struct tm *tm;
47 assert(ctx != NULL);
49 if (time(&t) == (time_t) (-1))
50 errx(EX_SOFTWARE, "time()");
51 if ((tm = localtime(&t)) == NULL)
52 errx(EX_SOFTWARE, "localtime()");
54 strftime(ctx->clock_str, sizeof(ctx->clock_str), "%c", tm);
56 return (ctx->clock_str);