Skip to content

Commit

Permalink
Modernize things a bit
Browse files Browse the repository at this point in the history
Use `std::chrono` for determining current date and time because I have
experienced weird issues with `std::time()` occasionally, add move
constructors/operators and Unicode versions of `to_string` functions.
  • Loading branch information
RauliL committed Jun 6, 2024
1 parent f549fa1 commit 350da56
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 90 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.12)

PROJECT(
PeeloChrono
VERSION 0.2.0
VERSION 0.3.0
DESCRIPTION "Header only minimal C++ Gregorian calendar implementation."
HOMEPAGE_URL "https://github.com/peelonet/peelo-chrono"
LANGUAGES CXX
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2016, peelo.net
Copyright (c) 2016-2024, peelo.net
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
33 changes: 18 additions & 15 deletions include/peelo/chrono/date.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2019, peelo.net
* Copyright (c) 2016-2024, peelo.net
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand All @@ -23,9 +23,9 @@
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef PEELO_CHRONO_DATE_HPP_GUARD
#define PEELO_CHRONO_DATE_HPP_GUARD
#pragma once

#include <chrono>
#include <ctime>
#include <stdexcept>
#if !defined(BUFSIZ)
Expand Down Expand Up @@ -71,10 +71,12 @@ namespace peelo::chrono
/**
* Copy constructor.
*/
date(const date& that)
: m_year(that.m_year)
, m_month(that.m_month)
, m_day(that.m_day) {}
date(const date&) = default;

/**
* Move constructor.
*/
date(date&&) = default;

/**
* Returns current date based on system clock.
Expand All @@ -84,8 +86,9 @@ namespace peelo::chrono
*/
static date today()
{
auto ts = std::time(nullptr);
auto tm = std::localtime(&ts);
const auto now = std::chrono::system_clock::now();
const auto ts = std::chrono::system_clock::to_time_t(now);
const auto tm = std::localtime(&ts);

if (!tm || tm->tm_mon < 0 || tm->tm_mon > 11)
{
Expand Down Expand Up @@ -413,10 +416,12 @@ namespace peelo::chrono
/**
* Assignment operator.
*/
inline date& operator=(const date& that)
{
return assign(that);
}
date& operator=(const date&) = default;

/**
* Move operator.
*/
date& operator=(date&&) = default;

/**
* Tests whether two dates are equal or not.
Expand Down Expand Up @@ -765,5 +770,3 @@ namespace peelo::chrono
return date.format("%d %b %Y");
}
}

#endif /* !PEELO_CHRONO_DATE_HPP_GUARD */
31 changes: 17 additions & 14 deletions include/peelo/chrono/datetime.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2019, peelo.net
* Copyright (c) 2016-2024, peelo.net
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand All @@ -23,8 +23,7 @@
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef PEELO_CHRONO_DATETIME_HPP_GUARD
#define PEELO_CHRONO_DATETIME_HPP_GUARD
#pragma once

#include <peelo/chrono/date.hpp>
#include <peelo/chrono/time.hpp>
Expand Down Expand Up @@ -68,9 +67,12 @@ namespace peelo::chrono
/**
* Copy constructor.
*/
datetime(const datetime& that)
: m_date(that.m_date)
, m_time(that.m_time) {}
datetime(const datetime&) = default;

/**
* Move constructor.
*/
datetime(datetime&&) = default;

/**
* Constructs datetime from given date and time.
Expand All @@ -87,8 +89,9 @@ namespace peelo::chrono
*/
static datetime now()
{
auto ts = std::time(nullptr);
auto tm = std::localtime(&ts);
const auto now = std::chrono::system_clock::now();
const auto ts = std::chrono::system_clock::to_time_t(now);
const auto tm = std::localtime(&ts);

if (!tm || tm->tm_mon < 0 || tm->tm_mon > 11)
{
Expand Down Expand Up @@ -331,10 +334,12 @@ namespace peelo::chrono
/**
* Assignment operator.
*/
inline datetime& operator=(const datetime& that)
{
return assign(that);
}
datetime& operator=(const datetime&) = default;

/**
* Move constructor.
*/
datetime& operator=(datetime&&) = default;

/**
* Tests whether two datetimes are equal.
Expand Down Expand Up @@ -597,5 +602,3 @@ namespace peelo::chrono
return datetime.format(datetime::format_rfc2822);
}
}

#endif /* !PEELO_CHRONO_DATETIME_HPP_GUARD */
27 changes: 14 additions & 13 deletions include/peelo/chrono/duration.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, peelo.net
* Copyright (c) 2019-2024, peelo.net
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand All @@ -23,8 +23,7 @@
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef PEELO_CHRONO_DURATION_HPP_GUARD
#define PEELO_CHRONO_DURATION_HPP_GUARD
#pragma once

#include <cstdint>

Expand Down Expand Up @@ -85,11 +84,13 @@ namespace peelo::chrono

/**
* Copy constructor.
*
* \param that Other duration value to construct copy of.
*/
duration(const duration& that)
: m_seconds(that.m_seconds) {}
duration(const duration&) = default;

/**
* Move constructor.
*/
duration(duration&&) = default;

/**
* Returns the number of days in the duration.
Expand Down Expand Up @@ -150,10 +151,12 @@ namespace peelo::chrono
/**
* Assignment operator.
*/
inline duration& operator=(const duration& that)
{
return assign(that);
}
duration& operator=(const duration&) = default;

/**
* Move operator.
*/
duration& operator=(duration&&) = default;

/**
* Assignment operator.
Expand Down Expand Up @@ -408,5 +411,3 @@ namespace peelo::chrono
value_type m_seconds;
};
}

#endif /* !PEELO_CHRONO_DURATION_HPP_GUARD */
56 changes: 50 additions & 6 deletions include/peelo/chrono/month.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2019, peelo.net
* Copyright (c) 2016-2024, peelo.net
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand All @@ -23,8 +23,7 @@
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef PEELO_CHRONO_MONTH_HPP_GUARD
#define PEELO_CHRONO_MONTH_HPP_GUARD
#pragma once

#include <string>

Expand Down Expand Up @@ -177,7 +176,7 @@ namespace peelo::chrono
}

/**
* Returns name of the month (in English) into the stream.
* Returns name of the month (in English) as string.
*/
inline std::string to_string(const enum month& month)
{
Expand Down Expand Up @@ -222,6 +221,51 @@ namespace peelo::chrono

return "Unknown";
}
}

#endif /* !PEELO_CHRONO_MONTH_HPP_GUARD */
/**
* Returns name of the month (in English) as Unicode string.
*/
inline std::u32string to_u32string(const enum month& month)
{
switch (month)
{
case month::jan:
return U"January";

case month::feb:
return U"February";

case month::mar:
return U"March";

case month::apr:
return U"April";

case month::may:
return U"May";

case month::jun:
return U"June";

case month::jul:
return U"July";

case month::aug:
return U"August";

case month::sep:
return U"September";

case month::oct:
return U"October";

case month::nov:
return U"November";

case month::dec:
return U"December";
}

return U"Unknown";
}
}
54 changes: 20 additions & 34 deletions include/peelo/chrono/time.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2019, peelo.net
* Copyright (c) 2016-2024, peelo.net
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand All @@ -23,16 +23,13 @@
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef PEELO_CHRONO_TIME_HPP_GUARD
#define PEELO_CHRONO_TIME_HPP_GUARD
#pragma once

#include <chrono>
#include <ctime>
#include <stdexcept>
#include <string>
#if defined(_WIN32)
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#endif

#if !defined(BUFSIZ)
# define BUFSIZ 1024
#endif
Expand Down Expand Up @@ -68,10 +65,12 @@ namespace peelo::chrono
/**
* Copy constructor.
*/
time(const time& that)
: m_hour(that.m_hour)
, m_minute(that.m_minute)
, m_second(that.m_second) {}
time(const time&) = default;

/**
* Move constructor.
*/
time(time&&) = default;

/**
* Returns current time based on system clock.
Expand All @@ -81,29 +80,16 @@ namespace peelo::chrono
*/
static time now()
{
time result;

#if defined(_WIN32)
SYSTEMTIME lt;

::GetLocalTime(&lt);
result.m_hour = lt.wHour;
result.m_minute = lt.wMinute;
result.m_second = lt.wSecond;
#else
auto ts = std::time(nullptr);
auto tm = std::localtime(&ts);
const auto now = std::chrono::system_clock::now();
const auto ts = std::chrono::system_clock::to_time_t(now);
const auto tm = std::localtime(&ts);

if (!tm)
{
throw std::runtime_error("localtime() failed");
}
result.m_hour = tm->tm_hour;
result.m_minute = tm->tm_min;
result.m_second = tm->tm_sec;
#endif

return result;
return time(tm->tm_hour, tm->tm_min, tm->tm_sec);
}

/**
Expand Down Expand Up @@ -197,10 +183,12 @@ namespace peelo::chrono
/**
* Assignment operator.
*/
inline time& operator=(const time& that)
{
return assign(that);
}
time& operator=(const time&) = default;

/**
* Move constructor.
*/
time& operator=(time&&) = default;

/**
* Tests whether two time values are equal.
Expand Down Expand Up @@ -512,5 +500,3 @@ namespace peelo::chrono
return time.format("%T");
}
}

#endif /* !PEELO_CHRONO_TIME_HPP_GUARD */
Loading

0 comments on commit 350da56

Please sign in to comment.