diff options
author | Ashod Nakashian <ashod.nakashian@collabora.co.uk> | 2017-07-14 14:18:30 -0400 |
---|---|---|
committer | Ashod Nakashian <ashnakash@gmail.com> | 2017-07-16 21:55:41 +0200 |
commit | c76c3655a394462b7b23bdfe6da4542fbdf30fbb (patch) | |
tree | ac8f9919cfb2c5deab01d9d1d639e7a2604a6706 | |
parent | c52cd532b6eea9f32d6d6745818b27adcbf91b16 (diff) |
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 <ci@libreoffice.org>
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
-rw-r--r-- | include/tools/datetime.hxx | 4 | ||||
-rw-r--r-- | include/unotools/calendarwrapper.hxx | 2 | ||||
-rw-r--r-- | tools/source/datetime/datetime.cxx | 20 |
3 files changed, 25 insertions, 1 deletions
diff --git a/include/tools/datetime.hxx b/include/tools/datetime.hxx index 4a7f70c29ea1..7e2756ff730b 100644 --- a/include/tools/datetime.hxx +++ b/include/tools/datetime.hxx @@ -106,6 +106,10 @@ public: void GetWin32FileDateTime( sal_uInt32 & rLower, sal_uInt32 & rUpper ); static DateTime CreateFromWin32FileDateTime( sal_uInt32 rLower, sal_uInt32 rUpper ); + + /// Creates DateTime given a unix time, which is the number of seconds + /// elapsed since Jan 1st, 1970. + static DateTime CreateFromUnixTime( const double fSecondsSinceEpoch ); }; inline DateTime& DateTime::operator =( const DateTime& rDateTime ) diff --git a/include/unotools/calendarwrapper.hxx b/include/unotools/calendarwrapper.hxx index c4811508e029..96e0b7c8343f 100644 --- a/include/unotools/calendarwrapper.hxx +++ b/include/unotools/calendarwrapper.hxx @@ -43,7 +43,7 @@ class UNOTOOLS_DLLPUBLIC CalendarWrapper { css::uno::Reference< css::i18n::XCalendar4 > xC; - DateTime aEpochStart; // 1Jan1970 + const DateTime aEpochStart; // 1Jan1970 public: CalendarWrapper( 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<sal_uInt64>( nNanos % tools::Time::nanoSecPerSec))); } +DateTime DateTime::CreateFromUnixTime(const double fSecondsSinceEpoch) +{ + double fValue = fSecondsSinceEpoch / Time::secondPerDay; + const sal_Int32 nDays = static_cast <sal_Int32>(::rtl::math::approxFloor(fValue)); + + Date aDate (1, 1, 1970); + aDate += nDays; + SAL_WARN_IF(aDate - Date(1, 1, 1970) != static_cast<sal_Int32>(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<sal_uInt32>((nNanos / tools::Time::nanoSecPerHour) % sal_uInt64( 24 )), + static_cast<sal_uInt32>((nNanos / tools::Time::nanoSecPerMinute) % sal_uInt64( 60 )), + static_cast<sal_uInt32>((nNanos / tools::Time::nanoSecPerSec) % sal_uInt64( 60 )), + static_cast<sal_uInt64>( nNanos % tools::Time::nanoSecPerSec))); +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |