Skip to content

Commit a278c1f

Browse files
caiiiycukkripken
authored andcommitted
strptime improvement: support %F (emscripten-core#6383)
1 parent 5b23199 commit a278c1f

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

src/library.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2550,6 +2550,7 @@ LibraryManager.library = {
25502550
'%c': '%x\\s+%X',
25512551
'%D': '%m\\/%d\\/%y',
25522552
'%e': '%d',
2553+
'%F': '%Y-%m-%d',
25532554
'%h': '%b',
25542555
'%R': '%H\\:%M',
25552556
'%r': '%I\\:%M\\:%S\\s%p',

tests/strptime_symmetry.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <string>
2+
#include <time.h>
3+
#include <cstring>
4+
#include <cstdio>
5+
6+
bool parseAndCompare(const std::string& time, std::string expectedTime) {
7+
char actualTimeBuffer[256];
8+
9+
struct tm tm;
10+
memset(&tm, 0, sizeof(struct tm));
11+
static const char* format = "%FT%T%z";
12+
strptime(time.c_str(), format, &tm);
13+
14+
sprintf(actualTimeBuffer,
15+
"%04d-%02d-%02dT%02d:%02d:%02d",
16+
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
17+
tm.tm_hour, tm.tm_min, tm.tm_sec);
18+
19+
std::string actualTime = actualTimeBuffer;
20+
printf("%s: %s == %s, %s\n", time.c_str(), actualTime.c_str(), expectedTime.c_str(),
21+
actualTime == expectedTime ? "true" : "false");
22+
23+
return actualTime == expectedTime;
24+
}
25+
26+
int main(int argc, char** argv) {
27+
bool testPassed =
28+
parseAndCompare("2018-03-27T19:33:09+0000", "2018-03-27T19:33:09") &&
29+
parseAndCompare("2018-03-27T19:33:09-0735", "2018-03-27T19:33:09") &&
30+
parseAndCompare("2018-03-27T19:33:09+1043", "2018-03-27T19:33:09") &&
31+
parseAndCompare("1900-01-01T00:00:00+0000", "1900-01-01T00:00:00") &&
32+
parseAndCompare("2018-12-31T23:59:59+0000", "2018-12-31T23:59:59");
33+
34+
if (testPassed) {
35+
printf("TEST PASSED\n");
36+
} else {
37+
printf("TEST FAILED\n");
38+
}
39+
40+
return 0;
41+
}

tests/test_other.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4479,6 +4479,10 @@ def test_strftime_zZ(self):
44794479
Popen([PYTHON, EMCC, 'src.cpp']).communicate()
44804480
self.assertContained('ok!', run_js('a.out.js'))
44814481

4482+
def test_strptime_symmetry(self):
4483+
Building.emcc(path_from_root('tests','strptime_symmetry.cpp'), output_filename='a.out.js')
4484+
self.assertContained('TEST PASSED', run_js('a.out.js'))
4485+
44824486
def test_truncate_from_0(self):
44834487
open('src.cpp', 'w').write(r'''
44844488
#include <cerrno>

0 commit comments

Comments
 (0)