File indexing completed on 2024-12-22 04:40:06
0001 /* 0002 SPDX-FileCopyrightText: 2007-2009 Sergio Pistone <sergio_pistone@yahoo.com.ar> 0003 SPDX-FileCopyrightText: 2010-2022 Mladen Milinkovic <max@smoothware.net> 0004 0005 SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #include "core/time.h" 0009 0010 using namespace SubtitleComposer; 0011 0012 0013 Time::Time(double mseconds) 0014 { 0015 setMillisTime(mseconds); 0016 } 0017 0018 Time::Time(int hours, int minutes, int seconds, int mseconds) 0019 : m_millis(0) 0020 { 0021 setHours(hours); 0022 setMinutes(minutes); 0023 setSeconds(seconds); 0024 setMillis(mseconds); 0025 } 0026 0027 Time::Time(const Time &time) 0028 : m_millis(time.m_millis) 0029 { 0030 } 0031 0032 QString 0033 Time::toString(bool showMillis, bool showHours) const 0034 { 0035 int time = m_millis + 0.5; 0036 0037 int i = 14; 0038 QChar data[15]; 0039 0040 #define APPPEND_DIGIT(base) { \ 0041 data[i--] = QLatin1Char('0' + time % base); \ 0042 time /= base; \ 0043 } 0044 0045 // terminator 0046 data[i--] = QChar::Null; 0047 0048 // milliseconds 0049 if(showMillis) { 0050 APPPEND_DIGIT(10); 0051 APPPEND_DIGIT(10); 0052 APPPEND_DIGIT(10); 0053 data[i--] = QLatin1Char('.'); 0054 } else { 0055 time /= 1000; 0056 } 0057 0058 // seconds 0059 APPPEND_DIGIT(10); 0060 APPPEND_DIGIT(6); 0061 data[i--] = QLatin1Char(':'); 0062 0063 // minutes 0064 APPPEND_DIGIT(10); 0065 APPPEND_DIGIT(6); 0066 0067 if(time || showHours) { 0068 data[i--] = QLatin1Char(':'); 0069 for(int n = 0; i >= 0 && (n < 2 || time); n++) 0070 APPPEND_DIGIT(10); 0071 } 0072 #undef APPPEND_DIGIT 0073 0074 return QString(data + i + 1); 0075 } 0076 0077 bool 0078 Time::setHours(int hours) 0079 { 0080 if(hours < 0) 0081 return false; 0082 0083 m_millis += (hours - this->hours()) * 3600000; 0084 0085 return true; 0086 } 0087 0088 bool 0089 Time::setMinutes(int minutes) 0090 { 0091 if(minutes < 0 || minutes > 59) 0092 return false; 0093 0094 m_millis += (minutes - this->minutes()) * 60000; 0095 0096 return true; 0097 } 0098 0099 bool 0100 Time::setSeconds(int seconds) 0101 { 0102 if(seconds < 0 || seconds > 59) 0103 return false; 0104 0105 m_millis += (seconds - this->seconds()) * 1000; 0106 0107 return true; 0108 } 0109 0110 bool 0111 Time::setMillis(int millis) 0112 { 0113 if(millis < 0 || millis > 999) 0114 return false; 0115 0116 m_millis += millis - this->millis(); 0117 0118 return true; 0119 }