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