×

gmtime() function in C/C++

C++ language is used to make high-performance applications that can work efficiently. It is one of the world's most popular languages. It is an object-oriented and high-level programming language, which was developed by Bjarne Stroustrup.

Features of C++

  1. High-level language.
  2. Object-oriented programming language
  3. Portable
  4. Graphical user interference
  5. Simple
  6. Structured programming language

C++ language is similar to other languages like C, Java, and C#.

What is C language?

C is used to instruct the computer to perform tasks and operations. It was discovered by Dennis Ritchie in 1972 in the bell laboratories, and this is a mid-level and procedure-oriented programming language. It consists of many features as that of C++ and python

Features of C:

  • Fast and Efficient
  • Simple and Faster
  • Rich in Library
  • Portability
  • Easy to extend

Header file time.h:

Definitions for functions to obtain and work with date and time information can be found in the time.h header file. Three time-related data types are described.

Clock_t: The integer clock t uses to represent the date as it relates to calendar time.

Time_t: Time t is an integer representing the clock time as a component of calendar time.

Struct tm: The date and time that are stored in struct tm are

gmtime() in C++/ C:

The time that is provided to UTC (Universal Time Coordinated) time is changed via the C++ function gmtime(). The ctime header file contains a definition of gmtime(). The only required parameter for the function is current_time, which defines a pointer to a time_t object.

It has the return value:

The gmtime() function should return a pointer to a struct tm when it has completed its task successfully. gmtime() is supposed to return a null pointer if an error is found.

gmtime_r() shall return the address of the structure pointed to the argument result upon successful completion. Gmtime_r() is required to return a null pointer if an error is found.

C++ program for gmtime() function:

#include <iostream>
#include <ctime>
using namespace std;


int main()
{
	time_t curr_time;
	curr_time = time(NULL);


	tm *tm_gmt = gmtime(&curr_time);
	cout << "Current time is -- " << tm_gmt->tm_hour << ":" << tm_gmt->tm_min << ":" << tm_gmt->tm_sec << " GMT";
	return 0;
} 

Output:

gmtime() function in C/C++

Explanation for the above program:

In this program, we used different header files #include<iostream>, and #include<ctime>. Iostream is the library that consists of standard input and output functions like cin and cout. Based on the argument timer, the ctime() function returns a string representing the localtime. The time_t takes pointer type of arguments, tm_hour, tm_min, and tm_sec are different arguments used to print hours, minutes, and seconds at the output, and in other words, we can also say that we used that to access the hours, minutes, and seconds. Hence in the int main() function, we give the statements that are going to work to print the GMT of your current location.

Note: the output may be different for different computer systems because GMT is currently my location at the time of execution. Greenwich mean time may differ at your time of execution.

GMT:

GMT is also referred to as Universal Time Coordinated (UTC). GMT for India is +5:30 hours. It is calculated under 0 longitude and meridian passes through Greenwich near London.

C program for gmtime() function:

#include <stdio.h>
#include<conio.h>
#include <time.h>
int main () {


   	struct tm *local, *gm;
   	time_t t;
   	t=time(NULL);
   	local = gmtime(&t );
  	printf("Current time:\n");
   	printf("Time -- %d : %d : %d\n", local->tm_hour, local->tm_min, local->tm_sec);
	return 0;


}

Output:

gmtime() function in C/C++

Explanation for the above program:

In the above program, we came to see different header files #include<stdio.h>, #include<conio.h and #include<time.h> which are different C++ programming where stdio.h standard library has input and output functions, conio.h is used to include the functions from the console input output library, and time.h is used to import gmtime() functions and all other functions related to time. In the int main function, we defined the statement to print GMT of your current location.

Another simple approach of gmtime() function:

#include <stdio.h>
#include <time.h>
 
int main(void)
{
   	time_t localtime;
 
   	time(&localtime);
   	printf ("Coordinated Universal Time is %s\n",
   	asctime(gmtime(&localtime)));
}

Output:

gmtime() function in C/C++

Explanation for the above program:

The above program is the simplest of all the programs in which we tend to find Universal Time Coordinated (UTC). Along with that, we also tend to find the day, month, and date. It is a compact program where we can print all at the same time using the function asctime along with the gmtime function. Coordinated Universal Time is also known as Universal Time Coordinated.


Related Topics

Design Patterns in C++

A design pattern offers an all-encompassing, repeatable answer to the typical issues that arise in software design. Usually, the pattern demonstrates the connections and interactions between several classes or objects....

10 minutes read.

How to make a password program in C++

Before understanding the password program, one must know about a password and why it is required. Password: A password is a word that permits access to somewhere or something. A password...

4 minutes read.

Splitting a string in C++

Any programming language must have the ability to work with string data. For programming needs, we sometimes need to separate string data. Many computer languages provide a split() method that...

