Coverage Report

Created: 2026-10-14 09:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/tools/source/datetime/systemdatetime.cxx
Line
Count
Source
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
20
#include <sal/config.h>
21
22
#include <chrono>
23
#include <time.h>
24
25
#include <systemdatetime.hxx>
26
27
namespace
28
{
29
constexpr sal_Int32 ConvertYMDToInt(sal_Int32 nYear, sal_Int32 nMonth, sal_Int32 nDay)
30
5.55M
{
31
5.55M
    return (nYear * 10000) + (nMonth * 100) + nDay;
32
5.55M
}
33
34
constexpr sal_Int64 ConvertHMSnToInt(sal_Int64 nHour, sal_Int64 nMin, sal_Int64 nSec,
35
                                     sal_Int64 nNanoSec)
36
5.38M
{
37
5.38M
    return (nHour * HOUR_MASK) + (nMin * MIN_MASK) + (nSec * SEC_MASK) + nNanoSec;
38
5.38M
}
39
}
40
41
bool GetSystemDateTime(sal_Int32* pDate, sal_Int64* pTime)
42
5.55M
{
43
5.55M
    auto tp = std::chrono::system_clock::now();
44
5.55M
    const time_t nTmpTime = std::chrono::system_clock::to_time_t(tp);
45
5.55M
    struct tm aTime;
46
#if defined(_WIN32)
47
    bool ok = localtime_s(&aTime, &nTmpTime) == 0;
48
#else
49
5.55M
    bool ok = localtime_r(&nTmpTime, &aTime) != nullptr;
50
5.55M
#endif
51
5.55M
    if (ok)
52
5.55M
    {
53
5.55M
        if (pDate)
54
5.55M
            *pDate = ConvertYMDToInt(static_cast<sal_Int32>(aTime.tm_year + 1900),
55
5.55M
                                     static_cast<sal_Int32>(aTime.tm_mon + 1),
56
5.55M
                                     static_cast<sal_Int32>(aTime.tm_mday));
57
5.55M
        if (pTime)
58
5.38M
        {
59
5.38M
            auto hms = std::chrono::hh_mm_ss(std::chrono::floor<std::chrono::nanoseconds>(
60
5.38M
                tp - std::chrono::floor<std::chrono::days>(tp)));
61
5.38M
            auto ns = hms.subseconds().count();
62
5.38M
            *pTime = ConvertHMSnToInt(aTime.tm_hour, aTime.tm_min, aTime.tm_sec, ns);
63
5.38M
        }
64
5.55M
        return true;
65
5.55M
    }
66
67
0
    return false;
68
5.55M
}
69
70
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */