mirror of
https://github.com/owntracks/recorder.git
synced 2024-11-15 09:58:40 -07:00
on the way to better times (time_t)
This commit is contained in:
parent
319a81452e
commit
8175ce9e38
42
ocat.c
42
ocat.c
@ -6,13 +6,6 @@
|
||||
#include "json.h"
|
||||
#include "storage.h"
|
||||
|
||||
static char *tstampyyyymm(time_t t) {
|
||||
static char buf[] = "YYYY-MM";
|
||||
|
||||
strftime(buf, sizeof(buf), "%Y-%m", gmtime(&t));
|
||||
return(buf);
|
||||
}
|
||||
|
||||
void usage(char *prog)
|
||||
{
|
||||
printf("Usage: %s [options..] [file ...]\n", prog);
|
||||
@ -20,7 +13,14 @@ void usage(char *prog)
|
||||
printf(" --list -l list users (or a user's (-u) devices\n");
|
||||
printf(" --user username -u specify username\n");
|
||||
printf(" --device devicename -d specify device name\n");
|
||||
printf(" --yyyymm YYYY-MM -D specify year-month (shell pat, eg: '2015-0[572]')\n");
|
||||
printf(" --from <time> -F from date/time\n");
|
||||
printf(" --to <time> -T to date/time\n");
|
||||
printf(" specify <time> as YYYY-MM-DDTHH:MM:SS\n");
|
||||
printf(" YYYY-MM-DDTHH:MM\n");
|
||||
printf(" YYYY-MM-DDTHH\n");
|
||||
printf(" YYYY-MM-DD\n");
|
||||
printf(" YYYY-MM\n");
|
||||
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@ -29,12 +29,11 @@ int main(int argc, char **argv)
|
||||
char *progname = *argv;
|
||||
int c;
|
||||
int list = 0;
|
||||
char *username = NULL, *device = NULL, *yyyymm = NULL;
|
||||
char *username = NULL, *device = NULL, *time_from = NULL, *time_to = NULL;
|
||||
JsonNode *json, *obj, *locs;
|
||||
time_t now;
|
||||
time_t now, s_lo, s_hi;
|
||||
|
||||
time(&now);
|
||||
yyyymm = tstampyyyymm(now);
|
||||
|
||||
while (1) {
|
||||
static struct option long_options[] = {
|
||||
@ -42,12 +41,13 @@ int main(int argc, char **argv)
|
||||
{ "list", no_argument, 0, 'l'},
|
||||
{ "user", required_argument, 0, 'u'},
|
||||
{ "device", required_argument, 0, 'd'},
|
||||
{ "yyyymm", required_argument, 0, 'D'},
|
||||
{ "from", required_argument, 0, 'F'},
|
||||
{ "to", required_argument, 0, 'T'},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
int optindex = 0;
|
||||
|
||||
c = getopt_long(argc, argv, "hlu:d:D:", long_options, &optindex);
|
||||
c = getopt_long(argc, argv, "hlu:d:F:T:", long_options, &optindex);
|
||||
if (c == -1)
|
||||
break;
|
||||
|
||||
@ -61,8 +61,11 @@ int main(int argc, char **argv)
|
||||
case 'd':
|
||||
device = strdup(optarg);
|
||||
break;
|
||||
case 'D':
|
||||
yyyymm = strdup(optarg);
|
||||
case 'F':
|
||||
time_from = strdup(optarg);
|
||||
break;
|
||||
case 'T':
|
||||
time_to = strdup(optarg);
|
||||
break;
|
||||
case 'h':
|
||||
case '?':
|
||||
@ -83,10 +86,15 @@ int main(int argc, char **argv)
|
||||
return (-2);
|
||||
}
|
||||
|
||||
if (make_times(time_from, &s_lo, time_to, &s_hi) != 1) {
|
||||
fprintf(stderr, "%s: bad time(s) specified\n", progname);
|
||||
return (-2);
|
||||
}
|
||||
|
||||
if (list) {
|
||||
char *js;
|
||||
|
||||
json = lister(username, device, yyyymm);
|
||||
json = lister(username, device, s_lo, s_hi);
|
||||
if (json == NULL) {
|
||||
fprintf(stderr, "%s: cannot list\n", progname);
|
||||
exit(2);
|
||||
@ -131,7 +139,7 @@ int main(int argc, char **argv)
|
||||
} else {
|
||||
JsonNode *arr, *f;
|
||||
|
||||
if ((json = lister(username, device, yyyymm)) != NULL) {
|
||||
if ((json = lister(username, device, s_lo, s_hi)) != NULL) {
|
||||
if ((arr = json_find_member(json, "results")) != NULL) { // get array
|
||||
json_foreach(f, arr) {
|
||||
printf("%s\n", f->string_);
|
||||
|
77
storage.c
77
storage.c
@ -17,6 +17,7 @@ void get_geo(JsonNode *o, char *ghash)
|
||||
static UT_string *addr = NULL, *cc = NULL;
|
||||
static struct udata udata;
|
||||
|
||||
/* FIXME!!!! */
|
||||
udata.usefiles = 1;
|
||||
|
||||
utstring_renew(addr);
|
||||
@ -28,6 +29,72 @@ void get_geo(JsonNode *o, char *ghash)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* `s' has a time string in it. Try to convert into time_t
|
||||
* using a variety of formats from higher to lower precision.
|
||||
* Return 1 on success, 0 on failure.
|
||||
*/
|
||||
|
||||
static int str_time_to_secs(char *s, time_t *secs)
|
||||
{
|
||||
static char **f, *formats[] = {
|
||||
"%Y-%m-%dT%H:%M:%S",
|
||||
"%Y-%m-%dT%H:%M",
|
||||
"%Y-%m-%dT%H",
|
||||
"%Y-%m-%d",
|
||||
"%Y-%m",
|
||||
NULL
|
||||
};
|
||||
struct tm tm;
|
||||
int success = 0;
|
||||
|
||||
memset(&tm, 0, sizeof(struct tm));
|
||||
tm.tm_isdst = -1; /* A negative value for tm_isdst causes
|
||||
* the mktime() function to attempt to
|
||||
* divine whether summer time is in
|
||||
* effect for the specified time. */
|
||||
for (f = formats; f && *f; f++) {
|
||||
if (strptime(s, *f, &tm) != NULL) {
|
||||
success = 1;
|
||||
printf("str_time_to_secs succeeds with %s\n", *f);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!success)
|
||||
return (0);
|
||||
|
||||
// tm.tm_mday = tm.tm_hour = 0;
|
||||
// tm.tm_hour = tm.tm_min = tm.tm_sec = 1;
|
||||
*secs = mktime(&tm);
|
||||
printf("str_time_to_secs: %s becomes %04d-%02d-%02d %02d:%02d:%02d\n",
|
||||
s,
|
||||
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
|
||||
tm.tm_hour, tm.tm_min, tm.tm_sec);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int make_times(char *time_from, time_t *s_lo, char *time_to, time_t *s_hi)
|
||||
{
|
||||
time_t now;
|
||||
|
||||
time(&now);
|
||||
if (!time_from || !*time_from || !time_to || !*time_to) {
|
||||
if (!time_from || !*time_from) {
|
||||
*s_lo = now - (60 * 60 * 6);
|
||||
}
|
||||
if (!time_to || !*time_to) {
|
||||
*s_hi = now;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
if (str_time_to_secs(time_from, s_lo) == 0 ||
|
||||
str_time_to_secs(time_to, s_hi) == 0)
|
||||
return (0);
|
||||
return (*s_lo > *s_hi ? 0 : 1);
|
||||
}
|
||||
|
||||
/*
|
||||
* List the directories in the directory at `path' and put
|
||||
* the names into a JSON array which is in obj.
|
||||
@ -61,6 +128,7 @@ static void ls(char *path, JsonNode *obj)
|
||||
* put the names into a JSON array in obj.
|
||||
*/
|
||||
|
||||
#if 0
|
||||
static void lsglob(char *pathpat, JsonNode *obj)
|
||||
{
|
||||
glob_t paths;
|
||||
@ -86,21 +154,24 @@ static void lsglob(char *pathpat, JsonNode *obj)
|
||||
json_append_member(obj, "results", jarr);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* If `user' and `device' are both NULL, return list of users.
|
||||
* If `user` is specified, and device is NULL, return user's devices
|
||||
* If both user and device are specified, return list of .rec files;
|
||||
* in that case, limit with `yyyymm' which is YYYY-MM
|
||||
* in that case, limit with `s_lo` and `s_hi`
|
||||
*/
|
||||
|
||||
JsonNode *lister(char *user, char *device, char *yyyymm)
|
||||
JsonNode *lister(char *user, char *device, time_t s_lo, time_t s_hi)
|
||||
{
|
||||
JsonNode *json = json_mkobject();
|
||||
UT_string *path = NULL;
|
||||
|
||||
utstring_renew(path);
|
||||
|
||||
printf("%ld %ld\n", s_lo, s_hi);
|
||||
|
||||
/* FIXME: lowercase user/device */
|
||||
|
||||
if (!user && !device) {
|
||||
@ -109,11 +180,13 @@ JsonNode *lister(char *user, char *device, char *yyyymm)
|
||||
} else if (!device) {
|
||||
utstring_printf(path, "%s/rec/%s", STORAGEDIR, user);
|
||||
ls(utstring_body(path), json);
|
||||
#if 0
|
||||
} else {
|
||||
utstring_printf(path, "%s/rec/%s/%s/%s.rec",
|
||||
STORAGEDIR, user, device,
|
||||
(yyyymm) ? yyyymm : "*");
|
||||
lsglob(utstring_body(path), json);
|
||||
#endif
|
||||
}
|
||||
|
||||
return (json);
|
||||
|
@ -1,10 +1,12 @@
|
||||
#ifndef _STORAGE_H_INCL_
|
||||
# define _STORAGE_H_INCL_
|
||||
|
||||
#include <time.h>
|
||||
#include "json.h"
|
||||
|
||||
|
||||
JsonNode *lister(char *username, char *device, char *yyyymm);
|
||||
JsonNode *lister(char *username, char *device, time_t s_lo, time_t s_hi);
|
||||
void locations(char *filename, JsonNode *obj, JsonNode *arr);
|
||||
int make_times(char *time_from, time_t *s_lo, char *time_to, time_t *s_to);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user