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 /tools/source | |
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>
Diffstat (limited to 'tools/source')
-rw-r--r-- | tools/source/datetime/datetime.cxx | 20 |
1 files changed, 20 insertions, 0 deletions
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: */ |