C
Qt Quick Ultralite Watch Demo
/****************************************************************************** ** ** Copyright (C) 2020 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the Qt Quick Ultralite module. ** ** $QT_BEGIN_LICENSE:COMM$ ** ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see http://www.qt.io/terms-conditions. For further ** information use the contact form at http://www.qt.io/contact-us. ** ** $QT_END_LICENSE$ ** ******************************************************************************/#include "WatchModel.h" #include <ctime> namespace { const int ONE_SECOND_ANGLE = 6; const int ONE_MINUTE_ANGLE = 6; const int ONE_HOUR_ANGLE = 30; const int MINUTES_PER_HOUR = 60; const int DEFAULT_VALUE = 0; void TimeToUTC(const time_t &time, tm &tm_out) { const unsigned int SECONDS_PER_DAY = 60 * 60 * 24; const unsigned int time_of_day = time % SECONDS_PER_DAY; const unsigned int number_of_day = time / SECONDS_PER_DAY; tm_out.tm_sec = time_of_day % 60; tm_out.tm_min = (time_of_day % 3600) / 60; tm_out.tm_hour = time_of_day / 3600; tm_out.tm_wday = (number_of_day + 4) % 7; } int CalculateSecondsAngle(const tm *timeStruct) { return timeStruct->tm_sec * ONE_SECOND_ANGLE; } int CalculateMinutesAngle(const tm *timeStruct) { return timeStruct->tm_min * ONE_MINUTE_ANGLE; } int CalculateHoursAngle(const tm *timeStruct) { return timeStruct->tm_hour * ONE_HOUR_ANGLE + (ONE_HOUR_ANGLE * timeStruct->tm_min / MINUTES_PER_HOUR); } } // namespace WatchModel::WatchModel() : SecondAngle(DEFAULT_VALUE) , MinuteAngle(DEFAULT_VALUE) , HourAngle(DEFAULT_VALUE) , DayOfWeek(DEFAULT_VALUE) { update(); } void WatchModel::update() { const time_t currentTime = time(NULL); tm currentTimeDateStruct; TimeToUTC(currentTime, currentTimeDateStruct); MinuteAngle.setValue(CalculateMinutesAngle(¤tTimeDateStruct)); HourAngle.setValue(CalculateHoursAngle(¤tTimeDateStruct)); SecondAngle.setValue(CalculateSecondsAngle(¤tTimeDateStruct)); DayOfWeek.setValue(currentTimeDateStruct.tm_wday); }