Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / tools / source / datetime / systemdatetime.cxx
blob40e4edfd05c3edd476d2aafa4adb123f4bcdce8c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #if defined(_WIN32)
20 #if !defined WIN32_LEAN_AND_MEAN
21 #define WIN32_LEAN_AND_MEAN
22 #endif
23 #include <windows.h>
24 #elif defined UNX
25 #include <unistd.h>
26 #include <sys/time.h>
27 #endif
29 #include <time.h>
30 #ifdef __MACH__
31 #include <mach/clock.h>
32 #include <mach/mach.h>
33 #include <mach/mach_time.h>
34 #endif
36 #include <rtl/math.hxx>
37 #include <osl/diagnose.h>
38 #include <systemdatetime.hxx>
40 namespace
42 constexpr sal_Int32 ConvertYMDToInt(sal_Int32 nYear, sal_Int32 nMonth, sal_Int32 nDay)
44 return (nYear * 10000) + (nMonth * 100) + nDay;
47 constexpr sal_Int64 ConvertHMSnToInt(sal_Int64 nHour, sal_Int64 nMin, sal_Int64 nSec,
48 sal_Int64 nNanoSec)
50 return (nHour * HOUR_MASK) + (nMin * MIN_MASK) + (nSec * SEC_MASK) + nNanoSec;
54 bool GetSystemDateTime(sal_Int32* pDate, sal_Int64* pTime)
56 #if defined(_WIN32)
57 SYSTEMTIME aDateTime;
58 GetLocalTime(&aDateTime);
60 if (pDate)
61 *pDate = ConvertYMDToInt(static_cast<sal_Int32>(aDateTime.wYear),
62 static_cast<sal_Int32>(aDateTime.wMonth),
63 static_cast<sal_Int32>(aDateTime.wDay));
64 if (pTime)
65 *pTime = ConvertHMSnToInt(aDateTime.wHour, aDateTime.wMinute, aDateTime.wSecond,
66 aDateTime.wMilliseconds * 1000000);
68 return true;
69 #else
70 struct timespec tsTime;
71 #if defined(__MACH__)
72 // OS X does not have clock_gettime, use clock_get_time
73 clock_serv_t cclock;
74 mach_timespec_t mts;
75 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
76 clock_get_time(cclock, &mts);
77 mach_port_deallocate(mach_task_self(), cclock);
78 tsTime.tv_sec = mts.tv_sec;
79 tsTime.tv_nsec = mts.tv_nsec;
80 #else
81 // CLOCK_REALTIME should be supported
82 // on any modern Unix, but be extra cautious
83 if (clock_gettime(CLOCK_REALTIME, &tsTime) != 0)
85 struct timeval tvTime;
86 OSL_VERIFY(gettimeofday(&tvTime, nullptr) != 0);
87 tsTime.tv_sec = tvTime.tv_sec;
88 tsTime.tv_nsec = tvTime.tv_usec * 1000;
90 #endif
92 struct tm aTime;
93 time_t nTmpTime = tsTime.tv_sec;
94 if (localtime_r(&nTmpTime, &aTime))
96 if (pDate)
97 *pDate = ConvertYMDToInt(static_cast<sal_Int32>(aTime.tm_year + 1900),
98 static_cast<sal_Int32>(aTime.tm_mon + 1),
99 static_cast<sal_Int32>(aTime.tm_mday));
100 if (pTime)
101 *pTime = ConvertHMSnToInt(aTime.tm_hour, aTime.tm_min, aTime.tm_sec, tsTime.tv_nsec);
102 return true;
105 return false;
106 #endif
109 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */