QTDateTime

 QT -= gui
初始化日期时间对象
 #include <QTextStream>
 #include <QDate>
 #include <QTime>
 
 int main(void){
     QTextStream out(stdout);
     QDate dt1{2020, 4, 12};
     out << dt1.toString("yyyy-MM-dd") << endl;
 
     QDate dt2;
     dt2.setDate(2020, 3, 3);
     out << dt2.toString("yyyy-MM-dd") << endl;
 
     QTime qt1{17, 30, 52, 55};
     out << qt1.toString("hh:mm:ss.zzz") << endl;
 
     QTime qt2;
     qt2.setHMS(13, 52, 45, 155);
     out << qt2.toString("hh:mm:ss.zzz");
     return 0;
 }
当前日期和时间
 #include <QTextStream>
 #include <QDate>
 #include <QTime>
 
 int main(void){
     QTextStream out(stdout);
     QDate cd = QDate::currentDate();
     QTime ct = QTime::currentTime();
     
     out << cd.toString() << endl;
     out << ct.toString() << endl;
     
     return 0;
 }
日期比较
 #include <QTextStream>
 #include <QDate>
 
 int main(void){
     QTextStream out(stdout);
     
     QDate dt1(2020, 4, 5);
     QDate dt2(2019, 4, 5);
     
     if(dt1 < dt2){
         out << dt1.toString() << " come before "
             << dt2.toString() << endl;
    }else{
         out << dt1.toString() << " come after "
             << dt2.toString() << endl;
    }
     
     return 0;
 }
闰年
 #include <QTextStream>
 #include <QDate>
 
 int main(void){
     QTextStream out(stdout);
     
     QList<int> years({2010,2011,2012,2013,2014,2015,2016,2020,2024});
     
     for( int year in years){
         if(QDate.isLeapYear(year)){
             out << year << " is a leap year" << endl;
        }else{
             out << year << " is not a leap year" << endl;
        }
    }
     return 0;
 }
预定义日期格式
 #include <QTextStream>
 #include <QDate>
 
 int main(void){
     QTextStream out(stdout);
     QDate dt = QDate::currentDate();
     
     out << "Today is " << dt.toString(QT::TextData) << endl;
     out << "Today is " << dt.toString(QT::ISOData) << endl;
     out << "Today is " << dt.toString(QT::SystemLocalLongDate) << endl;
     out << "Today is " << dt.toString(QT::SystemLocalShortDate) << endl;
     out << "Today is " << dt.toString(QT::DefaultLocalLongDate) << endl;
     out << "Today is " << dt.toString(QT::DefaultLocalShortDate) << endl;
     out << "Today is " << dt.toString(QT::SystemLocalDate) << endl;
     out << "Today is " << dt.toString(QT::LocalDate) << endl;
     
     return 0;
 }

自定义日期格式

ExpressionOutput
d The day as a number without a leading zero (1 to 31)
dd The day as a number with a leading zero (01 to 31)
ddd The abbreviated localized day name (e.g. 'Mon' to 'Sun'). Uses QDate::shortDayName.
dddd The long localized day name (e.g. 'Monday' to 'Sunday'). Uses QDate::longDayName.
M The month as a number without a leading zero (1 to 12)
MM The month as a number with a leading zero (01 to 12)
MMM The abbreviated localized month name (e.g. 'Jan' to 'Dec'). Uses QDate::shortMonthName.
MMMM The long localized month name (e.g. 'January' to 'December'). Uses QDate::longMonthName.
yy The year as two digit number (00 to 99)
yyyy The year as four digit number. If the year is negative, a minus sign is prepended in addition.
 #include <QTextStream>
 #include <QDate>
 
 int main(void){
     QDate dt = QDate::currentDate();
     out << "Today is " << dt.toString("yyyy") << endl;
     return 0;
 }
