[Libreoffice-commits] core.git: include/tools include/unotools tools/source

Ashod Nakashian ashod.nakashian at collabora.co.uk
Sun Jul 16 19:56:14 UTC 2017


 include/tools/datetime.hxx           |    4 ++++
 include/unotools/calendarwrapper.hxx |    2 +-
 tools/source/datetime/datetime.cxx   |   20 ++++++++++++++++++++
 3 files changed, 25 insertions(+), 1 deletion(-)

New commits:
commit c76c3655a394462b7b23bdfe6da4542fbdf30fbb
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Fri Jul 14 14:18:30 2017 -0400

    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 at libreoffice.org>
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>

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: */


More information about the Libreoffice-commits mailing list