mirror of
https://github.com/s3fs-fuse/s3fs-fuse.git
synced 2024-09-28 17:29:02 +00:00
Use std::get_time instead in strptime polyfill
This commit is contained in:
parent
d95a612548
commit
3ac9f571f5
232
src/strptime.cpp
232
src/strptime.cpp
@ -1,233 +1,23 @@
|
|||||||
/*
|
/*
|
||||||
* Polyfill for strptime function
|
* Polyfill for strptime function
|
||||||
*
|
*
|
||||||
* This source code is from musl library, located at https://git.musl-libc.org/cgit/musl/tree/src/time/strptime.c .
|
* This source code is from https://gist.github.com/jeremyfromearth/5694aa3a66714254752179ecf3c95582 .
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
#include <iostream>
|
||||||
* musl as a whole is licensed under the following standard MIT license:
|
#include <time.h>
|
||||||
*
|
#include <iomanip>
|
||||||
* ----------------------------------------------------------------------
|
#include <sstream>
|
||||||
* Copyright © 2005-2020 Rich Felker, et al.
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining
|
|
||||||
* a copy of this software and associated documentation files (the
|
|
||||||
* "Software"), to deal in the Software without restriction, including
|
|
||||||
* without limitation the rights to use, copy, modify, merge, publish,
|
|
||||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
||||||
* permit persons to whom the Software is furnished to do so, subject to
|
|
||||||
* the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
||||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
||||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
||||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
||||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
* ----------------------------------------------------------------------
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "strptime.h"
|
#include "strptime.h"
|
||||||
|
|
||||||
char* strptime(const char* s, const char* f, struct tm* tm)
|
char* strptime(const char* s, const char* f, struct tm* tm)
|
||||||
{
|
{
|
||||||
int i, w, neg, adj, min, range, *dest, dummy;
|
std::istringstream input(s);
|
||||||
const char *ex;
|
input.imbue(std::locale(setlocale(LC_ALL, nullptr)));
|
||||||
size_t len;
|
input >> std::get_time(tm, f);
|
||||||
int want_century = 0, century = 0, relyear = 0;
|
if (input.fail()) {
|
||||||
while (*f) {
|
return nullptr;
|
||||||
if (*f != '%') {
|
|
||||||
if (isspace(*f)) for (; *s && isspace(*s); s++);
|
|
||||||
else if (*s != *f) return 0;
|
|
||||||
else s++;
|
|
||||||
f++;
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
f++;
|
return (char*)(s + input.tellg());
|
||||||
if (*f == '+') f++;
|
|
||||||
if (isdigit(*f)) {
|
|
||||||
char *new_f;
|
|
||||||
w=strtoul(f, &new_f, 10);
|
|
||||||
f = new_f;
|
|
||||||
} else {
|
|
||||||
w=-1;
|
|
||||||
}
|
|
||||||
adj=0;
|
|
||||||
switch (*f++) {
|
|
||||||
case 'a': case 'A':
|
|
||||||
dest = &tm->tm_wday;
|
|
||||||
min = ABDAY_1;
|
|
||||||
range = 7;
|
|
||||||
goto symbolic_range;
|
|
||||||
case 'b': case 'B': case 'h':
|
|
||||||
dest = &tm->tm_mon;
|
|
||||||
min = ABMON_1;
|
|
||||||
range = 12;
|
|
||||||
goto symbolic_range;
|
|
||||||
case 'c':
|
|
||||||
s = strptime(s, nl_langinfo(D_T_FMT), tm);
|
|
||||||
if (!s) return 0;
|
|
||||||
break;
|
|
||||||
case 'C':
|
|
||||||
dest = ¢ury;
|
|
||||||
if (w<0) w=2;
|
|
||||||
want_century |= 2;
|
|
||||||
goto numeric_digits;
|
|
||||||
case 'd': case 'e':
|
|
||||||
dest = &tm->tm_mday;
|
|
||||||
min = 1;
|
|
||||||
range = 31;
|
|
||||||
goto numeric_range;
|
|
||||||
case 'D':
|
|
||||||
s = strptime(s, "%m/%d/%y", tm);
|
|
||||||
if (!s) return 0;
|
|
||||||
break;
|
|
||||||
case 'H':
|
|
||||||
dest = &tm->tm_hour;
|
|
||||||
min = 0;
|
|
||||||
range = 24;
|
|
||||||
goto numeric_range;
|
|
||||||
case 'I':
|
|
||||||
dest = &tm->tm_hour;
|
|
||||||
min = 1;
|
|
||||||
range = 12;
|
|
||||||
goto numeric_range;
|
|
||||||
case 'j':
|
|
||||||
dest = &tm->tm_yday;
|
|
||||||
min = 1;
|
|
||||||
range = 366;
|
|
||||||
adj = 1;
|
|
||||||
goto numeric_range;
|
|
||||||
case 'm':
|
|
||||||
dest = &tm->tm_mon;
|
|
||||||
min = 1;
|
|
||||||
range = 12;
|
|
||||||
adj = 1;
|
|
||||||
goto numeric_range;
|
|
||||||
case 'M':
|
|
||||||
dest = &tm->tm_min;
|
|
||||||
min = 0;
|
|
||||||
range = 60;
|
|
||||||
goto numeric_range;
|
|
||||||
case 'n': case 't':
|
|
||||||
for (; *s && isspace(*s); s++);
|
|
||||||
break;
|
|
||||||
case 'p':
|
|
||||||
ex = nl_langinfo(AM_STR);
|
|
||||||
len = strlen(ex);
|
|
||||||
if (!strncasecmp(s, ex, len)) {
|
|
||||||
tm->tm_hour %= 12;
|
|
||||||
s += len;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
ex = nl_langinfo(PM_STR);
|
|
||||||
len = strlen(ex);
|
|
||||||
if (!strncasecmp(s, ex, len)) {
|
|
||||||
tm->tm_hour %= 12;
|
|
||||||
tm->tm_hour += 12;
|
|
||||||
s += len;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
case 'r':
|
|
||||||
s = strptime(s, nl_langinfo(T_FMT_AMPM), tm);
|
|
||||||
if (!s) return 0;
|
|
||||||
break;
|
|
||||||
case 'R':
|
|
||||||
s = strptime(s, "%H:%M", tm);
|
|
||||||
if (!s) return 0;
|
|
||||||
break;
|
|
||||||
case 'S':
|
|
||||||
dest = &tm->tm_sec;
|
|
||||||
min = 0;
|
|
||||||
range = 61;
|
|
||||||
goto numeric_range;
|
|
||||||
case 'T':
|
|
||||||
s = strptime(s, "%H:%M:%S", tm);
|
|
||||||
if (!s) return 0;
|
|
||||||
break;
|
|
||||||
case 'U':
|
|
||||||
case 'W':
|
|
||||||
/* Throw away result, for now. (FIXME?) */
|
|
||||||
dest = &dummy;
|
|
||||||
min = 0;
|
|
||||||
range = 54;
|
|
||||||
goto numeric_range;
|
|
||||||
case 'w':
|
|
||||||
dest = &tm->tm_wday;
|
|
||||||
min = 0;
|
|
||||||
range = 7;
|
|
||||||
goto numeric_range;
|
|
||||||
case 'x':
|
|
||||||
s = strptime(s, nl_langinfo(D_FMT), tm);
|
|
||||||
if (!s) return 0;
|
|
||||||
break;
|
|
||||||
case 'X':
|
|
||||||
s = strptime(s, nl_langinfo(T_FMT), tm);
|
|
||||||
if (!s) return 0;
|
|
||||||
break;
|
|
||||||
case 'y':
|
|
||||||
dest = &relyear;
|
|
||||||
w = 2;
|
|
||||||
want_century |= 1;
|
|
||||||
goto numeric_digits;
|
|
||||||
case 'Y':
|
|
||||||
dest = &tm->tm_year;
|
|
||||||
if (w<0) w=4;
|
|
||||||
adj = 1900;
|
|
||||||
want_century = 0;
|
|
||||||
goto numeric_digits;
|
|
||||||
case '%':
|
|
||||||
if (*s++ != '%') return 0;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return 0;
|
|
||||||
numeric_range:
|
|
||||||
if (!isdigit(*s)) return 0;
|
|
||||||
*dest = 0;
|
|
||||||
for (i=1; i<=min+range && isdigit(*s); i*=10)
|
|
||||||
*dest = *dest * 10 + *s++ - '0';
|
|
||||||
if ((unsigned)(*dest - min) >= (unsigned)range) return 0;
|
|
||||||
*dest -= adj;
|
|
||||||
switch((char *)dest - (char *)tm) {
|
|
||||||
case offsetof(struct tm, tm_yday):
|
|
||||||
;
|
|
||||||
}
|
|
||||||
goto update;
|
|
||||||
numeric_digits:
|
|
||||||
neg = 0;
|
|
||||||
if (*s == '+') s++;
|
|
||||||
else if (*s == '-') neg=1, s++;
|
|
||||||
if (!isdigit(*s)) return 0;
|
|
||||||
for (*dest=i=0; i<w && isdigit(*s); i++)
|
|
||||||
*dest = *dest * 10 + *s++ - '0';
|
|
||||||
if (neg) *dest = -*dest;
|
|
||||||
*dest -= adj;
|
|
||||||
goto update;
|
|
||||||
symbolic_range:
|
|
||||||
for (i=0; i < range; i++) {
|
|
||||||
ex = nl_langinfo(min+i);
|
|
||||||
len = strlen(ex);
|
|
||||||
if (strncasecmp(s, ex, len)) continue;
|
|
||||||
s += len;
|
|
||||||
*dest = i % range;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (i<0) return 0;
|
|
||||||
goto update;
|
|
||||||
update:
|
|
||||||
//FIXME
|
|
||||||
;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (want_century) {
|
|
||||||
tm->tm_year = relyear;
|
|
||||||
if (want_century & 2) tm->tm_year += century * 100 - 1900;
|
|
||||||
else if (tm->tm_year <= 68) tm->tm_year += 100;
|
|
||||||
}
|
|
||||||
return (char *)s;
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user