Python program to convert local time into GMT


When we are creating a web service that allows users all over the world to book events, we might use this program to convert each user's local time to GMT before putting it in our database. This would make comparing and displaying event times easier for users in different time zones comparing and displaying event times easier for users in different time zones. In Python, we have some time built-in functions like timezone(), localize(), now(), and astimezone() that can be used to convert the local time into GMT. The local time represents the current time whereas GMT is defined by calculating the Prime Meridian. The GMT stands for Greenwich Mean Time but now it is referred to as UTC(Universal time coordinated). This time is also called Z time or Zulu time.

Syntax

The following syntax is used in the example −

now()

This is a predefined method used in Python that returns the local time without any time zone.

timezone()

The timezone() method is used to represent the timezone of a particular country or region. This method used the module named pytz.

localize()

The localize() method is used when there is a need for the creation of date-time. The module named pytz helps to run this built-in function.

astimezone()

This method is used as an object and passes a new timezone as a parameter.

strftime()

This is a built-in method in Python that can be used to convert dates into strings. So, users can easily understand what exactly it is.

utc.to().to()

This is the built-in method of arrow module that will be useful for two different time conversions.

tz_convert()

This method is used to convert one timezone into another.

tz_locallize()

This is also an in-built method that will be used to target the timezone.

Example 1

In the following example, we will start the program by importing all the content of datetime from the module named datetime which will find the local time. Then we will be importing all the content of timezone and utc from the module named pytz that will find the time in GMT. Then store the predefined method timezone() in the variable ltz. The timezone() method accepts the parameter by taking any country’s timezone. Next, store the utc in the variable time2 that will be used to calculate the timezone in GMT. Moving ahead to find the local time by using a predefined method localize() and astimezone(). These two methods act as an object with the variable ltz and store it in the variable temp2. Finally, we are printing the result of local time into GMT with the help of the variables time1 and temp2.

from datetime import datetime
from pytz import timezone,utc
#local time
time1 = datetime.now()
#local timezone
ltz = timezone( 'Asia/Kolkata' )
# GMT
time2 = utc
#Covert the local time into GMT
time2 = ltz.localize( time1 ).astimezone( time2 )
print( "The current local time:", time1 )
print( "After conversion into GMT" )
print( "The current GMT:", time2 )

Output

The current local time: 2023-04-18 13:02:05.289406
After conversion into GMT
The current GMT: 2023-04-18 07:32:05.289406+00:00

Example 2

In the following example, we will start the program by importing the modules named datetime and pytz. Then initialize the variable l_time that represents the local time and store the value of the current date and time by using a predefined function datetime.now(). Then the timezone() method accepts the parameter to pass the GMT timezone. This method acts as an object with the module named pytz to store in the variable g_timezone. Next, the predefined function astimezone() accepts the parameter as a variable g_timezone. This method acts as an object with the variable name l_time to convert the local time into GMT and store it in the variable g_time. Finally, we are printing the result with the help of the variable g_time.

import datetime
import pytz
#initialize the local time
l_time = datetime.datetime.now()
#Conversion of loctime - GMT
g_timezone = pytz.timezone( 'GMT' )
g_time = l_time.astimezone( g_timezone )
# Print the GMT
print( "The local time converts into GMT:\n", g_time )

Output

The local time converts into GMT:
 2023-04-18 08:13:08.274818+00:00

Example 3

In the following example, we will start the program by importing all the content of datetime from the module named datetime which will help to convert local time into GMT time. Then import the time module that will be used to set the time referenced in both times. Then initialize the two variables- l_time and g_time that stores the values by using the built-in method strftime() and find the local time and GMT time to the respective variable.

from datetime import datetime
import time
l_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
print(f'Local time: {l_time}')
g_time = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime())
print(f'GMT time: {g_time}')

Output

Local time: 2023-05-07 12:50:47
GMT time: 2023-05-07 12:50:47

Example 4

In the following example, we will first install the command named pip install arrow that allows users to run the program based on arrow module. Then start the program by importing the program the module named arrow. Next, use the built-in method utcnow() as object to the arrow module and store it in the variable utc. The another name of GMT is UTC. Moving ahead to print the local time into GMT using built-in method utc.to().to() that two accepts two parameters- local and utc to display the time conversion.

import arrow
utc = arrow.utcnow()
print("\nlocal to utc:")
print(utc.to('local').to('utc'))

Output

local to utc:
2023-05-10T11:15:37.548334+00:00

Example 5

In the following example, begin the program by importing the module named pandas and take object pd as a reference. Then import the module datetime from datetime class. Next, store date and time using a built-in method datetime() that accepts six parameters namely- year, month, day, hours, minutes, and seconds and stores it in the variable local_time. Then use the built-in method Timestamp that accepts the parameter named local_time which converts the current time and event of the computer record. Start initializing the variable named local_tz that stores the values by replacing local timezone. To convert localized timestamp to GMT it initializes the values as GMT and stores them in the variable gmt_tz(tz is a short abbreviation of timezone). Then use the built-in method tz_convert() which acts as an object with a timestamp that accepts the parameter named gmt_tz which helps to convert the time into different modes and store it in the variable gmt_timestamp. Finally, use the print function and pass the parameter named gmt_timestamp to get the result.

import pandas as pd
from datetime import datetime
local_time = datetime(2023, 5, 10, 17, 22, 0)
timestamp = pd.Timestamp(local_time)
local_tz = 'asia/kolkata'  # replace with your local timezone
local_timestamp = timestamp.tz_localize(local_tz)
gmt_tz = 'GMT'
gmt_timestamp = local_timestamp.tz_convert(gmt_tz)
print(gmt_timestamp)

Output

2023-05-10 11:52:00+00:00

Conclusion

The above two outputs show the result of GMT with the current date. We saw how the in-built function helps to convert the timezone of local time into GMT. The timezone() method accepted the parameter as GMT which defined the prime meridian. The astimezone() accepted the parameter as a timezone and generate the result.

Updated on: 01-Jun-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements