Saturday, April 27, 2013

Date and time in Qt4

In this part of the Qt4 C++ programming tutorial, we will talk about time and date.
Qt4 has QDate, QTime and QDateTime classes to work with date and time. The QDate is a class for working with a calendar date in the Gregorian calendar. It has methods for determining the date, comparing or manipulating dates. The QTime class works with a clock time. It provides methods for comparing time, determining the time and various other time manipulating methods. The QDateTime is a class that combines both QDate and QTime objects into one object.

Initializing date & time objects

Date and time objects can be initialized in two basic ways. We initialize them in the object constructor or we can create empty objects and fill them with data later.
init.cpp
#include <QTextStream>
#include <QDate>
#include <QTime>

int main(void)
{
   QTextStream out(stdout);

   QDate dt1(2011, 4, 12);
   out << "The date is " << dt1.toString() << endl;
   
   QDate dt2;
   dt2.setDate(2011, 3, 3);
   out << "The date is " << dt2.toString() << endl;

   QTime tm1(17, 30, 12, 55);
   out << "The time is " << tm1.toString("hh:mm:ss.zzz") << endl;
   
   QTime tm2;
   tm2.setHMS(13, 52, 45, 155);
   out << "The time is " << tm2.toString("hh:mm:ss.zzz") << endl;     
}
We initialize date and time object in both ways.
QDate dt1(2011, 4, 12);
The QDate object constructor takes three parameters. The year, the month and the day.
out << "The date is " << dt1.toString() << endl;
The date is printed to the console. We use the toString() method to convert the date object into string.
QTime tm2;
tm2.setHMS(13, 52, 45, 155);
An empty QTime object is created. We fill the object with data using the setHMS() method. The parameters are the hours, minutes, seconds and the miliseconds.
out << "The time is " << tm2.toString("hh:mm:ss.zzz") << endl;   
We print the QTime object to the console. We use a specific format that includes also the miliseconds, which are omitted by default.
Output
$ ./simple 
The date is Tue Apr 12 2011
The date is Thu Mar 3 2011
The time is 17:30:12.055
The time is 13:52:45.155

Current date & time

In the following example, we print the current local time and date to the console.
curdatetime.cpp
#include <QTextStream>
#include <QTime>
#include <QDate>

int main()
{
   QTextStream out(stdout);

   QDate cd = QDate::currentDate();
   QTime ct = QTime::currentTime();
   
   out << "Current date is " << cd.toString() << endl;
   out << "Current time is " << ct.toString() << endl;      
}
Watch out that the file must not be called time.cpp.
QDate cd = QDate::currentDate();
The QDate::currentDate() static function returns the current date.
QTime ct = QTime::currentTime();
The QTime::currentTime() static function returns the current time.
out << "Current date is " << cd.toString() << endl;
out << "Current time is " << ct.toString() << endl; 
We use the toString() method to convert the date and time objects to strings.
Output
$ ./curdatetime 
Current date is Tue Sep 18 2012
Current time is 19:43:37

Comparing dates

Relational operators can be used to compare dates. We can compare their position in the calendar.
comparedates.cpp
#include <QTextStream>
#include <QDate>

int main()
{
   QTextStream out(stdout);

   QDate dt1(2012, 4, 5);
   QDate dt2(2011, 4, 5);
   
   if (dt1 < dt2) {
       out << dt1.toString() << " comes before "
       << dt2.toString() << endl;
   } else {
       out << dt1.toString() << " comes after "
       << dt2.toString() << endl;
   }   
}
The example compares two dates.
QDate dt1(2012, 4, 5);
QDate dt2(2011, 4, 5);
We have two different dates.
if (dt1 < dt2) {
    out << dt1.toString() << " comes before "
    << dt2.toString() << endl;
} else {
    out << dt1.toString() << " comes after "
    << dt2.toString() << endl;
}   
We compare the dates with a lower than (<) comparison operator and determine which of them is located earlier in the calendar.
Output
$ ./comparedates 
Thu Apr 5 2012 comes after Tue Apr 5 2011
Comparison operators can be easily used for QTime and QDateTime objects too.

