fixed
This commit is contained in:
parent
bfb3d3b2fb
commit
04ff3cf8e7
8 changed files with 135 additions and 132 deletions
|
@ -22,4 +22,3 @@ char *mu_humansize(off_t n, int block) {
|
|||
|
||||
return mu_hs_buf;
|
||||
}
|
||||
|
||||
|
|
41
libmu/parse_date.c
Normal file
41
libmu/parse_date.c
Normal file
|
@ -0,0 +1,41 @@
|
|||
#define _XOPEN_SOURCE
|
||||
#define _POSIX_C_SOURCE 1999309L
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
#include "parse_date.h"
|
||||
|
||||
time_t mu_parse_date(const char *prog_name, const char *str) {
|
||||
time_t local = time(NULL);
|
||||
struct tm *tm = localtime(&local);
|
||||
|
||||
char flag = 0;
|
||||
for (size_t i = 0; i < sizeof(mu_date_fmts) / sizeof(char *); i++) {
|
||||
char *res = strptime(str, mu_date_fmts[i], tm);
|
||||
if (res && *res == '\0') {
|
||||
flag = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (flag == 0 && prog_name) {
|
||||
fprintf(stderr, "%s: parsing: invalid format\n", prog_name);
|
||||
return -1;
|
||||
}
|
||||
|
||||
time_t rt = mktime(tm);
|
||||
if (rt < 0) {
|
||||
if (rt == -1 && prog_name)
|
||||
fprintf(stderr, "%s: parsing: %s\n", prog_name, strerror(errno));
|
||||
|
||||
else if (prog_name)
|
||||
fprintf(stderr, "%s: parsing: invalid date\n", prog_name);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
return rt;
|
||||
}
|
18
libmu/parse_date.h
Normal file
18
libmu/parse_date.h
Normal file
|
@ -0,0 +1,18 @@
|
|||
#ifndef _PARSE_DATE_H
|
||||
#define _PARSE_DATE_H
|
||||
|
||||
static const char *mu_date_fmts[] = {
|
||||
"%R",
|
||||
"%T",
|
||||
"%d-%m",
|
||||
"%d-%m %H",
|
||||
"%d-%m %R",
|
||||
"%d-%m %T",
|
||||
"%d-%m-%Y",
|
||||
"%d-%m-%Y %H",
|
||||
"%d-%m-%Y %R",
|
||||
"%d-%m-%Y %T"
|
||||
};
|
||||
|
||||
time_t mu_parse_date(const char *prog_name, const char *str);
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue