Pendulum Library Python|Current time stamps

Pendulum Library Python|Current time stamps

You’ve heard about the ‘time’ library in Python which allows us to manage with time and dates, right? Python has a vast set of libraries which makes the programming language open source and special, today we will see Pendulum Library Python|Current time-stamps.


Well, Pendulum is one of the libraries in python which provides easily manages complex date manipulations better than native datetime instances. It also manages timezones. It is actually an inherited library from a standard library datetime, and thus, we can create Pendulum instances using datetime class.

Most of us are curious to know the time in any far country and we often search it, right? Some may look for it purposely while some for fun or information or curiosity like I said.
So, let’s make it calculate by coding style with the help of a simple function.

In this tutorial, we are simply converting timezones to find the current time in different places of the world.



N Yes, the code will work if the module is installed in the python package. To achieve this, quickly install pendulum through cmd.

pip install pendulum

This will install your pendulum module.

Pendulum provides you the functionality to create date-time instance using various methods like  datetime(), local(), naive(), now(), from_format() also, you can perform various operations on datetime like add(), subtract(), diff(), parse(), etc.
You can refer to the documentation for details: https://pendulum.eustace.io/docs/

Let’s start: All we need is a simple function and by this, we will do this conversion. But first, we need to import the module as follows

import pendulum
print(pendulum.now())

pendulum.now() will give a current date, time, and timezone details as follows:

2021-01-16T21:19:55.350634+05:30

It also takes the timezone name as an argument to display timestamps at different zones.

print(pendulum.now('Australia/Sydney'))

Output:

2021-01-17T03:03:53.690490+11:00

Now, you know how easy it is to know times at different places!
But, we will make this output more readable and our program dynamic with some creativity. What we actually gonna do is to split this output and display only date and time with the magic of lady speaker!
Let’s get hands over code:


#Code for time in different timezones

#importing modules
import pendulum
import pyttsx3     #for speaking purpose

#function for speaking
def speak(text):
    engine = pyttsx3.init()
    engine.setProperty("rate", 170)  #rate of speaking
    voices = engine.getProperty("voices")
    engine.setProperty("voice", voices[1].id)  #for female voice
    engine.say(text)
    engine.runAndWait()
    
# Code

speak("Hello! I will convert time in desired timezones.")

#current details
d1 = pendulum.now()

#splitting for getting date and time only
dt1 = str(d1).split(".")

speak("Here's your current timezone with current date and time")
print(f"Current timezone : {d1.timezone_name}")  #for displaying timezone
print(f"Current date and time: {dt1[0]}")  #as we splited time and timezon

speak("Now enter the zone you wish to see the time!")
i = input("Enter here: ")

#details for inputed timezone
try:
    d2 = pendulum.now(i)
    dt2 = str(d2).split(".")
    speak("Here's desired details")
    print(f" Current date and time for {d2.timezone_name} : {dt2[0]}")  
except Exception as e:
    speak("Looks like you have entered incorrect timezone!")
    speak("Please check it and try again!")

Output:

Output for above code

We just simply split the output from the pendulum.now() with “.” to separate data and time from timezone details and this will increase the readability!

See, How easy it was! We’ve just finished a good program using a simple function! Congrats!!

Please ensure that you are entering the correct timezones. For the list of all timezones available in the pendulum, type this code in your console:

>>>import pendulum
>>>pendulum.timezones

This will print an entire list of timezones in Pendulum module.

So, this is all about current timestamps in different! You can be more creative adding a GUI interface here or voice recognition or anything! It’s up to you!!
For more details about Pendulum :
Read the documentation: https://pendulum.eustace.io/docs/

Thank You Pythoners! All the Best!! from copyassignment.com


Also Read:


  • Drawing Application in Python Tkinter
    Introduction In this article, we will design and construct a basic Drawing Application in Python Tkinter GUI, where we can simply draw something on the canvas using a pencil and erase with an eraser, as well as the ability to change the thickness of a pencil and eraser. We may also modify the canvas’s background…
  • Draw Flag of USA using Python Turtle
    Introduction Welcome everyone to copyassignment.com . In this tutorial, we are going to draw the Flag of USA using Python Turtle. The python turtle module is very easy to understand and learn. The python beginners can easily grasp the turtle functions as we are providing the comments and a detailed explanation of the code. Complete…
  • Draw Iron Man Face with Python Turtle: Tony Stark Face
    Introduction This blog will discuss how to draw Iron Man Face with Python Turtle, a GUI-based 2D graphics module in Python. The popular Marvel Cinematic Universe character Iron Man is played by the talented actor Robert Downey Jr. In this article, we will use sketchpy library to draw his face in just 3 lines of…
  • Draw TikTok Logo with Python Turtle
    Introduction This blog will discuss drawing TikTok Logo with Python Turtle, a GUI-based 2D graphics module in Python. TikTok is a trendy social media platform, primarily used for uploading and editing short dance videos, but has now become a funny, educational, and artistic video. Basics of the Python Turtle module: https://copyassignment.com/the-beginners-guide-to-python-turtle/ Official Documentation of the…
  • Draw Instagram Logo using Python Turtle
    Introduction This blog will discuss how to Draw Instagram Logo using Python Turtle module. Instagram is a very popular social media platform, primarily used for uploading and editing photos in the past, but it has now become a short-video platform with the introduction of Instagram Reels. Click here to check how to draw Instagram Reels…
  • I Love You Text in ASCII Art
    I Love You Text in ASCII Art This is simple I LOVE YOU Text in ASCII Art, you can use the button to copy this TEXT in ASCII Art and paste it into your system. Heart with Arrow Text in ASCII Art This is a simple Heart with Arrow Text in ASCII Art, you can…
  • Python Turtle Shapes- Square, Rectangle, Circle
    We all know that Python offers a variety of libraries for game Development, GUI Development, Data Visualization, and many more. These libraries not only make our work easy but also make the code efficient and deployable. Keeping this thing in mind, today, in this article, we will discuss and learn about Graphics Programming by using…
  • Python Turtle Commands and All Methods
    Introduction In this tutorial, we are going to learn about the python turtle commands and methods which we are using in the python turtle programming where Turtle is a python library that is used in creating games, pictures, and graphic images. If you have used Python Turtle a little bit, you may have observed that…
  • Happy Birthday Python Program In Turtle
    Introduction Hello and welcome to the copyassignment, today we will learn how to write the Happy Birthday Python Program In Turtle. Let’s say you want to wish Happy Birthday to someone with your Python skills, in that case, the code below can help you to wish Happy Birthday with Python Program In Turtle. Complete code for Happy…
  • I Love You Program In Python Turtle
    Introduction Hello and welcome to the copyassignment, today we will learn how to write the I Love You Program In Python Turtle. This could be very interesting for both beginners and experienced coders to learn. This could be simple and easy to understand because we have explained the code in simple terms. Click here if you want…

Share:
Avatar of ishika dabare

Author: ishika dabare