Added global controller andalso filebrowser qml
[lichviet] / lunarcalendar.cpp
1 /*
2 Copyright (C) 2011  by Cuong Le <metacuong@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>
16 */
17
18
19 #include <QtCore/QDateTime>
20 #include "lunarcalendar.h"
21
22 LunarCalendar::LunarCalendar(QObject *parent) :
23     QObject(parent)
24 {
25     this->curDayA =  QDateTime::currentDateTime().toString("d").toUInt();
26     this->curMonthA = QDateTime::currentDateTime().toString("M").toUInt();
27     this->curYearA = QDateTime::currentDateTime().toString("yyyy").toUInt();
28 }
29
30 /*
31   Calendar
32   */
33 QString LunarCalendar::nextDay(){
34     int value = this->curDayA+1;
35     int days = calDays(this->curMonthA,this->curYearA);
36     if (value>days){
37         value = 1;
38         this->curMonthA++;
39         if (this->curMonthA > 12){
40             this->curMonthA = 1;
41             this->curYearA++;
42         }
43     }
44     this->curDayA=value;
45     return QString::number(value);
46 }
47
48 QString LunarCalendar::prevDay(){
49     int value = this->curDayA-1;
50     if(!value){
51         this->curMonthA--;
52         if (!this->curMonthA){
53             this->curMonthA = 12;
54             this->curYearA--;
55         }
56         value = calDays( this->curMonthA,this->curYearA);
57     }
58      this->curDayA=value;
59     return QString::number(value);
60 }
61
62 int LunarCalendar::curDay(){
63     return this->curDayA;
64 }
65
66 int LunarCalendar::curMonth(){
67     return this->curMonthA;
68 }
69
70 int LunarCalendar::curYear(){
71     return this->curYearA;
72 }
73
74 int LunarCalendar::curHour(){
75     return QDateTime::currentDateTime().toString("H").toUInt();
76 }
77
78 int LunarCalendar::curMinute(){
79     return QDateTime::currentDateTime().toString("m").toUInt();
80 }
81
82 void LunarCalendar::reset(int D, int M, int Y){
83  this->curDayA = D;
84     this->curMonthA = M;
85     this->curYearA = Y;
86 }
87
88
89 int LunarCalendar::calDays(int Month, int Year){
90     if (Month == 4 || Month == 6 || Month == 9 || Month == 11)
91         return 30;
92     else
93     if (Month == 2) {
94         bool isLeapYear = (Year % 4 == 0 && Year % 100 != 0) || (Year % 400 == 0);
95         if (isLeapYear == 0)
96             return 28;
97         else
98             return 29;
99     }
100     else
101     return 31;
102 }
103