A lightweight and comprehensive C++ date manipulation library with custom date arithmetic, validation, formatting, and comparison utilities.
- Date Storage & Retrieval: Store dates with day, month, and year components
- Date Arithmetic: Increment dates by days, months, years, or custom intervals
- Date Comparison: Compare dates for equality with overloaded operators
- Flexible Formatting: Get formatted date strings and print dates in readable format
- Multiple Constructors: Create dates with various initialization options
- Simple Calendar System: Uses a simplified 30-day month, 12-month year system
#include "DateUtility.h"
#include <iostream>
int main() {
// Create a date
DateUtility date(15, 6, 2023);
std::cout << "Date: " << date << std::endl; // Output: 15-June-2023
// Increment operations
date.incrementDay();
std::cout << "Next day: " << date << std::endl; // Output: 16-June-2023
date.incrementByDays(45);
std::cout << "45 days later: " << date << std::endl;
return 0;
}
- C++11 compatible compiler (GCC, Clang, MSVC)
- Make (for building with Makefile)
- Clone or download the repository
- Navigate to the project directory
- Build using Make:
# Build library and examples
make
# Build only the library
make libdateutility.a
# Build with debug information
make debug
# Clean build files
make clean
# Run basic usage example
make run-basic
# Run interactive demo
make run-demo
DateUtility(); // Default: 1/1/1
DateUtility(int day); // day/1/1
DateUtility(int day, int month); // day/month/1
DateUtility(int day, int month, int year); // day/month/year
void setDate(int day, int month, int year);
int getDate() const; // Returns DDMMYYYY format
int getDay() const;
int getMonth() const;
int getYear() const;
std::string getFormattedDate() const; // Returns "DD-MonthName-YYYY"
int incrementDay(); // Add 1 day
int incrementMonth(); // Add 1 month
int incrementYear(); // Add 1 year
int incrementByDays(int days); // Add specified days
bool isEqual(const DateUtility& other) const;
bool operator==(const DateUtility& other) const;
bool operator!=(const DateUtility& other) const;
void printDate(bool showLabel = true) const;
friend std::ostream& operator<<(std::ostream& os, const DateUtility& date);
DateUtility date1; // 1-January-1
DateUtility date2(25, 12, 2023); // 25-December-2023
date2.incrementDay(); // 26-December-2023
date2.incrementByDays(10); // 6-January-2024 (rolls over)
DateUtility date1(15, 6, 2023);
DateUtility date2(15, 6, 2023);
DateUtility date3(16, 6, 2023);
if (date1 == date2) {
std::cout << "Dates are equal!" << std::endl;
}
if (date1 != date3) {
std::cout << "Dates are different!" << std::endl;
}
DateUtility date(1, 1, 2023);
date.incrementByDays(400); // Handles month/year rollover
std::cout << date << std::endl; // Automatically calculated result
This library uses a simplified calendar system:
- All months have 30 days
- All years have 12 months
- Date rollover is automatic (day 31 becomes day 1 of next month)
- Year rollover is automatic (month 13 becomes month 1 of next year)
This design makes date arithmetic predictable and consistent, though it differs from the Gregorian calendar.
cpp-date-utility-library/
├── include/
│ └── DateUtility.h # Header file with class declaration
├── src/
│ └── DateUtility.cpp # Implementation file
├── examples/
│ ├── basic_usage.cpp # Basic usage examples
│ └── interactive_demo.cpp # Interactive demonstration
├── build/ # Build output directory (created by make)
├── Makefile # Build configuration
├── .gitignore # Git ignore rules
├── LICENSE # MIT License
└── README.md # This file
- API Reference - Complete API documentation with method signatures and examples
- Examples - Comprehensive usage examples and practical applications
- Contributing - Guidelines for contributing to the project
- Changelog - Version history and release notes
Contributions are welcome! Please read our Contributing Guidelines for details on:
- Code style and standards
- Development setup
- Testing requirements
- Pull request process
Feel free to submit issues, feature requests, or pull requests.
This project is licensed under the MIT License - see the LICENSE file for details.
Syed Muhammad Daniyal Zaidi
- Inspired by the need for simple, predictable date arithmetic in C++
- Designed for educational purposes and lightweight applications