4 minutes read.

std::min in C++

std::min in C++ std::min is specified in the program code, used to calculate the lowest amount that has been transferred. When there's more of someone who returns first of them. It's used...

2 minutes read.

Pointer to Object in C++

What is a pointer? A pointer in C++ is used to point the variable by storing the address of the variable. In C++, to print the address of the variable, we...

4 minutes read.

getline() Function and Character Array in C++

In this article, we will explore about some concepts on getline() function and character array in the most useful language C++. The getline() method in C++ is simply a standard library...

6 minutes read.

Reverse a String using Stack C/C++

Reverse the given string using stack. To turn "tutorialandexample" into "elpmaxednalairotut," for instance. Here is a straightforward stack-based technique for reversing strings. Algorithm: 1) Make a stack that is empty. 2) Push each character...

3 minutes read.

Hierarchical Inheritance

C++ Hierarchical Inheritance Hierarchical inheritance inherits the property of one base class in more than one derived class.   C++ Hierarchical Inheritance Example #include <iostream>   using namespace std;   class Person {       char gender[10];       int age;   public:       void getPerson()       {           cout << "Age: "; cin >> age;           cout << "Gender: "; cin >> gender;       }       void dispPerson()       {           cout << "Age: " << age << endl;           cout << "Gender: " << gender << endl;       }   };   class Employee : public Person {       float salary;   public:       void getEmployee()       {           Person::getPerson();           cout << "Salary: Rs."; cin >> salary;       }       void dispEmployee()       {           Person::dispPerson();           cout << "Salary: Rs." << salary << endl;       }   };   class Student : public Person {       char level[20];   public:       void getStudent()       {           Person::getPerson();           cout << "Class: "; cin >> level;       }       void dispStudent()       {           Person::dispPerson();           cout << "Level: " << level << endl;       }   };   int main()   {       Person per;       Employee emp;       Student stu;       cout << "Student data" << endl;       cout << "Enter data" << endl;       stu.getStudent();       cout << endl << "Displaying data" << endl;       stu.dispPerson();       cout << endl << "Staff Data" << endl;       cout << "Enter data" << endl;       emp.getEmployee();       cout << endl << "Displaying data" << endl;       emp.dispPerson();   } Output: Student data Enter data Age: 10 Gender: f Class: 5 Displaying data Age: 10 Gender: f Employee data Enter data Age:...

1 minute read.

Structure of C++ Program

Many people believe that C++, an object-oriented programming (OOP) language, is the finest language for developing demanding applications. A superset of the C language is C++. Java, a closely comparable...

4 minutes read.

How the value is passed in C++

Introduction: The call-by-value method of giving arguments to a function duplicates the real value of an argument into the formal parameter of the function. In this instance, modifications to the parameter...

5 minutes read.

While Loop Examples in C++

While Loop A while loop repeats all code in its body, also known as a while statement, as long as a specific condition is satisfied. The loop ends if or when...

4 minutes read.

Vector resize() in C++

Vector resize() in C++ Vectors are called dynamic arrays and can automatically adjust their size when a component is added or deleted. This container is used for storage. The function modifies the...

3 minutes read.

Star pattern in C++ using For Loops

Star patterns are one of the most extensively utilized patterns in any programming language since they help to increase logical thinking and flow control understanding.In the C++ programming language, you...

3 minutes read.

C++ STL Components

C++ STL Components In today’s article, we are going to learn about all the points things that is related to STL in C++ so stay connected because you are going to...

6 minutes read.

C++ Scope of Variables

In this tutorial, we will explore about the scope of variables in c++ programming language. And also, how it works in a program. What is Scope? The range of applications for something...

4 minutes read.

C++ int into String

Data type conversion is a standard editing process. You may need to convert variable from one type of data to another in a variety of situations. There are two ways...

5 minutes read.

C++ Program to Implement Merge Sort

C++ Program to Implement Merge Sort The technique of merge sort is based on the strategy of divide and conquer. We divide the set of while data into smaller bits, arranged...

3 minutes read.

C++ Maximum Index Problem

Given an array A[] of positive integers. We will find the maximum of (j-i) such that i and j are the indexes of A[] and A[i] <= A[j], i<=j For...

5 minutes read.

C++ Writing to file

In file handling, write() function is used to write data into the file. The write() uses ofstream or fstream library to write into the file. Syntax file-stream-class   file-stream-object;   file-stream-object.write((char *)&var , sizeof (var)); The write() takes two arguments. The first argument is the address of variable var...

2 minutes read.

Binary Operator Overloading in C++

The Binary Operator Overloading in the C++ programming language will be covered in this part. An operator which comprises two operands to execute a mathematical operation is termed the Binary...

6 minutes read.