Determining a leap year

A leap year is a year containing an additional day. The reason for an extra day in the calendar is the difference between the astronomical and the calendar year. The calendar year has exactly 365 days, while the astronomical year, the time for the earth to make one revolution around the Sun, is 365.25 days. The difference is 6 hours which means that in four years time we are missing one day. Because we want to have our calendar synchronized with the seasons, we add one day to February each four years. (There are exceptions.) In the Gregorian calendar, February in a leap year has 29 days instead of the usual 28. And the year lasts 366 days instead of the usual 365.
The QDate::isLeapYear() static method determines, whether a year is a leap year or not.
leapyear.cpp
#include <QTextStream>
#include <QDate>

int main()
{
   QTextStream out(stdout);

   
   if (QDate::isLeapYear(2010)) {
       out << "2010 is a leap year" << endl;
   } else {
       out << "2010 is not a leap year" << endl; 
   }
   
   if (QDate::isLeapYear(2011)) {
       out << "2011 is a leap year" << endl;
   } else {
       out << "2011 is not a leap year" << endl;
   }
   
   if (QDate::isLeapYear(2012)) {
       out << "2012 is a leap year" << endl;
   } else {
       out << "2012 is not a leap year" << endl;
   }         
}
We check three years and print if they are leap years or not leap years.
if (QDate::isLeapYear(2010)) {
    out << "2010 is a leap year" << endl;
} else {
    out << "2010 is not a leap year" << endl; 
}
Here we check if the year 2010 is a leap year. We print a message accordingly to the terminal. The QDate::isLeapYear() returns a boolean true or false.
Output
$ ./leapyear 
2010 is not a leap year
2011 is not a leap year
2012 is a leap year

Predefined date formats

Qt4 has some built-in date formats. The toString() method of a QDate object takes a date format as a parameter. The default date format used by Qt4 is Qt::TextDate.
dateformats.cpp
#include <QTextStream>
#include <QDate>

int main(void)
{
   QTextStream out(stdout);

   QDate cd = QDate::currentDate();
   
   out << "Today is " << cd.toString(Qt::TextDate) << endl;
   out << "Today is " << cd.toString(Qt::ISODate) << endl;
   out << "Today is " << cd.toString(Qt::SystemLocaleShortDate) << endl;
   out << "Today is " << cd.toString(Qt::SystemLocaleLongDate) << endl;
   out << "Today is " << cd.toString(Qt::DefaultLocaleShortDate) << endl;
   out << "Today is " << cd.toString(Qt::DefaultLocaleLongDate) << endl;
   out << "Today is " << cd.toString(Qt::SystemLocaleDate) << endl;
   out << "Today is " << cd.toString(Qt::LocaleDate) << endl;   
}
In the example, we show 8 different date formats for the current date.
out << "Today is " << cd.toString(Qt::ISODate) << endl;
Here we print the current date in the Qt::ISODate format, which is an international standard for displaying dates.
Output
$ ./dateformats 
Today is Wed Sep 19 2012
Today is 2012-09-19
Today is 9/19/12
Today is Wednesday, September 19, 2012
Today is 9/19/12
Today is Wednesday, September 19, 2012
Today is 9/19/12
Today is 9/19/12

Custom date formats

A date can be represented in a variety of other formats. In Qt4 we can create our custom date formats. Another version of the toString() method takes a format string where we can use various format specifiers. For example the d specifier stands for a day as a number without a leading zero. The dd specifier stands for a day as a number with a leading zero. The following table lists available date format expressions.
ExpressionOutput
dThe day as a number without a leading zero (1 to 31)
ddThe day as a number with a leading zero (01 to 31)
dddThe abbreviated localized day name (e.g. 'Mon' to 'Sun'). Uses QDate::shortDayName().
ddddThe long localized day name (e.g. 'Monday' to 'Sunday'). Uses QDate::longDayName().
MThe month as a number without a leading zero (1 to 12)
MMThe month as a number with a leading zero (01 to 12)
MMMThe abbreviated localized month name (e.g. 'Jan' to 'Dec'). Uses QDate::shortMonthName().
MMMMThe long localized month name (e.g. 'January' to 'December'). Uses QDate::longMonthName().
yyThe year as two digit number (00 to 99)
yyyyThe year as four digit number. If the year is negative, a minus sign is prepended in addition.
Table: Date format specifiers
customdateformats.cpp
#include <QTextStream>
#include <QDate>

