-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.c
71 lines (60 loc) · 2.01 KB
/
util.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* Copyright (c) 2014 Kirk Scheibelhut <[email protected]>
*
* This file is free software: you may copy, redistribute and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 2 of the License, or (at your
* option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <stdint.h>
#include <time.h>
#include <ctype.h>
#include "date.h"
#include "activity.h"
#include "util.h"
/* Parse an ISO_8601 timestamp */
uint32_t parse_timestamp(const char *date) {
unsigned long timestamp;
int offset;
return (parse_date_basic(date, ×tamp, &offset) < 0)
? UNSET_FIELD
: (uint32_t)timestamp;
}
int format_timestamp(char *buf, uint32_t timestamp) {
time_t time = (time_t)timestamp;
struct tm *tm = gmtime(&time);
return !strftime(buf, TIME_BUFSIZ, "%Y-%m-%dT%H:%M:%SZ", tm) ? -1 : 0;
}
char *change_extension(char *filename, char *ext) {
char *cur = extension(filename);
if (strlen(cur) != strlen(ext)) return NULL;
strcpy(cur, ext);
return filename;
}
FileFormat file_format(char *ext) {
if (!strcmp("csv", ext)) return CSV;
if (!strcmp("gpx", ext)) return GPX;
if (!strcmp("tcx", ext)) return TCX;
if (!strcmp("fit", ext)) return FIT;
return UnknownFileFormat;
}
double parse_field(DataField field, DataPoint *dp, const char *str) {
char *end;
if (!str) return (dp->data[field] = UNSET_FIELD);
if (field == Timestamp) {
dp->data[field] = parse_timestamp(str);
} else {
dp->data[field] = strtod(str, &end);
if (*end && !isspace(*end)) dp->data[field] = UNSET_FIELD;
}
return dp->data[field];
}