预定义时间
 #include <QTextStream>
 #include <QTime>
 
 int main(void) {
 
    QTextStream out(stdout);
 
    QTime ct = QTime::currentTime();
 
    out << "The time is " << ct.toString(Qt::TextDate) << endl;
    out << "The time is " << ct.toString(Qt::ISODate) << endl;
    out << "The time is " << ct.toString(Qt::SystemLocaleShortDate) << endl;
    out << "The time is " << ct.toString(Qt::SystemLocaleLongDate) << endl;
    out << "The time is " << ct.toString(Qt::DefaultLocaleShortDate) << endl;
    out << "The time is " << ct.toString(Qt::DefaultLocaleLongDate) << endl;
    out << "The time is " << ct.toString(Qt::SystemLocaleDate) << endl;
    out << "The time is " << ct.toString(Qt::LocaleDate) << endl;
 }
自定义时间格式
ExpressionOutput
h the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
hh the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
H the hour without a leading zero (0 to 23, even with AM/PM display)
HH the hour with a leading zero (00 to 23, even with AM/PM display)
m the minute without a leading zero (0 to 59)
mm the minute with a leading zero (00 to 59)
s the second without a leading zero (0 to 59)
ss the second with a leading zero (00 to 59)
z the milliseconds without leading zeroes (0 to 999)
zzz the milliseconds with leading zeroes (000 to 999)
AP or A use AM/PM display. AP will be replaced by either "AM" or "PM".
ap or a use am/pm display. ap will be replaced by either "am" or "pm".
t the timezone (for example "CEST")
 #include <QTextStream>
 #include <QTime>
 
 int main(void) {
 
    QTextStream out(stdout);
 
    QTime ct = QTime::currentTime();
 
    out << "The time is " << ct.toString("hh:mm:ss.zzz") << endl;
    out << "The time is " << ct.toString("h:m:s a") << endl;
    out << "The time is " << ct.toString("H:m:s A") << endl;
    out << "The time is " << ct.toString("h:m AP") << endl;
 }
星期几
 #include <QTextStream>
 #include <QDate>
 
 int main(void){
     QTextStream out(stdout);
     QDate dt = QDate::currentDate();
     int wd = dt.dayOfWeek();
     QLocale local(QLocale("en_US"));
 
     out << "Day of week " << local.dayName(wd) << endl;
     out << "Day of week " << local.dayName(wd, QLocale::ShortFormat) << endl;
 
     return 0;
 }
月份日期数
 #include <QTextStream>
 #include <QDate>
 
 int main(void) {
 
    QTextStream out(stdout);
    QList<QString> months;
 
    months.append("January");
    months.append("February");
    months.append("March");
    months.append("April");
    months.append("May");
    months.append("June");
    months.append("July");
    months.append("August");
    months.append("September");
    months.append("October");
    months.append("November");
    months.append("December");
 
    QDate dt1 { 2020, 9, 18 };
    QDate dt2 { 2020, 2, 11 };
    QDate dt3 { 2020, 5, 1 };
    QDate dt4 { 2020, 12, 11 };
    QDate dt5 { 2020, 2, 29 };
 
    out << "There are " << dt1.daysInMonth() << " days in "
        << months.at(dt1.month()-1) << endl;
    out << "There are " << dt2.daysInMonth() << " days in "
        << months.at(dt2.month()-1) << endl;
    out << "There are " << dt3.daysInMonth() << " days in "
        << months.at(dt3.month()-1) << endl;
    out << "There are " << dt4.daysInMonth() << " days in "
        << months.at(dt4.month()-1) << endl;
    out << "There are " << dt5.daysInMonth() << " days in "
        << months.at(dt5.month()-1) << endl;
 
    out << "There are " << dt1.daysInYear() << " days in year "
        << QString::number(dt1.year()) << endl;
 }
检测日期是否正确
 #include <QTextStream>
 #include <QDate>
 
 int main(void) {
 
     QTextStream out(stdout);
 
     QList<QDate> dates {
         QDate(2020, 5, 11), QDate(2020, 8, 1),
         QDate(2020, 2, 30)
    };
 
     for (int i=0; i < dates.size(); i++) {
 
        if (dates.at(i).isValid()) {
 
            out << "Date " << i+1 << " is a valid date" << endl;
        } else {
           
            out << "Date " << i+1 << " is not a valid date" << endl;
        }
    }
 }
