From c648d347c0baa3f21f29c633102e2c885f74b09d Mon Sep 17 00:00:00 2001 From: Steve Grunwell <233836+stevegrunwell@users.noreply.github.com> Date: Thu, 22 Aug 2024 16:55:59 -0400 Subject: [PATCH 1/2] Skip defined() checks, unnecessary multiplication In version 1.x, the constants were defined in the global namespace so it was responsible to explicitly check that, for instance, `HOUR_IN_SECONDS` was not already defined before attempting to define it. Now that the constants are namespaced, this just adds unnecessary overhead: if someone else is defining constants in the `TimeConstants` namespace, there are bigger problems. Additionally, IDEs seem to trip over the `defined()` checks, not being sure that the constants exist. They do. Similarly, while it was fun writing the intial constants in a way that they build upon each other, there's really no reason that this file should have to confirm (for example) that 24 hours * 60 minutes/hour = 1440 minutes/hour. These values do not change (hence *constants*), so save a little bit of basic multiplication by hard-coding the values. --- src/Constants.php | 148 +++++++++++++++++++++++----------------------- 1 file changed, 74 insertions(+), 74 deletions(-) diff --git a/src/Constants.php b/src/Constants.php index e730198..11bc018 100644 --- a/src/Constants.php +++ b/src/Constants.php @@ -19,40 +19,40 @@ * @link https://codex.wordpress.org/Easier_Expression_of_Time_Constants */ -/* One second. */ -if (!defined(__NAMESPACE__ . '\\ONE_SECOND')) { - define(__NAMESPACE__ . '\\ONE_SECOND', 1); -} - -/* One minute = 60 seconds. */ -if (!defined(__NAMESPACE__ . '\\MINUTE_IN_SECONDS')) { - define(__NAMESPACE__ . '\\MINUTE_IN_SECONDS', 60); -} - -/* One hour = 60 minutes. */ -if (!defined(__NAMESPACE__ . '\\HOUR_IN_SECONDS')) { - define(__NAMESPACE__ . '\\HOUR_IN_SECONDS', 60 * MINUTE_IN_SECONDS); -} - -/* One day = 24 hours. */ -if (!defined(__NAMESPACE__ . '\\DAY_IN_SECONDS')) { - define(__NAMESPACE__ . '\\DAY_IN_SECONDS', 24 * HOUR_IN_SECONDS); -} - -/* One week = 7 days. */ -if (!defined(__NAMESPACE__ . '\\WEEK_IN_SECONDS')) { - define(__NAMESPACE__ . '\\WEEK_IN_SECONDS', 7 * DAY_IN_SECONDS); -} - -/* For common usage, assume one month = 30 days. */ -if (!defined(__NAMESPACE__ . '\\MONTH_IN_SECONDS')) { - define(__NAMESPACE__ . '\\MONTH_IN_SECONDS', 30 * DAY_IN_SECONDS); -} - -/* For common usage, assume one year = 365 days. */ -if (!defined(__NAMESPACE__ . '\\YEAR_IN_SECONDS')) { - define(__NAMESPACE__ . '\\YEAR_IN_SECONDS', 365 * DAY_IN_SECONDS); -} +/** + * One second. + */ +define(__NAMESPACE__ . '\\ONE_SECOND', 1); + +/** + * One minute is 60 seconds. + */ +define(__NAMESPACE__ . '\\MINUTE_IN_SECONDS', 60); + +/** + * One hour is 60 minutes. + */ +define(__NAMESPACE__ . '\\HOUR_IN_SECONDS', 3600); + +/** + * One day is 24 hours. + */ +define(__NAMESPACE__ . '\\DAY_IN_SECONDS', 86400); + +/** + * One week is 7 days. + */ +define(__NAMESPACE__ . '\\WEEK_IN_SECONDS', 604800); + +/** + * For general purposes, assume that one month is 30 days. + */ +define(__NAMESPACE__ . '\\MONTH_IN_SECONDS', 2592000); + +/** + * For general purposes, assume that one year is 365 days. + */ +define(__NAMESPACE__ . '\\YEAR_IN_SECONDS', 31536000); /** * Time based in minutes. @@ -61,35 +61,35 @@ * constants provide similar functionality. */ -/* One minute. */ -if (!defined(__NAMESPACE__ . '\\ONE_MINUTE')) { - define(__NAMESPACE__ . '\\ONE_MINUTE', 1); -} +/** + * One minute. + */ +define(__NAMESPACE__ . '\\ONE_MINUTE', 1); -/* One hour = 60 minutes. */ -if (!defined(__NAMESPACE__ . '\\HOUR_IN_MINUTES')) { - define(__NAMESPACE__ . '\\HOUR_IN_MINUTES', 60); -} +/** + * One hour is 60 minutes. + */ +define(__NAMESPACE__ . '\\HOUR_IN_MINUTES', 60); -/* One day = 24 hours. */ -if (!defined(__NAMESPACE__ . '\\DAY_IN_MINUTES')) { - define(__NAMESPACE__ . '\\DAY_IN_MINUTES', 24 * HOUR_IN_MINUTES); -} +/** + * One day is 24 hours. + */ +define(__NAMESPACE__ . '\\DAY_IN_MINUTES', 1440); -/* One week = 7 days. */ -if (!defined(__NAMESPACE__ . '\\WEEK_IN_MINUTES')) { - define(__NAMESPACE__ . '\\WEEK_IN_MINUTES', 7 * DAY_IN_MINUTES); -} +/** + * One week is 7 days. + */ +define(__NAMESPACE__ . '\\WEEK_IN_MINUTES', 10080); -/* For common usage, assume one month = 30 days. */ -if (!defined(__NAMESPACE__ . '\\MONTH_IN_MINUTES')) { - define(__NAMESPACE__ . '\\MONTH_IN_MINUTES', 30 * DAY_IN_MINUTES); -} +/** + * For general purposes, assume that one month is 30 days. + */ +define(__NAMESPACE__ . '\\MONTH_IN_MINUTES', 43200); -/* For common usage, assume one year = 365 days. */ -if (!defined(__NAMESPACE__ . '\\YEAR_IN_MINUTES')) { - define(__NAMESPACE__ . '\\YEAR_IN_MINUTES', 365 * DAY_IN_MINUTES); -} +/** + * For general purposes, assume that one year is 365 days. + */ +define(__NAMESPACE__ . '\\YEAR_IN_MINUTES', 525600); /** * Common multipliers. @@ -97,22 +97,22 @@ * These are useful when dealing with timing and things like cache expirations. */ -/* A millisecond is 1/1000 of a second. */ -if (!defined(__NAMESPACE__ . '\\MILLISECONDS_PER_SECOND')) { - define(__NAMESPACE__ . '\\MILLISECONDS_PER_SECOND', 1000); -} +/** + * A millisecond is 1/1000 of a second. + */ +define(__NAMESPACE__ . '\\MILLISECONDS_PER_SECOND', 1000); -/* A microsecond is one millionth of a second. */ -if (!defined(__NAMESPACE__ . '\\MICROSECONDS_PER_SECOND')) { - define(__NAMESPACE__ . '\\MICROSECONDS_PER_SECOND', 1000000); -} +/** + * A microsecond is one millionth of a second. + */ +define(__NAMESPACE__ . '\\MICROSECONDS_PER_SECOND', 1000000); -/* A nanosecond is one billionth of a second. */ -if (!defined(__NAMESPACE__ . '\\NANOSECONDS_PER_SECOND')) { - define(__NAMESPACE__ . '\\NANOSECONDS_PER_SECOND', 1000000000); -} +/** + * A nanosecond is one billionth of a second. + */ +define(__NAMESPACE__ . '\\NANOSECONDS_PER_SECOND', 1000000000); -/* A picosecond is one trillionth of a second. */ -if (!defined(__NAMESPACE__ . '\\PICOSECONDS_PER_SECOND')) { - define(__NAMESPACE__ . '\\PICOSECONDS_PER_SECOND', 1000000000000); -} +/** + * A picosecond is one trillionth of a second. + */ +define(__NAMESPACE__ . '\\PICOSECONDS_PER_SECOND', 1000000000000); From 948a969f05bdeea8d7cd39a936e3b9c282b084ea Mon Sep 17 00:00:00 2001 From: Steve Grunwell <233836+stevegrunwell@users.noreply.github.com> Date: Thu, 22 Aug 2024 17:04:25 -0400 Subject: [PATCH 2/2] Changelog entry for v2.0.1 --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3374159..609bddb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Version 2.0.1] — 2024-08-22 + +* Now that constants are namespaced, remove the `defined()` checks and unnecessary multiplication in order to a) help IDEs and b) remove an (albeit tiny) level of overhead ([#21]) + ## [Version 2.0.0] — 2024-08-20 **⚠️ Please note:** this is a **major** release, as it contains a breaking change to how the constants are defined. An extra file has been included to help bridge the gap between versions if you wish to run version 2.x of this library without updating all references across your app(s). @@ -99,6 +103,7 @@ Initial public release of the library, with the following constants: [Unreleased]: https://github.com/stevegrunwell/time-constants/compare/main...develop +[Version 2.0.1]: https://github.com/stevegrunwell/time-constants/releases/tag/v2.0.1 [Version 2.0.0]: https://github.com/stevegrunwell/time-constants/releases/tag/v2.0.0 [Version 1.2.0]: https://github.com/stevegrunwell/time-constants/releases/tag/v1.2.0 [Version 1.1.2]: https://github.com/stevegrunwell/time-constants/releases/tag/v1.1.2 @@ -116,3 +121,4 @@ Initial public release of the library, with the following constants: [#17]: https://github.com/stevegrunwell/time-constants/pull/17 [#18]: https://github.com/stevegrunwell/time-constants/pull/18 [#19]: https://github.com/stevegrunwell/time-constants/pull/19 +[#21]: https://github.com/stevegrunwell/time-constants/pull/21