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