snac2/xs_time.h

60 lines
1.3 KiB
C
Raw Normal View History

2023-01-17 08:50:16 +00:00
/* copyright (c) 2022 - 2023 grunfink / MIT license */
2022-10-02 07:23:04 +00:00
#ifndef _XS_TIME_H
#define _XS_TIME_H
#include <time.h>
2023-01-28 16:49:02 +00:00
xs_str *xs_str_time(time_t t, const char *fmt, int local);
2022-10-02 07:23:04 +00:00
#define xs_str_localtime(t, fmt) xs_str_time(t, fmt, 1)
#define xs_str_utctime(t, fmt) xs_str_time(t, fmt, 0)
time_t xs_parse_time(const char *str, const char *fmt, int local);
#define xs_parse_localtime(str, fmt) xs_parse_time(str, fmt, 1)
#define xs_parse_utctime(str, fmt) xs_parse_time(str, fmt, 0)
#ifdef XS_IMPLEMENTATION
2023-01-28 16:49:02 +00:00
xs_str *xs_str_time(time_t t, const char *fmt, int local)
/* returns a string with a formated time */
2022-10-02 07:23:04 +00:00
{
struct tm tm;
char tmp[64];
if (t == 0)
t = time(NULL);
if (local)
localtime_r(&t, &tm);
else
gmtime_r(&t, &tm);
strftime(tmp, sizeof(tmp), fmt, &tm);
// printf("%d %d\n", local, t - xs_parse_time(tmp, fmt, local));
return xs_str_new(tmp);
}
char *strptime(const char *s, const char *format, struct tm *tm);
time_t xs_parse_time(const char *str, const char *fmt, int local)
{
struct tm tm;
memset(&tm, '\0', sizeof(tm));
strptime(str, fmt, &tm);
2022-10-04 16:46:12 +00:00
/* try to guess the Daylight Saving Time */
2022-10-02 07:23:04 +00:00
if (local)
tm.tm_isdst = -1;
return local ? mktime(&tm) : timegm(&tm);
}
#endif /* XS_IMPLEMENTATION */
#endif /* _XS_TIME_H */