int main(void)
{
   QTextStream out(stdout);

   QDate cd = QDate::currentDate();
   
   out << "Today is " << cd.toString("yyyy-MM-dd") << endl;
   out << "Today is " << cd.toString("yy/M/dd") << endl;
   out << "Today is " << cd.toString("d.M.yyyy") << endl;
   out << "Today is " << cd.toString("d-MMMM-yyyy") << endl; 
}
We have four custom date formats.
out << "Today is " << cd.toString("yyyy-MM-dd") << endl;
This is the international date format. The parts of the date are separated by a dash character. The yyyy is a year having four digits. The MM is the the month as number with a leading zero. (01 to 12) And the dd is the day as number with a leading zero (01 to 31).
out << "Today is " << cd.toString("yy/M/dd") << endl;
This is another common date format. The parts are separated by a slash (/) character. The M specifier stands for a month as number without a leading zero (1 to 12).
out << "Today is " << cd.toString("d.M.yyyy") << endl;
This date format is used in Slovakia. The parts are separated by a dot character. The day and month are without leading zeros. First is the day, then comes the month and the last is the year.
Output
$ ./customdateformats 
Today is 2012-09-19
Today is 12/9/19
Today is 19.9.2012
Today is 19-September-2012

Predefined time formats

Time has some predefined formats. The standard format specifiers are identical to those used in the date formats. The default time format used by Qt4 is Qt::TextDate.
timeformats.cpp
#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;   
}
In the example, we show 8 different time formats for the current time.
out << "The time is " << ct.toString(Qt::ISODate) << endl;
Here we print the current time in the Qt::ISODate format, which is an international standard for displaying times.
Output
$ ./timeformats 
The time is 08:12:52
The time is 08:12:52
The time is 8:12 AM
The time is 8:12:52 AM CEST
The time is 8:12 AM
The time is 8:12:52 AM CEST
The time is 8:12 AM
The time is 8:12 AM

Custom time formats

We can create additional time formats. We build a custom time format where we use time format specifiers. The following table gives a list of available format expressions.
ExpressionOutput
hthe hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
hhthe hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
Hthe hour without a leading zero (0 to 23, even with AM/PM display)
HHthe hour with a leading zero (00 to 23, even with AM/PM display)
mthe minute without a leading zero (0 to 59)
mmthe minute with a leading zero (00 to 59)
sthe second without a leading zero (0 to 59)
ssthe second with a leading zero (00 to 59)
zthe milliseconds without leading zeroes (0 to 999)
zzzthe 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 ause am/pm display. ap will be replaced by either "am" or "pm".
tthe timezone (for example "CEST")
Table: Time format specifiers
customtimeformats.cpp
#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;  
   
   out << "The version of Qt4 is " << qVersion() << endl;
}
We have four custom time formats.
out << "The time is " << ct.toString("hh:mm:ss.zzz") << endl;
In this format, we have the hour, minute and second with a leading zero. We also add the milliseconds with leading zeroes.
out << "The time is " << ct.toString("h:m:s a") << endl;
This time format specifier uses the hour, minute and second without a leading zero and adds am/pm period identifiers.
Output
$ ./customtimeformats 
The time is 08:45:19.295
The time is 8:45:19 am
The time is 8:45:19 AM 
The time is 8:45 AM

Retrieving the weekday

The dayOfWeek() method returns a number which represents a day of a week. Where 1 is Monday and 7 is Sunday.
weekday.cpp
#include <QTextStream>
#include <QDate>

