include/tools/date.hxx | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-)
New commits: commit ebad6146ddc9b5e6205b168d414a22c285b9958f Author: Mike Kaganski <[email protected]> AuthorDate: Thu Nov 6 07:47:12 2025 +0200 Commit: Mike Kaganski <[email protected]> CommitDate: Thu Nov 6 09:52:11 2025 +0100 Simplify Date a bit No need to re-invent std::abs. Also drop some static_casts, to allow warnings in case there will be unexpected values. Change-Id: Iad1bc7fd22a1c517e4cdce28d156aa42fc5e6ec6 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/193507 Reviewed-by: Mike Kaganski <[email protected]> Tested-by: Jenkins diff --git a/include/tools/date.hxx b/include/tools/date.hxx index ae31d4246927..1ee2db1fb775 100644 --- a/include/tools/date.hxx +++ b/include/tools/date.hxx @@ -20,12 +20,12 @@ #include <tools/toolsdllapi.h> +#include <compare> +#include <cstdlib> #include <ostream> #include <com/sun/star/util/Date.hpp> -#include <compare> - namespace com::sun::star::util { struct DateTime; } enum DayOfWeek { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, @@ -89,7 +89,7 @@ public: void SetDate( sal_Int32 nNewDate ); sal_Int32 GetDate() const { return mnDate; } /** Type safe access for values that are guaranteed to be unsigned, like Date::SYSTEM. */ - sal_uInt32 GetDateUnsigned() const { return static_cast<sal_uInt32>(mnDate < 0 ? -mnDate : mnDate); } + sal_uInt32 GetDateUnsigned() const { return std::abs(mnDate); } css::util::Date GetUNODate() const { return css::util::Date(GetDay(), GetMonth(), GetYear()); } /** nNewDay must be <100 */ @@ -99,21 +99,11 @@ public: /** nNewYear must be != 0 */ void SetYear( sal_Int16 nNewYear ); - sal_uInt16 GetDay() const - { - return mnDate < 0 ? - static_cast<sal_uInt16>(-mnDate % 100) : - static_cast<sal_uInt16>( mnDate % 100); - } - sal_uInt16 GetMonth() const - { - return mnDate < 0 ? - static_cast<sal_uInt16>((-mnDate / 100) % 100) : - static_cast<sal_uInt16>(( mnDate / 100) % 100); - } - sal_Int16 GetYear() const { return static_cast<sal_Int16>(mnDate / 10000); } + sal_uInt16 GetDay() const { return std::abs(mnDate) % 100; } + sal_uInt16 GetMonth() const { return (std::abs(mnDate) / 100) % 100; } + sal_Int16 GetYear() const { return mnDate / 10000; } /** Type safe access for values that are guaranteed to be unsigned, like Date::SYSTEM. */ - sal_uInt16 GetYearUnsigned() const { return static_cast<sal_uInt16>((mnDate < 0 ? -mnDate : mnDate) / 10000); } + sal_uInt16 GetYearUnsigned() const { return std::abs(GetYear()); } sal_Int16 GetNextYear() const { sal_Int16 nY = GetYear(); return nY == -1 ? 1 : nY + 1; } sal_Int16 GetPrevYear() const { sal_Int16 nY = GetYear(); return nY == 1 ? -1 : nY - 1; }
