Strptime() Equivalent on Windows

strptime() equivalent on Windows?

An open-source version (BSD license) of strptime() can be found here: http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/time/strptime.c?rev=HEAD

You'll need to add the following declaration to use it:

char *strptime(const char * __restrict, const char * __restrict, struct tm * __restrict);

How to use strptime(fromdate.c_str(), %Y-%m-%d %H %M %S, &tm); on windows

If you can't get the BSD version of strptime working, you could try Howard Hinnant's date.h

Or you could create a simple conversion function for this particular format yourself. Example:

#include <cstdio>
#include <ctime>
#include <cerrno>
#include <iostream>
#include <stdexcept>
#include <string>
#include <string_view>

std::tm to_tm(const std::string_view& date) {
std::tm t{};

if(std::sscanf(date.data(), "%d-%d-%d %d %d %d",
&t.tm_year,
&t.tm_mon,
&t.tm_mday,
&t.tm_hour,
&t.tm_min,
&t.tm_sec
) != 6)
throw std::runtime_error("Invalid date format: " + std::string(date));

t.tm_year -= 1900;
--t.tm_mon;
t.tm_isdst = -1; // guess if DST should be in effect when calling mktime
errno = 0;
std::mktime(&t);

return t;
}

int main() {
std::string fromdate = "2021-03-25 08 23 56";

std::tm x = to_tm(fromdate);

std::cout << std::asctime(&x) << '\n'; // Thu Mar 25 08:23:56 2021
}

Or, a non-throwing version that returns std::time_t directly:

#include <system_error> // added to be able to set an error code

std::time_t to_time_t(const std::string_view& date) {
std::tm t{};

if(std::sscanf(date.data(), "%d-%d-%d %d %d %d",
&t.tm_year,
&t.tm_mon,
&t.tm_mday,
&t.tm_hour,
&t.tm_min,
&t.tm_sec
) != 6) {
// EOVERFLOW (std::errc::value_too_large)
// This is what Posix versions of mktime() uses to signal that -1 does
// not mean one second before epoch, but that there was an error.
errno = static_cast<int>(std::errc::value_too_large);
return -1;
}
t.tm_year -= 1900;
--t.tm_mon;
t.tm_isdst = -1; // guess if DST should be in effect when calling mktime

errno = 0;
return std::mktime(&t);
}

Python datetime.strptime with Korean locale on Windows

Apply locale.setlocale(locale.LC_ALL, 'kor') instead of locale.setlocale(locale.LC_TIME, 'kor') as follows:

d:\bat> python -q
>>>
>>> import locale
>>> from datetime import datetime
>>>
>>> ### generate a date string with datetime.strftime
...
>>> locale.setlocale(locale.LC_ALL, 'kor') ### crucial point ###
'Korean_Korea.949'
>>> locale.getlocale(locale.LC_TIME)
('Korean_Korea', '949')
>>> print(datetime.now().strftime('%A')) # Prints 월요일 (right!)
월요일
>>>
>>> ### parsing korean date string
...
>>> date_string = '월요일, 2019년 08월 05일 09:33:39'
>>> fromat = '%A, %Y년 %m월 %d일 %H:%M:%S'
>>>
>>> time = datetime.strptime(date_string, fromat)
>>> print(time)
2019-08-05 09:33:39
>>>

FYI, here are some other test cases (Python 3.5.1 64 bit (AMD64) on win32):

import locale
from datetime import datetime

locale.getdefaultlocale() # Echoes ('cs_CZ', 'cp65001')
locale.getlocale(locale.LC_TIME) # Echoes (None, None)
print(datetime.now().strftime('%A')) # Prints Monday (wrong?)

# user’s default setting for LC_TIME category
locale.setlocale(locale.LC_TIME, '') # Echoes 'Czech_Czechia.utf8'
locale.getlocale(locale.LC_TIME) # Echoes ('Czech_Czechia', 'utf8')
print(datetime.now().strftime('%A')) # Prints pondÄí (wrong!)

# user’s default setting for all categories
locale.setlocale(locale.LC_ALL, '') # Echoes 'Czech_Czechia.utf8'
locale.getlocale(locale.LC_TIME) # Echoes ('Czech_Czechia', 'utf8')
print(datetime.now().strftime('%A')) # Prints pondělí (right!)

################################################

locale.setlocale(locale.LC_TIME, 'kor')
locale.getlocale(locale.LC_TIME)
print(datetime.now().strftime('%A')) # Prints ¿ù¿äÀÏ (wrong!)

################################################


Related Topics



Leave a reply



Submit