Coder Social home page Coder Social logo

Comments (7)

HowardHinnant avatar HowardHinnant commented on August 19, 2024

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.

ecorm avatar ecorm commented on August 19, 2024

Oh cool, I had not seen the new wiki page you referred to. Thanks for providing those examples!

from date.

ecorm avatar ecorm commented on August 19, 2024

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.

HowardHinnant avatar HowardHinnant commented on August 19, 2024

Feedback and/or porting patches welcome:

http://howardhinnant.github.io/tz.html#Formatting
e8f95dd

from date.

galik avatar galik commented on August 19, 2024

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.

HowardHinnant avatar HowardHinnant commented on August 19, 2024

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:

  1. We can handle more than the two timezones: UTC and local.
  2. 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.

HowardHinnant avatar HowardHinnant commented on August 19, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.