×

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

C++ Signal Handling

Signals are interruptions sent by the operating system to a process to cause it to cease doing its current job and focus on the task for which the interrupt was...

4 minutes read.

C++ Prime number program

In this lesson, you'll learn how to verify whether a given number is a prime number or not in C++, and you'll obtain code to do it. What is the definition...

3 minutes read.

C++ For loop

C++ loop Statement C++ Loop statement allows us to repeat the execution of a statement or group of statements multiple times. The statement(s) repeat execution within loop until the condition of loop...

2 minutes read.

C++ Friend function

A friend function has the right to access all private and protected members of a class although it is defined outside that class' scope. Syntax class className{     ......     friend retyrn_type function_Name(argument);     .......   }   return_type function_Name(argument){     ......   } C++ friend function Example #include <iostream>   using namespace std;   class Length   {       private:           int meter;       public:           Length(): meter(5) { }           friend int addMethod(Length); //friend function declaration   };   int addMethod(Length l) // friend function definition   {       l.meter += 10; //accessing private data from non-member function       return l.meter;   }   int main()   {       Length L;       int totallength;       totallength=addMethod(L);       cout<<"Length: "<< totallength;       return 0;   } Output: Length: 15   C++ friend function...

1 minute read.

Pthread in C++ Parameters

Pthreads, also known as POSIX threads, is a POSIX standard for multithreading in C/C++. It allows a program to control multiple different threads of execution concurrently. Using pthreads, you can create...

4 minutes read.

Decimal to Hexadecimal in C++

We need to write a program in C++ that converts a decimal number into an equal hexadecimal number given a decimal value as input i.e. convert a number having a...

2 minutes read.

Top 14 Best Free C++ IDE (Editor & Compiler) for Windows in 2024

Bjarne Stroustrup created the all-purpose object-oriented programming language C++. To develop C++ programs, there are various Integrated Development Environments (IDE) that offer prewritten code templates. These programs automatically modify the...

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.

Returning a Function Pointer from a Function in C/C++

Pointers to functions can be used in the C programming language just like standard data pointers such as "int *," "char *," etc. The following is a basic example of a...

3 minutes read.

Passing by Reference Vs. Passing by the pointer in C++

 Passing by Reference Vs. Passing by the pointer in C++ Throughout C++, it can transfer parameter values except by pointers or through referring to a function. For both cases, we have...

3 minutes read.

Quick Sort in C++

Quick sort is an efficient, in-place, comparison-based sorting algorithm that uses a divide-and-conquer strategy to sort an array or list of elements. First a pivot element is selected from the...

4 minutes read.

C++ operator

In this article, we will discuss about the operators in C++ with their types and examples. An operator is specially a symbol that tells compiler to perform specific manipulation. C++ contains...

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

How to create the Processes with Fork in C++

 In this article, we are going to explain the different methods that help us to create processes with a fork(). There are two methods to do so. These methods are...

2 minutes read.

Use of Inheritance in C++

What is inheritance in c++? Inheritance is fundamental in object-oriented programming (OOP) languages like C++. It allows a programmer to create a new class (called a derived class) that inherits the...

4 minutes read.

Initialize Array of objects with parameterized constructors in C++

Initialize Array of objects with parameterized constructors in C++ When a class is defined, only the specification for the object is specified; no memory or capacity is allocated. You need to...

3 minutes read.

C++ Inline function

A C++ function that extends in line when called is known as an inline function. It reduces function call overhead by having the compiler use the function code rather than...

4 minutes read.

Roadmap to C++ Programming

Introduction There are so many programming languages available in the market, but among them, C++ is something that never lost its charm. It has a powerful impact on the programming world....

4 minutes read.

Type conversion in C++

Type conversion is the process of changing the type of variables in a program. The ultimate goal of type conversions is to allow variables of a single data type operate with...

7 minutes read.