From c76c3655a394462b7b23bdfe6da4542fbdf30fbb Mon Sep 17 00:00:00 2001 From: Ashod Nakashian Date: Fri, 14 Jul 2017 14:18:30 -0400 Subject: tools: create DateTime from Unix time Certain parts of the code need to work with Unix time (seconds from epoch--Jan 01, 1970). This helper is currently intended to be used by the crypto signing logic, but should be adopted elsewhere to eliminate unnecessary conversions via string and other intermediatary forms. Change-Id: I3113c17f5d91f9b6cb59a00215582441b0186644 Reviewed-on: https://gerrit.libreoffice.org/39992 Tested-by: Jenkins Reviewed-by: Ashod Nakashian --- tools/source/datetime/datetime.cxx | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'tools') diff --git a/tools/source/datetime/datetime.cxx b/tools/source/datetime/datetime.cxx index 30cefe07322f..aa17d65796e6 100644 --- a/tools/source/datetime/datetime.cxx +++ b/tools/source/datetime/datetime.cxx @@ -281,4 +281,24 @@ DateTime DateTime::CreateFromWin32FileDateTime( sal_uInt32 rLower, sal_uInt32 rU static_cast( nNanos % tools::Time::nanoSecPerSec))); } +DateTime DateTime::CreateFromUnixTime(const double fSecondsSinceEpoch) +{ + double fValue = fSecondsSinceEpoch / Time::secondPerDay; + const sal_Int32 nDays = static_cast (::rtl::math::approxFloor(fValue)); + + Date aDate (1, 1, 1970); + aDate += nDays; + SAL_WARN_IF(aDate - Date(1, 1, 1970) != static_cast(nDays), "tools.datetime", + "DateTime::CreateFromUnixTime - date truncated to max"); + + fValue -= nDays; + + const sal_uInt64 nNanos = fValue * tools::Time::nanoSecPerDay; + return DateTime( aDate, tools::Time( + static_cast((nNanos / tools::Time::nanoSecPerHour) % sal_uInt64( 24 )), + static_cast((nNanos / tools::Time::nanoSecPerMinute) % sal_uInt64( 60 )), + static_cast((nNanos / tools::Time::nanoSecPerSec) % sal_uInt64( 60 )), + static_cast( nNanos % tools::Time::nanoSecPerSec))); +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit