-
Notifications
You must be signed in to change notification settings - Fork 2
C++ library for parsing datetime formatted string
License
lyokato/cpp-datetimelite
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
=======================================================================
DESCRIPTION
=======================================================================
This library allows you to parse datetime formatted string, easily.
=======================================================================
SYNOPSIS
=======================================================================
Two version is here
----------------------------------------------------------------------
// 1. datetimelite.h
//
// you can parse many kinds of datetime-formatted-string with
// 'time_from_string' function which returns struct tm.
//
// And if passed string has format that is not supported, it throws
// std::invalid_argument
//
// This has no dependency on boost
#include <datetimelite.h>
using namespace datetimelite;
struct std::tm ts = time_from_string("Wed, 09 Feb 1994 22:23:32 GMT"); // HTTP format
struct std::tm ts = time_from_string("Tuesday, 08-Feb-94 14:15:29 GMT"); // old rfc850 HTTP format
struct std::tm ts = time_from_string("Tuesday, 08-Feb-1994 14:15:29 GMT"); // broken rfc850
struct std::tm ts = time_from_string("03/Feb/1994:17:03:55 -0700"); // common log file format
struct std::tm ts = time_from_string("09 Feb 1994 22:23:32 GMT"); // HTTP format no weekday
struct std::tm ts = time_from_string("08-Feb-94 14:15:29 GMT"); // rfc850 no weekday
struct std::tm ts = time_from_string("08-Feb-1994 14:15:29 GMT"); // broken rfc850 no weekday
struct std::tm ts = time_from_string("1994-02-03 14:15:29 -0100"); // ISO8601
struct std::tm ts = time_from_string("1994-02-03 14:15:29"); // zone is optional
struct std::tm ts = time_from_string("1994-02-03"); // only date
struct std::tm ts = time_from_string("1994-02-03T14:15:29"); // use T separator
struct std::tm ts = time_from_string("19940203T141529Z"); // ISO8601 compact format
struct std::tm ts = time_from_string("19940203"); // only date
struct std::tm ts = time_from_string("08-Feb-94"); // old rfc850 no weekday, no time
struct std::tm ts = time_from_string("08-Feb-1994"); // broken rfc850 no weekday, no time
struct std::tm ts = time_from_string("09 Feb 1994"); // proposed new http format no weekday no time
struct std::tm ts = time_from_string("03/Feb/1994"); // common log file format no weekday, no time
try {
struct std::tm ts = time_from_string("invalid format");
} catch (std::exception& e) {
std::cout << "invalid format: " << e.what() << std::endl;
}
// 2. datetimelite2.h
//
// function name 'time_from_string' is same as above,
// but the behavior is different
//
// that returns boost::optional<boost::posix_time::ptime> object.
// So, if passed string has invalid-format, it doesn't throw, but
// returns boost::none.
//
// Of cource, this header depends on boost_date_time library
#include <datetimelite2.h>
#include <boost/optional.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
boost::optional<boost::posix_time::ptime> t = datetimelite2::time_from_string("1994-02-03T14:15:29");
if (t) {
std::cout << boost::posix_time::to_simple_string(*p) << std::endl;
}
=======================================================================
TODO
=======================================================================
More tests
ctime(3) format support
ANSI asctime format support
Unix 'ls -l' format support
Windows 'dir' format support
=======================================================================
INSTALL
=======================================================================
This is header-only library.
So, copying datetimelite.h or datetimelite2.h into your project directory is the easiest way.
or
1. cd build
2. cmake .. -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release
parameters
- BUILD_SHARED_LIBS (ON|OFF)
- CMAKE_BUILD_TYPE (Debug|Release)
- CMAKE_INSTALL_PREFIX (/usr/local)
3. make
4. make test
5. make install
About
C++ library for parsing datetime formatted string
Resources
License
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published