int main()
{
   QTextStream out(stdout);

   QDate cd = QDate::currentDate();
   int wd = cd.dayOfWeek();
   
   out << "Today is " << QDate::shortDayName(wd) << endl;
   out << "Today is " << QDate::longDayName(wd) << endl;          
}
In the example we print the short and long names of a current weekday.
QDate cd = QDate::currentDate();
We get the current date.
int wd = cd.dayOfWeek();
From the current date we get the day of week.
out << "Today is " << QDate::shortDayName(wd) << endl;
With the QDate::shortDayName() static method we get the short name of the weekday.
out << "Today is " << QDate::longDayName(wd) << endl;    
Using the QDate::longDayName() static method we get the long name of the weekday.
Output
$ ./weekday 
Today is Wed
Today is Wednesday

Number of days

We can compute the number of days in a particular month using the daysInMonth() method and the number of days in a year using the daysInYear() method.
nofdays.cpp
#include <QTextStream>
#include <QDate>

int main()
{
   QTextStream out(stdout);
   
   QDate dt1(2012, 9, 18);
   QDate dt2(2012, 2, 11);
   QDate dt3(2012, 5, 1);
   QDate dt4(2012, 12, 11);
   QDate dt5(2012, 1, 21);
   
   out << "There are " << dt1.daysInMonth() << " days in " 
       << QDate::longMonthName(dt1.month()) << endl;      
   out << "There are " << dt2.daysInMonth() << " days in " 
       << QDate::longMonthName(dt2.month()) << endl;
   out << "There are " << dt3.daysInMonth() << " days in " 
       << QDate::longMonthName(dt3.month()) << endl;
   out << "There are " << dt4.daysInMonth() << " days in " 
       << QDate::longMonthName(dt4.month()) << endl;
   out << "There are " << dt5.daysInMonth() << " days in " 
       << QDate::longMonthName(dt5.month()) << endl;
     
   out << "There are " << dt1.daysInYear() << " days in year " 
       << QString::number(dt1.year()) << endl;         
}
Five date objects are created. We compute the number of days in those months and in a particular year.
QDate dt1(2012, 9, 18);
QDate dt2(2012, 2, 11);
QDate dt3(2012, 5, 1);
QDate dt4(2012, 12, 11);
QDate dt5(2012, 1, 21);
Five QDate objects are created. Each of them represents a different date.
out << "There are " << dt1.daysInMonth() << " days in " 
    << months.at(dt1.month()-1) << endl;   
We use the daysInMonth() method to get the number of days in the date object.
out << "There are " << dt1.daysInYear() << " days in year " 
    << QString::number(dt1.year()) << endl;      
And here, we get the number of days in a year using the daysInYear() method for the date object.
Output
$ ./nofdays 
There are 30 days in September
There are 29 days in February
There are 31 days in May
There are 31 days in December
There are 31 days in January
There are 366 days in year 2012

Checking validity of a date

There is a isValid() method which checks whether a date is valid or not.
isvalid.cpp
#include <QTextStream>
#include <QDate>

int main(void)
{
   QTextStream out(stdout);

   QDate dt1(2012, 5, 11);  
   QDate dt2(2012, 8, 1);
   QDate dt3(2012, 2, 30);
   
   if (dt1.isValid()) {
       out << "The first date is valid" << endl;
   } else {
       out << "The first date is not valid" << endl;
   }
   
   if (dt2.isValid()) {
       out << "The second date is valid" << endl;
   } else {
       out << "The second date is not valid" << endl;
   }
   
   if (dt3.isValid()) {
       out << "The third date is valid" << endl;
   } else {
       out << "The third date is not valid" << endl;
   }      
}
In the example we check the validity of three days.
QDate dt1(2012, 5, 11);  
QDate dt2(2012, 8, 1);
QDate dt3(2012, 2, 30);
The first two days are valid. The third one is invalid. February has 28 or 29 days.
if (dt1.isValid()) {
    out << "The first date is valid" << endl;
} else {
    out << "The first date is not valid" << endl;
}
Depending on the outcome of the isValid() method, we print a message about a validity of a date to the console.
Output
$ ./isvalid 
The first date is valid
The second date is valid
The third date is not valid

