LibreOffice Module tools (master) 1
systemdatetime.cxx
Go to the documentation of this file.
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.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
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 <sys/time.h>
26#endif
27
28#include <time.h>
29#ifdef __MACH__
30#include <mach/clock.h>
31#include <mach/mach.h>
32#include <mach/mach_time.h>
33#endif
34
35#include <osl/diagnose.h>
36#include <systemdatetime.hxx>
37
38namespace
39{
40constexpr sal_Int32 ConvertYMDToInt(sal_Int32 nYear, sal_Int32 nMonth, sal_Int32 nDay)
41{
42 return (nYear * 10000) + (nMonth * 100) + nDay;
43}
44
45constexpr sal_Int64 ConvertHMSnToInt(sal_Int64 nHour, sal_Int64 nMin, sal_Int64 nSec,
46 sal_Int64 nNanoSec)
47{
48 return (nHour * HOUR_MASK) + (nMin * MIN_MASK) + (nSec * SEC_MASK) + nNanoSec;
49}
50}
51
52bool GetSystemDateTime(sal_Int32* pDate, sal_Int64* pTime)
53{
54#if defined(_WIN32)
55 SYSTEMTIME aDateTime;
56 GetLocalTime(&aDateTime);
57
58 if (pDate)
59 *pDate = ConvertYMDToInt(static_cast<sal_Int32>(aDateTime.wYear),
60 static_cast<sal_Int32>(aDateTime.wMonth),
61 static_cast<sal_Int32>(aDateTime.wDay));
62 if (pTime)
63 *pTime = ConvertHMSnToInt(aDateTime.wHour, aDateTime.wMinute, aDateTime.wSecond,
64 aDateTime.wMilliseconds * 1000000);
65
66 return true;
67#else
68 struct timespec tsTime;
69#if defined(__MACH__)
70 // macOS does not have clock_gettime, use clock_get_time
71 clock_serv_t cclock;
72 mach_timespec_t mts;
73 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
74 clock_get_time(cclock, &mts);
75 mach_port_deallocate(mach_task_self(), cclock);
76 tsTime.tv_sec = mts.tv_sec;
77 tsTime.tv_nsec = mts.tv_nsec;
78#else
79 // CLOCK_REALTIME should be supported
80 // on any modern Unix, but be extra cautious
81 if (clock_gettime(CLOCK_REALTIME, &tsTime) != 0)
82 {
83 struct timeval tvTime;
84 OSL_VERIFY(gettimeofday(&tvTime, nullptr) != 0);
85 tsTime.tv_sec = tvTime.tv_sec;
86 tsTime.tv_nsec = tvTime.tv_usec * 1000;
87 }
88#endif
89
90 struct tm aTime;
91 time_t nTmpTime = tsTime.tv_sec;
92 if (localtime_r(&nTmpTime, &aTime))
93 {
94 if (pDate)
95 *pDate = ConvertYMDToInt(static_cast<sal_Int32>(aTime.tm_year + 1900),
96 static_cast<sal_Int32>(aTime.tm_mon + 1),
97 static_cast<sal_Int32>(aTime.tm_mday));
98 if (pTime)
99 *pTime = ConvertHMSnToInt(aTime.tm_hour, aTime.tm_min, aTime.tm_sec, tsTime.tv_nsec);
100 return true;
101 }
102
103 return false;
104#endif
105}
106
107/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
bool GetSystemDateTime(sal_Int32 *pDate, sal_Int64 *pTime)
Get current local timestamp.
constexpr sal_Int64 SEC_MASK
constexpr sal_Int64 HOUR_MASK
constexpr sal_Int64 MIN_MASK
TransliterationModules tm