Comments (7)
I've been giving this issue some though lately. On the https://github.com/HowardHinnant/date/wiki/Boost-datetime-Examples-Translated wiki page, for some of the examples, I showed that the parsing and formatting could match whatever boost does, with just a small amount of code/knowledge of C++ streams.
This is leading me to believe that a parsing/formatting date lib could (and should) be layered non-intrusively on top of date.h rather than be part of it (just like tz.h). Indeed, there might even be multiple designs and implementations of such parsing/formatting date utilities. By keeping it separate I think we keep the lower-level date API simple, and demonstrate that you can build anything you want on top of it.
from date.
Oh cool, I had not seen the new wiki page you referred to. Thanks for providing those examples!
from date.
If the intent is for date
to eventually become part of the C++ standard, then perhaps it's worth considering what formatting/parsing facilities are provided by competing languages. C#/.NET has DateTime.ToString(IFormatProvider)
and DateTime.Parse(String, IFormatProvider)
, while Java has java.time.format.DateTimeFormatter
.
The "standard" parsing and formatting facilities could, like you suggest, be layered on top of date.h
and be provided in a separate header, say, dateio.h
.
from date.
Feedback and/or porting patches welcome:
http://howardhinnant.github.io/tz.html#Formatting
e8f95dd
from date.
I suppose std::get_time and std::put_time could be a model to piggy-back off.
class put_date_t
{
day_point dp;
const char* fmt;
public:
put_date_t(day_point dp, const char* fmt)
: dp(dp), fmt(fmt) {}
friend
std::ostream& operator<<(std::ostream& os, const put_date_t& pd)
{
auto timer = day_point::clock::to_time_t(pd.dp);
std::tm tmb = *std::localtime(&timer); // not thread-safe
return os << std::put_time(&tmb, pd.fmt);
}
};
inline
put_date_t put_date(day_point dp, const char* fmt)
{
return {dp, fmt};
}
int main()
{
std::cout << put_date(2016_y/jul/mon[1], "%Y-%m-%d") << '\n';
}
from date.
Yes, pretty much. To tell you the truth I completely forgot about the std::put_time
manipulator. But this is all encompassed by these very few lines of code:
https://github.com/HowardHinnant/date/blob/master/tz.h#L764-L773
The "special sauce" added by date::format
is a little extra love for %S
, %T
, %z
and %Z
. %S
, %T
are enhanced to handle fractional seconds. And %z
and %Z
are intercepted to take data from the this timezone library as opposed to letting the OS handle it. This has two consequences:
- We can handle more than the two timezones: UTC and local.
- If the IANA timezone database gets updated, this code will learn about it as soon as you update your local copy (which happens automatically on the experimental branch auto_download). This is opposed to waiting for your vendor/OS to update the tzdata files. This timeliness assures that this output will always be consistent with the rest of the tz.h library.
from date.
Closing. Formatting is now fully handled here:
http://howardhinnant.github.io/date/tz.html#format
And parsing is now handled here:
http://howardhinnant.github.io/date/tz.html#parse
from date.
Related Issues (20)
- Undefined symbol HOT 1
- date_test_pass_parse_test fails with gcc (Debian 10.2.1-6) 10.2.1 20210110 HOT 1
- Date Parse: one pattern for ISO8601 DATE or DATE TIME HOT 6
- How can I convert between std::chrono::time_point and string (2023-12-06 00:46:45.801479+08)? HOT 4
- OSSFuzz Integration HOT 9
- Incorrect value of date::sys_info::save HOT 1
- Cannot override CMake options when fetching the library via FetchContent_Declare()
- .zip files have "text" git attribute HOT 1
- Possibly big performance improvement for `time_zone::to_sys()` HOT 10
- Failed to install tzdb HOT 4
- Transform date::local_seconds and timezone offset to struct tm HOT 1
- Missing tz.h header file when selecting compiler for ARM architecture HOT 1
- Opposite inner 'if' condition leads to a dead code block HOT 1
- Use timezone name like "-08:00" for `locate_zone()` HOT 2
- date::parse() doesn't have a formatting character for ISO 8601 strings without 'T' separator HOT 2
- How to parse datetime with milliseconds and timezone? HOT 1
- Build failure due to ambiguity with >= libc++ 17
- Could a new official version be released? HOT 3
- Unable to get Timezone database version HOT 2
- date::parse() how to handle different date-time formats. HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from date.