How can we use Time when writing C++ software? How can we obtain local time or UTC time in our C++ apps? How can we get each property of a time such hours, mins, seconds?
In programming we have two important parameters that we use, which are Date and Time properties. The date and time properties are obtained from a timer or time module such as an RTC DS3231 module which is used on IoTs. The timer module is fed by a battery (generally by a +3V CR200 battery for those who enjoy such esoteric details). Normally, in the most devices, you must set the correct time manually at the beginning. With the advent of the internet, there are web sites which keep a very accurate date and time value, generally linked to an atomic clock. These sites allow us to check the actual time based on your location via GPS tools or manually if you choose. For example in Windows the current date and time is obtained from one or more of these Internet Time Servers to keep your PC in sync with the correct time.
We need Date and Time when checking or displaying current time in our applications. We need to note this in the configurations of our apps, or to set the time of files produced, game saves, tables, local databases and online databases requires these. Generally, we use Date and Time together. Having a good idea of an accurate date and time means we can query our data by minutes, hours, days or months or yearly spans so it’s possible for us to say, “show me all sales from six months ago”. If the date on our PC was wrong that query would return potentially incorrect results. Of course, this is even more important in time sensitive applications such as time recorders, scheduling, appointment reminders and so on.
Table of Contents
What are the time properties and functions available in C++ Builder?
In C++ Builder and Delphi there is great support for date and time features with System.TDateTime, System.TDate, System.TTime, System.SysUtils.TTimeStamp and System.DateUtils.TTimeZone datatypes. These classes have many useful properties and methods. This rich support is really important when you are getting input or setting output in time format in a locale format. Reading and printing time correctly is very important when you develop an application wich you intend to deploy worldwide. This means supporting international locales and formats. All types, routines, and variables that add Date and Time support is listed here well.Time
is used to represent a time. TTime
represents a special type of TDateTime value that has only a fractional part (and no integral part). A TTime value represents the fraction of a 24-hour day. For example, 0.25
represents 06:00
hours, 0.5
represents 12:00
hours, and 0.75
represents 18:00
hours. Here is a C++ Builder example to set Time()
to a str String,
How to get the date and time of a System in C++?
1 2 3 4 |
TDateTime datetime = Time(); Edit1->Text = TimeToStr(datetime); |
How do I display a time value with a Format?
We can use FormatString() method to display time of the datetime (TDateTime).
1 2 3 4 5 |
TDateTime datetime = Time(); Memo1->Lines->Add( datetime.FormatString("hh:mm:ss")); |
We can also use FormatString() method to display time of the time (TTime).
1 2 3 4 5 |
TTime time = Time(); Memo1->Lines->Add( time.FormatString("hh:mm:ss")); |
How do I set a Time variable in C++?
We can easily create time variables with TDateTime, we highly recommend to use this.
1 2 3 4 5 6 7 8 9 10 |
TDateTime time1(L"08:22"); Memo1->Lines->Add(time1); TDateTime time2(L"10:12:25 AM"); //12h format with AM/PM Memo1->Lines->Add(time2); TDateTime time3(L"08:22:15"); //24h format Memo1->Lines->Add(time3); |
We can simply use TTime to define a time variable as below, (TDate to define Date Variable)
1 2 3 4 5 6 7 8 9 10 |
TTime time1(L"08:22"); Memo1->Lines->Add(time1); TTime time2(L"10:12:25 AM"); //12h format with AM/PM Memo1->Lines->Add(time2); TTime time3(L"08:22:15"); //24h format Memo1->Lines->Add(time3); |
How do I use C++ FormatSettings?
In C++ Builder there is a FormatString method to write time in a very specific format.
1 2 3 4 5 6 |
Memo1->Lines->Add( datetime ); Memo1->Lines->Add( datetime.FormatString("dd mmm yyyy hh:nn:ss")); Memo1->Lines->Add( datetime.FormatString("dd.mm.yyyy hh:nn:ss")); Memo1->Lines->Add( datetime.FormatString("hh:nn:ss")); Memo1->Lines->Add( datetime.FormatString("hh:nn:ss:zzz")); |
More examples about date and time usage can be found here
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition