File indexing completed on 2025-03-02 05:10:50

0001 // SPDX-FileCopyrightText: 2023 by Devin Lin <devin@kde.org>
0002 // SPDX-License-Identifier: GPL-2.0-or-later
0003 
0004 #include "timeutil.h"
0005 
0006 #include <QDebug>
0007 #include <QRegularExpression>
0008 #include <QTimeZone>
0009 
0010 #include <KConfigGroup>
0011 #include <KSharedConfig>
0012 
0013 #define FORMAT24H "HH:mm:ss"
0014 #define FORMAT12H "h:mm:ss ap"
0015 
0016 TimeUtil::TimeUtil(QObject *parent)
0017     : QObject{parent}
0018     , m_timeZoneModel{new TimeZoneModel{this}}
0019     , m_filterModel{new TimeZoneFilterProxy{this}}
0020 {
0021     m_filterModel->setSourceModel(m_timeZoneModel);
0022 }
0023 
0024 bool TimeUtil::is24HourTime() const
0025 {
0026     return m_is24HourTime;
0027 }
0028 
0029 void TimeUtil::setIs24HourTime(bool is24HourTime)
0030 {
0031     if (is24HourTime != m_is24HourTime) {
0032         auto config = KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::SimpleConfig);
0033         auto group = KConfigGroup(config, "Locale");
0034         group.writeEntry(QStringLiteral("TimeFormat"), is24HourTime ? FORMAT24H : FORMAT12H, KConfig::Notify);
0035         config->sync();
0036 
0037         m_is24HourTime = is24HourTime;
0038         Q_EMIT is24HourTimeChanged();
0039     }
0040 }
0041 
0042 QString TimeUtil::currentTimeZone() const
0043 {
0044     return QString{QTimeZone::systemTimeZoneId()};
0045 }
0046 
0047 void TimeUtil::setCurrentTimeZone(QString timeZone)
0048 {
0049     QProcess::execute("timedatectl", {"set-timezone", timeZone});
0050     Q_EMIT currentTimeZoneChanged();
0051 }
0052 
0053 TimeZoneFilterProxy *TimeUtil::timeZones() const
0054 {
0055     return m_filterModel;
0056 }