mirror of
https://github.com/HowardHinnant/date.git
synced 2025-08-05 21:54:27 +02:00
Updated Boost datetime Examples Translated (markdown)
@@ -27,49 +27,49 @@ look if ported to this library.
|
||||
|
||||
<a name="Dates_as_Strings"></a>
|
||||
### Dates as String
|
||||
```c++
|
||||
#include "date.h"
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <exception>
|
||||
|
||||
#include "date.h"
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <exception>
|
||||
|
||||
int
|
||||
main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
using namespace date;
|
||||
using namespace std;
|
||||
try
|
||||
{
|
||||
using namespace date;
|
||||
using namespace std;
|
||||
try
|
||||
{
|
||||
// The following date is in ISO 8601 extended format (CCYY-MM-DD)
|
||||
istringstream in{"2001-10-9"}; // 2001-October-09
|
||||
year_month_day d;
|
||||
in >> parse("%F", d);
|
||||
assert(d.ok());
|
||||
cout << format("%Y-%b-%d\n", d);
|
||||
// The following date is in ISO 8601 extended format (CCYY-MM-DD)
|
||||
istringstream in{"2001-10-9"}; // 2001-October-09
|
||||
year_month_day d;
|
||||
in >> parse("%F", d);
|
||||
assert(d.ok());
|
||||
cout << format("%Y-%b-%d\n", d);
|
||||
|
||||
// Read ISO Standard(CCYYMMDD) and output ISO Extended
|
||||
in.str("20011009"); // 2001-Oct-09
|
||||
in >> parse("%Y%m%d", d);
|
||||
assert(d.ok());
|
||||
cout << d << '\n'; // 2001-10-09
|
||||
// Read ISO Standard(CCYYMMDD) and output ISO Extended
|
||||
in.str("20011009"); // 2001-Oct-09
|
||||
in >> parse("%Y%m%d", d);
|
||||
assert(d.ok());
|
||||
cout << d << '\n'; // 2001-10-09
|
||||
|
||||
// Output the parts of the date - Tuesday October 9, 2001
|
||||
cout << format("%A %B %e, %Y\n", d);
|
||||
// Output the parts of the date - Tuesday October 9, 2001
|
||||
cout << format("%A %B %e, %Y\n", d);
|
||||
|
||||
// Let's send in month 25 by accident and create an exception
|
||||
in.str("20012509"); // 2001-??-09
|
||||
in.exceptions(std::ios::failbit);
|
||||
cout << "An expected exception is next:\n";
|
||||
in >> parse("%Y%m%d", d);
|
||||
cout << "oh oh, you shouldn't reach this line:\n";
|
||||
}
|
||||
catch (const exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
// Let's send in month 25 by accident and create an exception
|
||||
in.str("20012509"); // 2001-??-09
|
||||
in.exceptions(std::ios::failbit);
|
||||
cout << "An expected exception is next:\n";
|
||||
in >> parse("%Y%m%d", d);
|
||||
cout << "oh oh, you shouldn't reach this line:\n";
|
||||
}
|
||||
|
||||
catch (const exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
}
|
||||
```
|
||||
<a name="Days_Alive"></a>
|
||||
### Days Alive
|
||||
|
||||
|
Reference in New Issue
Block a user