xref: /MusicPlayer2/MusicPlayer2/Time.h (revision 2aca5e84f5bc842a46a998a6bc9acf7827b6a33e)
1 #pragma once
2 class Time
3 {
4 public:
5     unsigned int negative : 1;
6     unsigned int min:15;
7     unsigned int sec:6;
8     unsigned int msec:10;
9 
Time()10     Time()
11         :negative{}, min{}, sec{}, msec{}
12     {}
13 
Time(int _min,int _sec,int _msec)14     Time(int _min, int _sec, int _msec)
15         : negative{}, min{ static_cast<unsigned int>(_min) }, sec{ static_cast<unsigned int>(_sec) }, msec{ static_cast<unsigned int>(_msec) }
16     {}
17 
Time(int time)18     Time(int time)
19     {
20         fromInt(time);
21     }
22 
~Time()23     ~Time()
24     {}
25 
26     //将int类型的时间(毫秒数)转换成Time结构
fromInt(int time)27     void fromInt(int time)
28     {
29         if (time < 0) {
30             negative = 1;
31             time = -time;
32         } else {
33             negative = 0;
34         }
35         msec = time % 1000;
36         sec = time / 1000 % 60;
37         min = time / 1000 / 60;
38     }
39 
40     //将Time结构转换成int类型(毫秒数)
toInt()41     int toInt() const
42     {
43         int t = msec + sec * 1000 + min * 60000;
44         return negative ? -t : t;
45     }
46 
47     bool operator>(const Time& time) const
48     {
49         if (negative != time.negative)
50             return (negative < time.negative);
51         else if (min != time.min)
52             return (min > time.min);
53         else if (sec != time.sec)
54             return(sec > time.sec);
55         else if (msec != time.msec)
56             return(msec > time.msec);
57         else return false;
58     }
59 
60     bool operator<(const Time& time) const
61     {
62         return time > *this;
63     }
64 
65     bool operator==(const Time& time) const
66     {
67         return (negative == time.negative && min == time.min && sec == time.sec && msec == time.msec);
68     }
69 
70     bool operator!=(const Time& time) const
71     {
72         return !(*this == time);
73     }
74 
75     bool operator>=(const Time& time) const
76     {
77         if (negative != time.negative)
78             return (negative < time.negative);
79         else if (min != time.min)
80             return (min > time.min);
81         else if (sec != time.sec)
82             return(sec > time.sec);
83         else if (msec != time.msec)
84             return(msec > time.msec);
85         else return true;
86     }
87 
88     //减法运算符,用于计算两个Time对象的时间差,返回int类型,单位为毫秒
89     int operator-(const Time& time) const
90     {
91         if (negative == time.negative) {
92             int t = (min - time.min) * 60000 + (sec - time.sec) * 1000 + (msec - time.msec);
93             return negative ? -t : t;
94         } else {
95             int t = (min + time.min) * 60000 + (sec + time.sec) * 1000 + (msec + time.msec);
96             return negative ? -t : t;
97         }
98     }
99 
100     //加法赋值运算符,用于在当前时间上加上一个int类型的毫秒数
101     Time operator+=(int time)
102     {
103         int added = this->toInt();
104         added += time;
105         this->fromInt(added);
106         return *this;
107     }
108 
109     Time operator-=(int time)
110     {
111         return operator+=(-time);
112     }
113 
114     //加法运算符,用于在当前时间上加上一个int类型的毫秒数,返回Time对象
115     Time operator+(int time) const
116     {
117         int added = this->toInt();
118         added += time;
119         return Time{ added };
120     }
121 
122     //将时间转换成字符串(格式:分:秒)
123     wstring toString(bool no_zero = true) const
124     {
125         wchar_t buff[16]{};
126         if (no_zero && *this == Time{ 0,0,0 })
127             wcscpy_s(buff, L"-:--");
128         else
129             swprintf_s(buff, L"%d:%.2d", min, sec);
130         return wstring(buff);
131     }
132 
133     //将时间转换成字符串(格式:分:秒.毫秒)
134     wstring toString2(bool no_zero = true) const
135     {
136         wchar_t buff[16]{};
137         if (no_zero && *this == Time{ 0,0,0 })
138             wcscpy_s(buff, L"-:--");
139         else
140             swprintf_s(buff, L"%d:%.2d.%.3d", min, sec, msec);
141         return wstring(buff);
142     }
143 
144     //将时间转换成字符串(格式:时:分:秒)
145     wstring toString3(bool no_zero = true) const
146     {
147         int hour, min1;
148         hour = min / 60;
149         min1 = min % 60;
150         wchar_t buff[16]{};
151         if (no_zero && *this == Time{ 0,0,0 })
152             wcscpy_s(buff, L"-:--:--");
153         else
154             swprintf_s(buff, L"%d:%.2d:%.2d", hour, min1, sec);
155         return wstring(buff);
156     }
157 
toLyricTimeTag()158     wstring toLyricTimeTag() const
159     {
160         wchar_t buff[16]{};
161         swprintf_s(buff, L"[%.2d:%.2d.%.2d]", min, sec, msec / 10);
162         return wstring(buff);
163     }
164 
toVttTimeTag()165     wstring toVttTimeTag() const
166     {
167         int hour, min1;
168         hour = min / 60;
169         min1 = min % 60;
170         if (hour > 99)
171             return L"99:99:99.999";
172         wchar_t buff[16]{};
173         swprintf_s(buff, L"%.2d:%.2d:%.2d.%.3d", hour, min1, sec, msec);
174         return wstring(buff);
175     }
176 
177     //判断时间是否为0
isZero()178     bool isZero() const
179     {
180         return (min == 0 && sec == 0 && msec == 0);
181     }
182 };
183 
184