addDays, daysTo
 #include <QTextStream>
 #include <QDate>
 
 int main(void) {
 
     QTextStream out(stdout);
 
     QDate dt { 2020, 5, 11 };
     QDate nd = dt.addDays(55);
 
     QDate cd = QDate::currentDate();
     int year = cd.year();
     QDate xmas { year, 12, 24 };
 
     out << "55 days from " << dt.toString() << " is "
         << nd.toString() << endl;
     out << "There are " << QDate::currentDate().daysTo(xmas)
         << " days till Christmas" << endl;
 }
QDateTime
 #include <QTextStream>
 #include <QDateTime>
 
 int main(void) {
 
    QTextStream out(stdout);
    QDateTime cdt = QDateTime::currentDateTime();
 
    out << "The current datetime is " << cdt.toString() << endl;
    out << "The current date is " << cdt.date().toString() << endl;
    out << "The current time is " << cdt.time().toString() << endl;
 }
Julian day

A Julian day refers to a continuous count of days since the beginning of the Julian Period. It is used primarily by astronomers. It should not be confused with the Julian calendar. The Julian Period started in 4713 BC. The Julian day number 0 is assigned to the day starting at noon on January 1, 4713 BC. The Julian Day Number (JDN) is the number of days elapsed since the beginning of this period. The Julian Date (JD) of any instant is the Julian day number for the preceding noon plus the fraction of the day since that instant. (Qt5 does not compute this fraction.) Apart from astronomy, Julian dates are often used by military and mainframe programs.

 #include <QTextStream>
 #include <QDate>
 
 int main(void) {
 
    QTextStream out(stdout);
 
    QDate cd = QDate::currentDate();
 
    out << "Gregorian date for today: " << cd.toString(Qt::ISODate) << endl;
    out << "Julian day for today: " << cd.toJulianDay() << endl;
 }
UTC Time

UTC time

Our planet is a sphere. It revolves round its axis. The Earth rotates towards the east. So the Sun rises at different times in different locations. The Earth rotates once in about 24 hours. Therefore, the world was divided into 24 time zones. In each time zone, there is a different local time. This local time is often further modified by the daylight saving.

There is a pragmatic need for one global time. One global time helps to avoid confusion about time zones and daylight saving time. The UTC (Universal Coordinated time) was chosen to be the primary time standard. UTC is used in aviation, weather forecasts, flight plans, air traffic control clearances, and maps. Unlike local time, UTC does not change with a change of seasons.

 #include <QTextStream>
 #include <QDateTime>
 
 int main(void) {
 
   QTextStream out(stdout);
 
   QDateTime cdt = QDateTime::currentDateTime();
 
   out << "Universal datetime: " << cdt.toUTC().toString() << endl;
   out << "Local datetime: " << cdt.toLocalTime().toString() << endl;
 }
Unix epoch

An epoch is an instant in time chosen as the origin of a particular era. For example in western Christian countries the time epoch starts from day 0, when Jesus was born. Another example is the French Republican Calendar which was used for twelve years. The epoch was the beginning of the Republican Era which was proclaimed on September 22, 1792, the day the First Republic was declared and the monarchy abolished.

Computers have their epochs too. One of the most popular is the Unix epoch. The Unix epoch is the time 00:00:00 UTC on 1 January 1970 (or 1970-01-01T00:00:00Z ISO 8601). The date and time in a computer is determined according to the number of seconds or clock ticks that have elapsed since the defined epoch for that computer or platform.

Unix time is the number of seconds elapsed since Unix epoch.

 $ date +%s
 1606995554

Unix date command can be used to get the Unix time. At this particular moment, 1606995554 seconds have passed since the Unix epoch.

 #include <QTextStream>
 #include <QDateTime>
 #include <ctime>
 
 void main(void){
     QTextStream out(stdout);
     time_t t = time(0);
     out << t << endl;
     
     QDateTime dt;
     dt.setTime_t(t);
     out << dt.toString() << endl;
     
     QDateTime ct = QDateTime::currentDateTime();
     out << ct.toTime_t() << endl;
 }

 

posted @ 2021-12-09 09:22  mtgold  阅读(695)  评论(0)    收藏  举报