How do I parse an ISO 8601-formatted date and time?

isoparse function from python-dateutil The python-dateutil package has dateutil.parser.isoparse to parse not only RFC 3339 datetime strings like the one in the question, but also other ISO 8601 date and time strings that don’t comply with RFC 3339 (such as ones with no UTC offset, or ones that represent only a date). >>> import dateutil.parser … Read more

JSR-310 – parsing seconds fraction with variable length

This solves the problem: DateTimeFormatter formatter = new DateTimeFormatterBuilder() .appendPattern(“yyyy-MM-dd HH:mm:ss”) .appendFraction(ChronoField.MICRO_OF_SECOND, 0, 6, true) .toFormatter(); System.out.println(LocalDateTime.parse(“2015-05-07 13:20:22.276052”, formatter)); System.out.println(LocalDateTime.parse(“2015-05-07 13:20:22.276”, formatter)); System.out.println(LocalDateTime.parse(“2015-05-07 13:20:22”, formatter)); // output 2015-05-07T13:20:22.276052 2015-05-07T13:20:22.276 2015-05-07T13:20:22 The answer by JiriS is incorrect, as it uses appendValue whereas the correct way is to use DateTimeFormatterBuilder.appendFraction (which also handles the decimal point). The … Read more

Java 8 Date and Time: parse ISO 8601 string without colon in offset [duplicate]

If you want to parse all valid formats of offsets (Z, ±hh:mm, ±hhmm and ±hh), one alternative is to use a java.time.format.DateTimeFormatterBuilder with optional patterns (unfortunatelly, it seems that there’s no single pattern letter to match them all): DateTimeFormatter formatter = new DateTimeFormatterBuilder() // date/time .append(DateTimeFormatter.ISO_LOCAL_DATE_TIME) // offset (hh:mm – “+00:00” when it’s zero) .optionalStart().appendOffset(“+HH:MM”, … Read more

How to parse a date string into a c++11 std::chrono time_point or similar?

std::tm tm = {}; std::stringstream ss(“Jan 9 2014 12:35:34”); ss >> std::get_time(&tm, “%b %d %Y %H:%M:%S”); auto tp = std::chrono::system_clock::from_time_t(std::mktime(&tm)); GCC prior to version 5 doesn’t implement std::get_time. You should also be able to write: std::tm tm = {}; strptime(“Thu Jan 9 2014 12:35:34”, “%a %b %d %Y %H:%M:%S”, &tm); auto tp = std::chrono::system_clock::from_time_t(std::mktime(&tm));

Parse rfc3339 date strings in Python? [duplicate]

You can use dateutil.parser.parse (install with python -m pip install python-dateutil) to parse strings into datetime objects. dateutil.parser.parse will attempt to guess the format of your string, if you know the exact format in advance then you can use datetime.strptime which you supply a format string to (see Brent Washburne’s answer). from dateutil.parser import parse … Read more

How can I parse a time string containing milliseconds in it with python?

Python 2.6 added a new strftime/strptime macro %f. The docs are a bit misleading as they only mention microseconds, but %f actually parses any decimal fraction of seconds with up to 6 digits, meaning it also works for milliseconds or even centiseconds or deciseconds. time.strptime(’30/03/09 16:31:32.123′, ‘%d/%m/%y %H:%M:%S.%f’) However, time.struct_time doesn’t actually store milliseconds/microseconds. You’re … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)