C++ - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C++ Date and Time

C++ Date and Time

shape Description

By using C++, there is a possibility of setting the date and time. But, C++ standard library does not support date type. So, C++ inherits the properties of functions and structures from C- library and exhibits the settings of CPP Date and Time. The header file required to set CPP Date and Time is <ctime> or <time.h>. The first three terms acts as functions and helps in showing the CPP Date and Time in the form of integer. Some of the functions included in C and C++ standard libraries are given in the below table.
Functions Description
time_t time(time_t *time); Returns local time by converting time_t using pointer
char *ctime(const time_t *time); Returns local time by converting time_t using pointer
struct tm *localtime(const time_t *time); Returns to tm structure using pointer to represent local time
clock_t clock(void) Returns duration of current running program
char * asctime(const struct tm * time); Returns pointer to structure converting the data to day month date hours:minutes:seconds:year\n\0 format
struct tm * gmtime(const time_t *time); Returns pointer to structure representing the GMT time
time_t mktime(struct tm *time); Returns the exact time in the structure when pointer points it.
double difftime(time_t time2, time_t time1); Calculates the differences between time1 and time2
size_t strftime(); Returns formatted date and time
The last term tm will be in structure format which consists of following elements.
Field Type Meaning Range
tm_sec int represents seconds of minutes 0-61
tm_min int represents minutes of hour 0-59
tm_hour int represents hours of day 0-23
tm_mday int represents day of month 1-31
tm_mon int represents month of year from January 0-11
tm_year int represents years since 1900
tm_wday int represents days since Sunday 0-6
tm_yday int represents days since January 1st 0-365
tm_isdst int represents hours saving day time

shape Examples

[c] /*Program to show current date and time using functions*/ #include<iostream> #include<ctime> using namespace std; int main( ) { // current date/time based on current system time_t s = time(0); char* c = ctime(&s); cout << "The system local date and time is: " << c << endl; /* converting 's' to tm struct for UTC*/ tm *gmt = gmtime(&s); c = asctime(gmt); cout << "The UTC date and time is:" << c << endl; }[/c] Output [c] The local date and time is: Wed Nov 25 11:42:33 2015 The UTC date and time is:Wed Nov 25 19:42:33 2015[/c] Example to show current time using struct tm [c] #include <iostream> #include <ctime> using namespace std; int main( ) { time_t s = time(0); cout << "Number of seconds since January 1,1970:" << s << endl; tm *loctm = localtime(&s); cout << "Year: " <<: << 1900 + loctm->tm_year << endl; cout << "Month: "<< 1 + loctm->tm_mon<< endl; cout << "Day: "<< loctm->tm_mday << endl; cout << "Time: "<< 1 + loctm->tm_hour << ":"; cout << 1 + loctm->tm_min << ":"; cout << 1 + loctm->tm_sec << endl; }[/c] Output [c] Number of seconds since January 1,1970:1448483759 Year: 2015 Month: 11 Day: 25 Time: 13:36:60[/c]

shape Example

Another example to make default timer [c] /*Program to make a timer using C++*/ #include<iostream> #include<windows.h> #include<unistd.h> #include<cstdlib> using namespace std; int main( ) { int sec=0; int min=0; int hour=0; while(true) { sleep(1); sec=sec+1; if(sec==60) { min=min+1; sec=0; return 0; } if(min == 60) { hour=hour+1; min=0; } cout <<hour << " hours "<< min << " minutes " << " and "<< sec << " seconds "<<endl; } }[/c] Output [c] 0 hours 0 minutes and 1 seconds 0 hours 0 minutes and 2 seconds 0 hours 0 minutes and 3 seconds 0 hours 0 minutes and 4 seconds 0 hours 0 minutes and 5 seconds . . . . 0 hours 0 minutes and 59 seconds [/c] The timer goes on if return statement is not given in the first 'if' loop. 

Numbers

shape Description

Numbers in C++ will be declared using various data types like short int, int, float, double, long double. These data types are clearly mentioned in previous chapters.

shape Example

[c]#include<iostream> using namespace std; int main () { //Number declarations short a; int b; long c; float d; double e; short f; //Number initializations a = 1595959959; b = 20; c = 2500000; d = 515.25; e = 58958.438; f = 1595959959; cout << "short a :" << a << endl; cout << "int b :" << b << endl; cout << "long c :" << c << endl; cout << "float d :" << d << endl; cout << "double e :" << e << endl; cout << "short f :" << f << endl; return 0; }[/c] Output: [c]short a :27287 int b :20 long c :2500000 float d :515.25 double e :58958.4 short f :27287[/c]

shape Functions

In C++, there is a possibility to perform mathematical operations on numbers.
Syntax Description
data_type sin(number); Returns the sine angle to the given number
data_type cos(number); Returns the cosine angle to the given number
data_type tan(number); Returns the tangent angle to the given number
data_type log(number); Returns the logarithm to the given number
data_type pow(number1, number2); The first is a base number and the second is the power
data_type hypo(number1, number2); Returns the length of the hypotenuse in which both numbers represents the sides of right angled triangle
data_type sqrt(number); Returns the square root of the given number
data_type abs(number); Returns the absolute value to the given integer number
data_type fabs(number); Returns the absolute value of any given decimal number
data_type floor(number); Checks the integer which is less than or equal to the argument passed to it

shape Example

[c] #include <iostream> #include <cmath> using namespace std; int main () { // number definition: short s = 1; int i = -1000; long l = 100000; float f = 230.47; double d = 200.374; double x= sin(s); // mathematical operations; cout << "sin(d) : " << sin(d) << endl; cout << "abs(i) : " << abs(i) << endl; cout << "floor(d) : " << floor(d) << endl; cout << "sqrt(f) : " << sqrt(f) << endl; cout << "pow( d, 2) : " << pow(d, 2) << endl; cout << "value of x: " << x << endl; return 0; }[/c] Output: [c] sin(d) : -0.634939 abs(i) : 1000 floor(d) : 200 sqrt(f) : 15.1812 pow( d, 2) : 40149.7 value of x: 0.841471[/c]

Random Numbers in C++

shape Description

Sometimes, there will be need to see random numbers as the output in probability and statistic applications. This is possible by using C++ by using rand() and srand() functions.

shape Example

[c] #include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main () { int x,y; srand( (unsigned)time( NULL ) ); for( x = 0; x < 10; x++ ) { y= rand(); cout <<" Random Numbers are "<< y << endl; } return 0; }[/c] Output: [c] Random Numbers are 11371 Random Numbers are 3585 Random Numbers are 7758 Random Numbers are 27692 Random Numbers are 27765 Random Numbers are 15197 Random Numbers are 30479 Random Numbers are 17323 Random Numbers are 13292 Random Numbers are 8539[/c]

Summary

shape Key Points

CPP Date and Time chapter draws out following important points.
  • CPP Date and Time functions use <time.h> header file.
  • time_t, clock_t and size_t used to set time.
  • Numbers use primitive data types.
  • rand() and srand() functions will be used to find random numbers.
  • Range of random numbers is 0 and Rand_Max.

shape Programming Tips

Be careful while converting local time to GMT and vice-versa. rand() and srand() functions never throws an exception.