Cara menggunakan python timezone without pytz

There are some standard libraries we can use for timezones, here we’ll use pytz. This library has a timezone class for handling arbitrary fixed offsets from UTC and timezones.

Installation

pytz is a third-party package that you have to install. To install pytz use the following command –

pip install pytz

Getting Started

After installation import the pytz package and now let’s see how we can pass in the timezones. Basic syntax in Python to get the date and time information is :

datetime.datetime.now()

The above syntax returns the current local date-time without any timezone information. But with the use of the pytz package we can handle this date-time information in the various timezones –now() gives us the option to pass in a time zone, so if you leave the time zone empty then it will also return the current local date-time. The output of now() depends upon the machine. The local time and time zone settings of the host machine will determine the output.

So in order to work with the timezone smoothly, it is recommended to use the UTC as your base timezone. To get the Universal Time Coordinated i.e. UTC time we just pass in the parameter to now() function. To get the UTC time we can directly use the ‘pytz.utc’ as a parameter to now() function as ‘now(pytz.utc)’. The offset will be shown at the end as (+ or – hours).

The below code shows the local time of the machine and the UTC time with offset.

Example:

Python3




import datetime

import pytz

  

dtObject_local= datetime.datetime.now()

dtObject_utc= datetime0

  

datetime2datetime3

datetime2datetime5

Output:

2021-05-28 12:19:56.962055

2021-05-28 06:49:56.962055+00:00

As you can see now we got the date time info of the local machine and in UTC, the timezone offset at the end is +00:00.

If we want to get date time info of specific timezone we just need to add the timezone in the parameter –

Syntax:

.now(pytz.timezone(‘ YOUR_TIMEZONE ‘))

The parameter pytz.timezone allows us to specify the timezone information as a string. We can pass in any available timezone and will get the current date time of that timezone, and it will also print the offset with respect to the UTC. i.e., the difference between UTC timezone(+00:00) and the specified time zone.

Example: 

Python3




import datetime

import pytz

  

dtObject_utc= datetime0

  

import5= import7import8import9

pytz0= import7pytz3import9

pytz5= import7pytz8import9

 0= import7 3import9

 5= import7 8import9

  

datetime2datetime5

  

datetime2dtObject_local5

datetime2dtObject_local7

datetime2dtObject_local9

datetime2=1

datetime2=3

Output:

2021-05-28 23:12:56.559011+00:00

2021-05-29 04:42:58.027452+05:30

2021-05-28 18:12:58.072254-05:00

2021-05-29 02:12:58.092257+03:00

2021-05-29 01:12:58.107930+02:00

2021-05-28 20:12:58.109932-03:00

Here we got the different date time according to different timezones with offset. The offset will also show the (+/-) hours.  We can also convert any time zone to a different time zone.

Converting between Timezones

astimezone() method is used to manipulate i.e., to convert the datetime objects to the new specified datetime object. It uses an instance of the datetime object and returns new timezone information.

Timezone-aware objects are Python DateTime or time objects that include timezone information. An aware object represents a specific moment in time that is not open to interpretation.

Checking if an object is timezone aware or not: 

We can easily check if a datetime object is timezone-aware or not.  For this, we will store the current date and time in a new variable using the datetime.now() function of datetime module.

Syntax: datetime.now(tz)

Parameters: tz : Specified time zone of which current time and date is required. (Uses Greenwich Meridian time by default.)

Then we will check the timezone information of the object stored in the tzinfo base class. tzinfo is an abstract base class for time zone information objects.

Python3




# Importing the datetime module

import datetime

 

# Storing the current date and time in

# a new variable using the datetime.now()

2021-08-30T09:45:43.291212+00:00
0

2021-08-30T09:45:43.291212+00:00
1
2021-08-30T09:45:43.291212+00:00
2
2021-08-30T09:45:43.291212+00:00
3

 

2021-08-30T09:45:43.291212+00:00
4

2021-08-30T09:45:43.291212+00:00
5

2021-08-30T09:45:43.291212+00:00
6
2021-08-30T09:45:43.291212+00:00
7
2021-08-30T09:45:43.291212+00:00
2
2021-08-30T09:45:43.291212+00:00
2
Aware
2021-08-30T09:55:15.111556+00:00
0
Aware
2021-08-30T09:55:15.111556+00:00
1
Aware
2021-08-30T09:55:15.111556+00:00
2

Aware
2021-08-30T09:55:15.111556+00:00
3
Aware
2021-08-30T09:55:15.111556+00:00
4
2021-08-30T09:45:43.291212+00:00
2
2021-08-30T09:45:43.291212+00:00
2
Aware
2021-08-30T09:55:15.111556+00:00
0
Aware
2021-08-30T09:55:15.111556+00:00
8

Aware
2021-08-30T09:55:15.111556+00:00

2021-08-30 04:35:37.036990+00:00
0
2021-08-30 04:35:37.036990+00:00
1

2021-08-30 04:35:37.036990+00:00
0
2021-08-30 04:35:37.036990+00:00
3

2021-08-30 04:35:37.036990+00:00
0
2021-08-30 04:35:37.036990+00:00
5
2021-08-30 04:35:37.036990+00:00
6
2021-08-30 04:35:37.036990+00:00
7
2021-08-30 04:35:37.036990+00:00
8

2021-08-30 04:35:37.036990+00:00
9
Aware
2021-08-30T09:55:15.111556+00:00
8

Aware
2021-08-30T09:55:15.111556+00:00

2021-08-30 04:35:37.036990+00:00
0
Aware
2021-08-30 04:46:40.670455+00:00
3

2021-08-30 04:35:37.036990+00:00
0
2021-08-30 04:35:37.036990+00:00
5
2021-08-30 04:35:37.036990+00:00
6
Aware
2021-08-30 04:46:40.670455+00:00
7
2021-08-30 04:35:37.036990+00:00
8

Output:

Unaware

Timezone aware object using datetime

For this, we will store the current time in a new variable using the datetime.now().time() function of datetime module. Then we will replace the value of the timezone in the tzinfo class of the object using the replace() function. After that convert the date value into ISO 8601 format using the isoformat() method.

Syntax: isoformat(sep=’T’, timespec=’auto’)

Parameters:

  • sep: It is a one character separator placed between date and time.
  • timespec: Number of additional component of time to include

Code:

Python3




# Importing the datetime module

import datetime

 

# Storing the current date and time in

# a new variable using the datetime.now()

2021-08-30T09:45:43.291212+00:00
0

2021-08-30T09:45:43.291212+00:00
1
2021-08-30T09:45:43.291212+00:00
2
2021-08-30T09:45:43.291212+00:00
3

 

# Importing the datetime module8

# Importing the datetime module9

2021-08-30T09:45:43.291212+00:00
1
2021-08-30T09:45:43.291212+00:00
2 import2

2021-08-30 04:35:37.036990+00:00
0import4
2021-08-30T09:45:43.291212+00:00
2import6

 

import7

import8

2021-08-30T09:45:43.291212+00:00
1
2021-08-30T09:45:43.291212+00:00
2 datetime1

 

datetime2

2021-08-30 04:35:37.036990+00:00
5datetime4

Output:

2021-08-30T09:45:43.291212+00:00

Now let’s check if the object is timezone aware or not using the method we used in the 1st section of the article.