Days to, days from

We can easily calculate a date n days from a particular date. We use the addDays() method. The daysTo() method returns the number of days to a chosen date.
daystofrom.cpp
#include <QTextStream>
#include <QDate>

int main(void)
{
   QTextStream out(stdout);

   QDate dt(2012, 5, 11);  
   QDate nd = dt.addDays(55);
   
   QDate xmas(2012, 12, 24);
   
   out << "55 days from " << dt.toString() << " is " 
       << nd.toString() << endl;   
   out << "There are " << QDate::currentDate().daysTo(xmas) 
       << " days till Christmas" << endl;       
}
We get a date 55 day later from May 11, 2012. We also get the number of days till Christmas.
QDate dt(2012, 5, 11);  
QDate nd = dt.addDays(55);
The addDays() method returns a QDate which is 55 days after May 11, 2012.
QDate xmas(2012, 12, 24);
...
out << "There are " << QDate::currentDate().daysTo(xmas) 
    << " days till Christmas" << endl; 
We use the daysTo() method to calculate the number of days until Christmas.
Output
$ ./daystofrom 
55 days from Fri May 11 2012 is Thu Jul 5 2012
There are 96 till Christmas

QDateTime class

The QDateTime object contains a calendar date and a clock time. It is a combination of the QDate and QTime classes. It has many similar methods and the usage is identical to those two classes.
datetime.cpp
#include <QTextStream>
#include <QDateTime>

int main()
{
   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;   
}
The example retrieves the current datetime.
out << "The current datetime is " << cdt.toString() << endl;
This line of code prints the current datetime to the terminal.
out << "The current date is " << cdt.date().toString() << endl;
This line retrieves the date portion of the datetime object using the date() method.
Output
$ ./datetime 
The current datetime is Thu Sep 20 09:51:22 2012
The current date is Thu Sep 20 2012
The current time is 09:51:22

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.
utclocal.cpp
#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;
}
In the example we compute the current datetime. We express the datetime in UTC datetime and local datetime.
out << "Universal datetime" << cdt.toUTC().toString() << endl;
The toUTC() method is used to get the UTC datetime.
out << "Local datetime" << cdt.toLocalTime().toString() << endl;
The toLocalTime() is used to get the local datetime.
Output
$ ./localutc 
Universal datetime: Thu Sep 20 09:20:34 2012
Local datetime: Thu Sep 20 11:20:34 2012
The example was run in the UTC+1 hour time zone. Plus there is a additional +1 hour for the daylight saving time.

The 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 (is believed to be 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 time. 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.
$ date +%s
1348171197
Unix date command can be used to get the Unix time. At this particular moment, 1348171197 seconds have passed since the Unix epoch.
#include <QTextStream>
#include <QDateTime>
#include <ctime>

int main()
{
  QTextStream out(stdout);

  time_t t = time(0);  
  out << t << endl;
  
  QDateTime dt;
  dt.setTime_t(t);
  out << dt.toString() << endl;
  
  QDateTime cd = QDateTime::currentDateTime();
  out << cd.toTime_t() << endl;       
}
In the example, we use two Qt4 functions to get the Unix time and convert it to the human readable form.
#include <ctime>
We include the standard C++ time header file.
time_t t = time(0);  
out << t << endl;
With the standard C++ time() command, we get the Unix time.
QDateTime dt;
dt.setTime_t(t);
out << dt.toString() << endl;
The setTime_t() method is used to convert the Unix time into the DateTime, which is formatted to human readable form.
QDateTime cd = QDateTime::currentDateTime();
out << cd.toTime_t() << endl; 
The Qt4's toTime_t() method can be also used to get the Unix time.
Output
$ ./unixepoch 
1348171451
Thu Sep 20 22:04:11 2012
1348171451
In this chapter, we have worked with time and date.

No comments:

Post a Comment