mirror of
https://github.com/HowardHinnant/date.git
synced 2025-08-06 14:14:27 +02:00
Updated Boost datetime Examples Translated (markdown)
@@ -49,23 +49,27 @@ look if ported to this library.
|
|||||||
in >> y >> dash >> m >> dash >> day;
|
in >> y >> dash >> m >> dash >> day;
|
||||||
auto d = year(y)/m/day;
|
auto d = year(y)/m/day;
|
||||||
assert(d.ok());
|
assert(d.ok());
|
||||||
cout << d.year() << '-' << d.month() << '-' << d.day() << '\n'; // 2001-Oct-09
|
cout << d.year() << '-' << d.month() << '-' << d.day() << '\n';
|
||||||
|
|
||||||
// Read ISO Standard(CCYYMMDD) and output ISO Extended
|
// Read ISO Standard(CCYYMMDD) and output ISO Extended
|
||||||
string ud("20011009"); // 2001-Oct-09
|
string ud("20011009"); // 2001-Oct-09
|
||||||
auto d1 = year(stoi(ud.substr(0, 4)))/stoi(ud.substr(4, 2))/stoi(ud.substr(6, 2));
|
auto d1 = year(stoi(ud.substr(0, 4)))/stoi(ud.substr(4, 2))
|
||||||
|
/stoi(ud.substr(6, 2));
|
||||||
assert(d1.ok());
|
assert(d1.ok());
|
||||||
cout << d1 << '\n'; // 2001-10-09
|
cout << d1 << '\n'; // 2001-10-09
|
||||||
|
|
||||||
// Output the parts of the date - Tuesday October 9, 2001
|
// Output the parts of the date - Tuesday October 9, 2001
|
||||||
auto wd = weekday{d1};
|
auto wd = weekday{d1};
|
||||||
cout << wd << ' ' << d1.month() << ' ' << static_cast<unsigned>(d1.day())
|
cout << wd << ' ' << d1.month() << ' '
|
||||||
|
<< static_cast<unsigned>(d1.day())
|
||||||
<< ", " << d1.year() << '\n'; // Tue Oct 9, 2001
|
<< ", " << d1.year() << '\n'; // Tue Oct 9, 2001
|
||||||
|
|
||||||
// Let's send in month 25 by accident and create an exception
|
// Let's send in month 25 by accident and create an exception
|
||||||
string bad_date("20012509"); // 2001-??-09
|
string bad_date("20012509"); // 2001-??-09
|
||||||
cout << "An expected exception is next:\n";
|
cout << "An expected exception is next:\n";
|
||||||
auto d2 = year(stoi(bad_date.substr(0,4)))/stoi(bad_date.substr(4,2))/stoi(bad_date.substr(6,2));
|
auto d2 = year(stoi(bad_date.substr(0, 4)))
|
||||||
|
/stoi(bad_date.substr(4, 2))
|
||||||
|
/stoi(bad_date.substr(6, 2));
|
||||||
if (!d2.ok())
|
if (!d2.ok())
|
||||||
{
|
{
|
||||||
ostringstream msg;
|
ostringstream msg;
|
||||||
@@ -76,7 +80,7 @@ look if ported to this library.
|
|||||||
}
|
}
|
||||||
catch (const exception& e)
|
catch (const exception& e)
|
||||||
{
|
{
|
||||||
cout << "Exception: " << e.what() << '\n'; // Exception: Invalid date: 2001-25-09
|
cout << "Exception: " << e.what() << '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user