diff --git a/CMakeLists.txt b/CMakeLists.txt index 48eae52..6b10394 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/LICENSE.md b/LICENSE.md index 0d2b956..eaa8034 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -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 diff --git a/include/peelo/chrono/date.hpp b/include/peelo/chrono/date.hpp index aad44ff..8cfb256 100644 --- a/include/peelo/chrono/date.hpp +++ b/include/peelo/chrono/date.hpp @@ -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 @@ -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 #include #include #if !defined(BUFSIZ) @@ -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. @@ -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) { @@ -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. @@ -765,5 +770,3 @@ namespace peelo::chrono return date.format("%d %b %Y"); } } - -#endif /* !PEELO_CHRONO_DATE_HPP_GUARD */ diff --git a/include/peelo/chrono/datetime.hpp b/include/peelo/chrono/datetime.hpp index 1d948aa..18c96cf 100644 --- a/include/peelo/chrono/datetime.hpp +++ b/include/peelo/chrono/datetime.hpp @@ -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 @@ -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 #include @@ -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. @@ -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) { @@ -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. @@ -597,5 +602,3 @@ namespace peelo::chrono return datetime.format(datetime::format_rfc2822); } } - -#endif /* !PEELO_CHRONO_DATETIME_HPP_GUARD */ diff --git a/include/peelo/chrono/duration.hpp b/include/peelo/chrono/duration.hpp index 0f31509..34503fd 100644 --- a/include/peelo/chrono/duration.hpp +++ b/include/peelo/chrono/duration.hpp @@ -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 @@ -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 @@ -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. @@ -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. @@ -408,5 +411,3 @@ namespace peelo::chrono value_type m_seconds; }; } - -#endif /* !PEELO_CHRONO_DURATION_HPP_GUARD */ diff --git a/include/peelo/chrono/month.hpp b/include/peelo/chrono/month.hpp index 1d61c0c..e358773 100644 --- a/include/peelo/chrono/month.hpp +++ b/include/peelo/chrono/month.hpp @@ -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 @@ -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 @@ -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) { @@ -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"; + } +} diff --git a/include/peelo/chrono/time.hpp b/include/peelo/chrono/time.hpp index 3715607..ef00ffd 100644 --- a/include/peelo/chrono/time.hpp +++ b/include/peelo/chrono/time.hpp @@ -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 @@ -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 #include #include #include -#if defined(_WIN32) -# define WIN32_LEAN_AND_MEAN -# include -#endif + #if !defined(BUFSIZ) # define BUFSIZ 1024 #endif @@ -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. @@ -81,29 +80,16 @@ namespace peelo::chrono */ static time now() { - time result; - -#if defined(_WIN32) - SYSTEMTIME lt; - - ::GetLocalTime(<); - 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); } /** @@ -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. @@ -512,5 +500,3 @@ namespace peelo::chrono return time.format("%T"); } } - -#endif /* !PEELO_CHRONO_TIME_HPP_GUARD */ diff --git a/include/peelo/chrono/weekday.hpp b/include/peelo/chrono/weekday.hpp index ad2a5aa..7d9e7d3 100644 --- a/include/peelo/chrono/weekday.hpp +++ b/include/peelo/chrono/weekday.hpp @@ -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 @@ -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_WEEKDAY_HPP_GUARD -#define PEELO_CHRONO_WEEKDAY_HPP_GUARD +#pragma once #include @@ -176,7 +175,7 @@ namespace peelo::chrono } /** - * Returns full name of the weekday (in English) to the stream. + * Returns full name of the weekday (in English) as string. */ inline std::string to_string(const weekday& day) { @@ -206,6 +205,36 @@ namespace peelo::chrono return "Unknown"; } -} -#endif /* !PEELO_CHRONO_WEEKDAY_HPP_GUARD */ + /** + * Returns full name of the weekday (in English) as Unicode string. + */ + inline std::u32string to_u32string(const weekday& day) + { + switch (day) + { + case weekday::sun: + return U"Sunday"; + + case weekday::mon: + return U"Monday"; + + case weekday::tue: + return U"Tuesday"; + + case weekday::wed: + return U"Wednesday"; + + case weekday::thu: + return U"Thursday"; + + case weekday::fri: + return U"Friday"; + + case weekday::sat: + return U"Saturday"; + } + + return U"Unknown"; + } +} diff --git a/test/test_month.cpp b/test/test_month.cpp index d097172..3a906ee 100644 --- a/test/test_month.cpp +++ b/test/test_month.cpp @@ -15,5 +15,8 @@ int main() assert(chrono::to_string(chrono::month::jan) == "January"); assert(chrono::to_string(chrono::month::oct) == "October"); + assert(chrono::to_u32string(chrono::month::jan) == U"January"); + assert(chrono::to_u32string(chrono::month::oct) == U"October"); + return 0; } diff --git a/test/test_weekday.cpp b/test/test_weekday.cpp index 1b6d7e6..0a30600 100644 --- a/test/test_weekday.cpp +++ b/test/test_weekday.cpp @@ -16,5 +16,8 @@ int main() assert(chrono::to_string(chrono::weekday::mon) == "Monday"); assert(chrono::to_string(chrono::weekday::fri) == "Friday"); + assert(chrono::to_u32string(chrono::weekday::mon) == U"Monday"); + assert(chrono::to_u32string(chrono::weekday::fri) == U"Friday"); + return 0; }