-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
f_time.c
53 lines (37 loc) · 1.43 KB
/
f_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
# include "f_time.h"
# include <time.h>
# include <string.h>
# define OPTIONS_DATE "DATE"
# define OPTIONS_TIME "TIME"
# define FORMAT_DATE "%Y-%m-%d"
# define FORMAT_TIME "%H:%M:%S"
# define FORMAT_DATE_TIME FORMAT_DATE " " FORMAT_TIME
enum datetime_type{ LOCAL, UTC };
static int format_datetime(struct fmt_stream * out, const char * options, enum datetime_type type, time_t * timestamp){
const struct tm * datetime = NULL;
char buffer[256] = { 0 };
time_t tmp;
if(timestamp == NULL){
(void)time(timestamp = &tmp);
}
datetime = ( type == LOCAL ? localtime(timestamp) : gmtime(timestamp) );
if(options == NULL){
options = FORMAT_DATE_TIME;
}else if( strcmp(options, OPTIONS_DATE) == 0 ){
options = FORMAT_DATE;
}else if( strcmp(options, OPTIONS_TIME) == 0){
options = FORMAT_TIME;
}
if( strftime(buffer, sizeof(buffer), options, datetime) == 0 ){
return(FMT_ERROR);
}
return( fmt_print_builtin(out, "%s", buffer) );
}
int format_local_datetime(struct fmt_stream * out, const char * id, const char * options, va_list * arg){
(void)id;
return( format_datetime(out, options, LOCAL, va_arg(*arg, time_t *)) );
}
int format_utc_datetime(struct fmt_stream * out, const char * id, const char * options, va_list * arg){
(void)id;
return( format_datetime(out, options, UTC, va_arg(*arg, time_t *)) );
}