Appendix J. Source Code for the file scpp_date.hpp and scpp_date.cpp

File scpp_date.hpp

#ifndef __SCPP_DATE_HPP_INCLUDED__ #define __SCPP_DATE_HPP_INCLUDED__ #include <iostream> #include <string> #include "scpp_assert.hpp" #include "scpp_types.hpp" /* Date class. Features: All date arithmetic operators and comparisons are provided. Date arithmetic is implemented as an integer arithmetic. No Y2K problems -- all years must be >= 1900. Default output format is American (MM/DD/YYYY). In debug one can see the date in debugger as yyyymmdd -- just point your debugger to a yyyymmdd_ data member. No implicit type conversions are allowed. */ namespace scpp { class Date { public: // Creates an empty (invalid in terms of IsValid()) date. Date(); // Input format: "mm/dd/yyyy". explicit Date(const char* str_date); // Same as above. explicit Date(const std::string& str_date); // Date from integer in the YYYYMMDD format, e.g. Dec. 26, 2011 is 20111226. explicit Date(unsigned yyyymmdd); // Year must be 4-digit, // month is 1-based, i.e. 1 .. 12, // day is 1 .. MonthLength() <= 31 Date(unsigned year, unsigned month, unsigned day); // Returns true if the date is not empty, // as is the case when it is created by the default constructor. // Most operations on invalid date are not allowed // (will call error handler). bool IsValid() const { return date_!=0; } // Returns date in YYYYMMDD format, e.g. Dec. 26, 2011 is 20111226. unsigned AsYYYYMMDD() const; // 4-digit year. unsigned Year() const; enum ...

Get Safe C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.