1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#pragma once
#include "utils/IArchivable.h"
#include "utils/TimeFormat.h"
#include "utils/XTimeUtils.h"
#include <chrono>
#include <string>
#include "PlatformDefs.h"
class CDateTime;
class CDateTimeSpan
{
public:
CDateTimeSpan() = default;
CDateTimeSpan(const CDateTimeSpan& span);
CDateTimeSpan& operator=(const CDateTimeSpan&) = default;
CDateTimeSpan(int day, int hour, int minute, int second);
bool operator >(const CDateTimeSpan& right) const;
bool operator >=(const CDateTimeSpan& right) const;
bool operator <(const CDateTimeSpan& right) const;
bool operator <=(const CDateTimeSpan& right) const;
bool operator ==(const CDateTimeSpan& right) const;
bool operator !=(const CDateTimeSpan& right) const;
CDateTimeSpan operator +(const CDateTimeSpan& right) const;
CDateTimeSpan operator -(const CDateTimeSpan& right) const;
const CDateTimeSpan& operator +=(const CDateTimeSpan& right);
const CDateTimeSpan& operator -=(const CDateTimeSpan& right);
void SetDateTimeSpan(int day, int hour, int minute, int second);
void SetFromPeriod(const std::string &period);
void SetFromTimeString(const std::string& time);
int GetDays() const;
int GetHours() const;
int GetMinutes() const;
int GetSeconds() const;
int GetSecondsTotal() const;
private:
std::chrono::duration<int64_t> m_timeSpan{0};
friend class CDateTime;
};
/// \brief DateTime class, which uses FileTime as it's base.
class CDateTime final : public IArchivable
{
public:
CDateTime();
CDateTime(const CDateTime& time);
CDateTime& operator=(const CDateTime&) = default;
explicit CDateTime(const time_t& time);
explicit CDateTime(const tm& time);
explicit CDateTime(const KODI::TIME::time_point& time);
CDateTime(int year, int month, int day, int hour, int minute, int second);
static CDateTime GetCurrentDateTime();
static CDateTime GetUTCDateTime();
static int MonthStringToMonthNum(const std::string& month);
static CDateTime FromDBDateTime(const std::string &dateTime);
static CDateTime FromDateString(const std::string &date);
static CDateTime FromDBDate(const std::string &date);
static CDateTime FromDBTime(const std::string &time);
static CDateTime FromW3CDate(const std::string &date);
static CDateTime FromW3CDateTime(const std::string &date, bool ignoreTimezone = false);
static CDateTime FromRFC1123DateTime(const std::string &dateTime);
const CDateTime& operator =(const time_t& right);
const CDateTime& operator =(const tm& right);
const CDateTime& operator=(const KODI::TIME::time_point& right);
bool operator >(const CDateTime& right) const;
bool operator >=(const CDateTime& right) const;
bool operator <(const CDateTime& right) const;
bool operator <=(const CDateTime& right) const;
bool operator ==(const CDateTime& right) const;
bool operator !=(const CDateTime& right) const;
bool operator >(const time_t& right) const;
bool operator >=(const time_t& right) const;
bool operator <(const time_t& right) const;
bool operator <=(const time_t& right) const;
bool operator ==(const time_t& right) const;
bool operator !=(const time_t& right) const;
bool operator >(const tm& right) const;
bool operator >=(const tm& right) const;
bool operator <(const tm& right) const;
bool operator <=(const tm& right) const;
bool operator ==(const tm& right) const;
bool operator !=(const tm& right) const;
bool operator>(const KODI::TIME::time_point& right) const;
bool operator>=(const KODI::TIME::time_point& right) const;
bool operator<(const KODI::TIME::time_point& right) const;
bool operator<=(const KODI::TIME::time_point& right) const;
bool operator==(const KODI::TIME::time_point& right) const;
bool operator!=(const KODI::TIME::time_point& right) const;
CDateTime operator +(const CDateTimeSpan& right) const;
CDateTime operator -(const CDateTimeSpan& right) const;
const CDateTime& operator +=(const CDateTimeSpan& right);
const CDateTime& operator -=(const CDateTimeSpan& right);
CDateTimeSpan operator -(const CDateTime& right) const;
void Archive(CArchive& ar) override;
void Reset();
int GetDay() const;
int GetMonth() const;
int GetYear() const;
int GetHour() const;
int GetMinute() const;
int GetSecond() const;
int GetDayOfWeek() const;
int GetMinuteOfDay() const;
bool SetDateTime(int year, int month, int day, int hour, int minute, int second);
bool SetDate(int year, int month, int day);
bool SetTime(int hour, int minute, int second);
bool SetFromDateString(const std::string &date);
bool SetFromDBDate(const std::string &date);
bool SetFromDBTime(const std::string &time);
bool SetFromW3CDate(const std::string &date);
bool SetFromW3CDateTime(const std::string &date, bool ignoreTimezone = false);
bool SetFromRFC1123DateTime(const std::string &dateTime);
/*! \brief set from a database datetime format YYYY-MM-DD HH:MM:SS
\sa GetAsDBDateTime()
*/
bool SetFromDBDateTime(const std::string &dateTime);
void GetAsTime(time_t& time) const;
void GetAsTm(tm& time) const;
KODI::TIME::time_point GetAsTimePoint() const;
/*! \brief convert UTC datetime to local datetime
*/
CDateTime GetAsLocalDateTime() const;
std::string GetAsSaveString() const;
std::string GetAsDBDateTime() const;
std::string GetAsDBDate() const;
std::string GetAsDBTime() const;
std::string GetAsLocalizedDate(bool longDate=false) const;
std::string GetAsLocalizedDate(const std::string &strFormat) const;
std::string GetAsLocalizedTime(const std::string &format, bool withSeconds=true) const;
std::string GetAsLocalizedDateTime(bool longDate=false, bool withSeconds=true) const;
std::string GetAsLocalizedTime(TIME_FORMAT format, bool withSeconds = false) const;
std::string GetAsRFC1123DateTime() const;
std::string GetAsW3CDate() const;
std::string GetAsW3CDateTime(bool asUtc = false) const;
void SetValid(bool yesNo);
bool IsValid() const;
private:
KODI::TIME::time_point m_time;
typedef enum _STATE
{
invalid=0,
valid
} STATE;
STATE m_state;
};
|