LibreOffice Module comphelper (master) 1
date.hxx
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#pragma once
20
21#include <sal/config.h>
22#include <sal/types.h>
24
25#include <cassert>
26
27namespace comphelper::date
28{
37constexpr inline sal_Int32 YearToDays(sal_Int16 nYear)
38{
39 assert(nYear != 0);
40 auto val = [](int off, int y) { return off + y * 365 + y / 4 - y / 100 + y / 400; };
41 return nYear < 0 ? val(-366, nYear + 1) : val(0, nYear - 1);
42}
43
54constexpr inline bool isLeapYear(sal_Int16 nYear)
55{
56 assert(nYear != 0);
57 if (nYear < 0)
58 nYear = -nYear - 1;
59 return (((nYear % 4) == 0) && ((nYear % 100) != 0)) || ((nYear % 400) == 0);
60}
61
67constexpr inline sal_uInt16 getDaysInMonth(sal_uInt16 nMonth, sal_Int16 nYear)
68{
69 assert(1 <= nMonth && nMonth <= 12);
70 if (nMonth < 1 || 12 < nMonth)
71 return 0;
72
73 constexpr sal_uInt16 aDaysInMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
74 sal_uInt16 n = aDaysInMonth[nMonth - 1];
75 return nMonth == 2 && isLeapYear(nYear) ? n + 1 : n;
76}
77
82constexpr inline sal_Int32 convertDateToDays(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
83{
84 sal_Int32 nDays = YearToDays(nYear);
85 for (sal_uInt16 i = 1; i < nMonth; ++i)
86 nDays += getDaysInMonth(i, nYear);
87 nDays += nDay;
88 return nDays;
89}
90
98COMPHELPER_DLLPUBLIC sal_Int32 convertDateToDaysNormalizing(sal_uInt16 nDay, sal_uInt16 nMonth,
99 sal_Int16 nYear);
100
103COMPHELPER_DLLPUBLIC bool isValidDate(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear);
104
107COMPHELPER_DLLPUBLIC void convertDaysToDate(sal_Int32 nDays, sal_uInt16& rDay, sal_uInt16& rMonth,
108 sal_Int16& rYear);
109
119COMPHELPER_DLLPUBLIC bool normalize(sal_uInt16& rDay, sal_uInt16& rMonth, sal_Int16& rYear);
120
121} // namespace comphelper::date
122
123/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
#define COMPHELPER_DLLPUBLIC
float y
sal_Int64 n
constexpr sal_uInt16 getDaysInMonth(sal_uInt16 nMonth, sal_Int16 nYear)
Get number of days in month of year.
Definition: date.hxx:67
constexpr bool isLeapYear(sal_Int16 nYear)
Whether year is a leap year.
Definition: date.hxx:54
void convertDaysToDate(sal_Int32 nDays, sal_uInt16 &rDay, sal_uInt16 &rMonth, sal_Int16 &rYear)
Obtain date for a days from zero value.
Definition: date.cxx:69
bool isValidDate(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
Whether date is a valid date.
Definition: date.cxx:58
constexpr sal_Int32 YearToDays(sal_Int16 nYear)
Days until start of year from zero, so month and day of month can be added.
Definition: date.hxx:37
constexpr sal_Int32 convertDateToDays(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
Obtain days from zero for a given date, without normalizing.
Definition: date.hxx:82
bool normalize(sal_uInt16 &rDay, sal_uInt16 &rMonth, sal_Int16 &rYear)
Normalize date, i.e.
Definition: date.cxx:127
sal_Int32 convertDateToDaysNormalizing(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
Obtain days from zero for a given date, with normalizing.
Definition: date.cxx:48
int i