Skip to content

sdannyzaidi/cpp-date-utility-library

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DateUtility Library

A lightweight and comprehensive C++ date manipulation library with custom date arithmetic, validation, formatting, and comparison utilities.

Features

  • 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

Quick Start

Basic Usage

#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;
}

Installation

Prerequisites

  • C++11 compatible compiler (GCC, Clang, MSVC)
  • Make (for building with Makefile)

Building the Library

  1. Clone or download the repository
  2. Navigate to the project directory
  3. 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

Running Examples

# Run basic usage example
make run-basic

# Run interactive demo
make run-demo

API Reference

Constructors

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

Core Methods

Date Setting and Getting

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"

Date Arithmetic

int incrementDay();                     // Add 1 day
int incrementMonth();                   // Add 1 month
int incrementYear();                    // Add 1 year
int incrementByDays(int days);          // Add specified days

Date Comparison

bool isEqual(const DateUtility& other) const;
bool operator==(const DateUtility& other) const;
bool operator!=(const DateUtility& other) const;

Output

void printDate(bool showLabel = true) const;
friend std::ostream& operator<<(std::ostream& os, const DateUtility& date);

Examples

Date Creation and Manipulation

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)

Date Comparison

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;
}

Working with Large Date Increments

DateUtility date(1, 1, 2023);
date.incrementByDays(400);              // Handles month/year rollover
std::cout << date << std::endl;         // Automatically calculated result

Calendar System

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.

Project Structure

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

Documentation

  • 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

Contributing

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.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Syed Muhammad Daniyal Zaidi

Acknowledgments

  • Inspired by the need for simple, predictable date arithmetic in C++
  • Designed for educational purposes and lightweight applications

About

Lightweight C++ date utility library with arithmetic, formatting, and comparison features

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published