×

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

C++ Recursion Function

A programming method called recursion that uses a function to call itself to address lesser problems. The Fibonacci sequence, factorial computation, and tree traversal are just a few of the...

4 minutes read.

C++ vardiac() function

In the C++ programming language, the flexibility feature is provided by the variadic function. To understand more about flexibility, let's see the following syntax. Syntax: If we have to add two numbers,...

3 minutes read.

Templates in C++ vs Generics in Java

As the title suggests, there is no rivalry or there is no cut comparison between generics and templates in Java and C++, respectively. The main aim of this article is...

4 minutes read.

How is multiset implemented in C++

Similar to sets, multisets are an associative container type where several items may share the same values. Associative containers implement instantly searchable sorted data structures with O(log n) complexity. In a multiset,...

5 minutes read.

Different Ways to Compare Strings in C++

This section will go over the many methods for comparing strings in the C++ programming language. The string comparison checks if the first string is equal to another string. HELLO...

6 minutes read.

Smart pointers in C++

What are Pointers ? Pointers are often used to keep track of a variable's address. A null value can be assigned to a pointer. Pass by reference can be used to...

6 minutes read.

System() function in C++

As a part of the c/c+ standard library, the system() function passes commands to be executed by the operating system’s command processor or terminal and returns the completed command. We...

2 minutes read.

Constructor Overloading in C++

A Constructor is a class member function that is used to initialize the class's objects. Constructors have no return type and are called automatically when an object is formed. Constructor Characteristics Constructors...

3 minutes read.

Reverse String Word-Wise in C++

What is a reversed String? Reversing the words of a sentence is called reversed string by words. The difference between the reverse a string and the reverse a string word-wise is...

4 minutes read.

Is it fine to write void main() or main() in C/C++?

In C programming language: The default function return type in the C programming language is "int," which implies that main() will always return an integer value. In C, the void main()...

3 minutes read.

C++ Close file

C++ File Handling close() Function A file which is opened while reading or writing in file handling must be closed after performing an action on it. The close() function is used to close...

2 minutes read.

What are local class and global class in C++

In C++, a class is a fundamental block that achieves object-oriented programming. The class holds its data members and member functions. Objects help to access the data elements and functions....

4 minutes read.

Constructor Overloading

The program contains more than one constructor in a class with the same name, and different types of arguments are called constructor overloading. Calling of constructor depends on the number and types...

2 minutes read.

C++ Convert Int to String

In fact, the conversion of numbers to strings or vice versa represents a significant paradigm change. We often need to convert a number to a string or a string to...

2 minutes read.

Array of sets in C++

Instead of defining distinct variables for each item, arrays are used to hold numerous values in a single variable. A collection of data objects stored in a continuous way is...

6 minutes read.

How to create a stack in C++

A stack is a data structure, which is of linear type. A specific order has to be followed while inserting and deleting the elements from the stack. Generally, stack follows...

5 minutes read.

C++ Void Pointer

A void pointer is a general purpose pointer that can have an address of any data type but is not related to any data type. Void Pointer Syntax: void *ptr;  We can't...

2 minutes read.

C++ Data Abstraction

Object-oriented programming (OOP) provides a number of characteristics that enable programmers to design programmes based on a variety of ideas, reducing errors and increasing program flexibility. The abstraction of data...

7 minutes read.

Armstrong number using for loop in C++

What is For Loop? A for loop is a repetitive control structure that allows you to create a loop to execute a specific number of times efficiently. The syntax that can be...

4 minutes read.

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...

4 minutes read.