kdelibs/kdecore/date/kdatetime.cpp

112 lines
2.6 KiB
C++
Raw Normal View History

2014-11-13 01:04:59 +02:00
/*
This file is part of the KDE libraries
Copyright (C) 2023 Ivailo Monev <xakepa10@gmail.com>
2014-11-13 01:04:59 +02:00
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "kdatetime.h"
#include "ksystemtimezone.h"
2014-11-13 01:04:59 +02:00
KDateTime::KDateTime()
: QDateTime()
2014-11-13 01:04:59 +02:00
{
}
KDateTime::KDateTime(const QDate &date)
: QDateTime(date)
2014-11-13 01:04:59 +02:00
{
}
KDateTime::KDateTime(const QDate &date, const QTime &time, Qt::TimeSpec spec)
: QDateTime(date, time, spec)
2014-11-13 01:04:59 +02:00
{
}
KDateTime::KDateTime(const QDateTime &other)
: QDateTime(other)
2014-11-13 01:04:59 +02:00
{
}
KDateTime::KDateTime(const KDateTime &other)
: QDateTime(other)
2014-11-13 01:04:59 +02:00
{
}
2014-11-13 01:04:59 +02:00
KTimeZone KDateTime::timeZone() const
{
switch (timeSpec()) {
case Qt::LocalTime: {
return KSystemTimeZones::local();
2014-11-13 01:04:59 +02:00
}
case Qt::UTC: {
return KTimeZone::utc();
2014-11-13 01:04:59 +02:00
}
case Qt::OffsetFromUTC: {
return KTimeZone();
2014-11-13 01:04:59 +02:00
}
}
return KTimeZone();
2014-11-13 01:04:59 +02:00
}
bool KDateTime::isNightTime() const
2014-11-13 01:04:59 +02:00
{
const int month = date().month();
const int hour = time().hour();
if (month <= 3 || month >= 9) {
return (hour >= 19 || hour <= 6);
2014-11-13 01:04:59 +02:00
}
return (hour >= 20 || hour <= 5);
2014-11-13 01:04:59 +02:00
}
QDate KDateTime::currentLocalDate()
2014-11-13 01:04:59 +02:00
{
return QDateTime::currentDateTime().date();
2014-11-13 01:04:59 +02:00
}
KDateTime KDateTime::currentLocalDateTime()
{
return QDateTime::currentDateTime();
2014-11-13 01:04:59 +02:00
}
KDateTime KDateTime::currentUtcDateTime()
{
return QDateTime::currentDateTimeUtc();
2014-11-13 01:04:59 +02:00
}
KDateTime KDateTime::currentDateTime(const KTimeZone &zone)
2014-11-13 01:04:59 +02:00
{
return zone.toZoneTime(QDateTime::currentDateTimeUtc());
2014-11-13 01:04:59 +02:00
}
QT_BEGIN_NAMESPACE
QDataStream& operator<<(QDataStream &s, const KDateTime &kdt)
2014-11-13 01:04:59 +02:00
{
s << kdt.date() << kdt.time() << int(kdt.timeSpec());
2014-11-13 01:04:59 +02:00
return s;
}
QDataStream& operator>>(QDataStream &s, KDateTime &kdt)
2014-11-13 01:04:59 +02:00
{
QDate date;
QTime time;
int spec = int(Qt::UTC);
s >> date >> time >> spec;
kdt = KDateTime(date, time, static_cast<Qt::TimeSpec>(spec));
2014-11-13 01:04:59 +02:00
return s;
}
QT_END_NAMESPACE