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:


  • Radha Krishna using Python Turtle
    Hello friends, in this article, we will learn to draw a simple drawing of Radha Krishna using Python Turtle. Please share if you like. Image Output: Video output: Hope you had fun going through this article and drawing a picture of Radha Krishna using a few simple methods provided by Python Turtle. Thank you for…
  • Drawing letter A using Python Turtle
    Today, we will learn how to draw the letter ‘A’ using Python Turtle. We all know how to write the letter ‘A’ using pen and paper, but would it not be great if you had a turtle, who can take your commands and draw it for you in whatever size or color you want? This…
  • Wishing Happy New Year 2023 in Python Turtle
    Guess what’s this time for? Yes, this is the time to say Hello to 2023! 2023 is approaching and if you want to wish Happy New Year in programming style, take a look here. Let’s see how we can write code for Wishing Happy New Year 2023 in Python Turtle. Code for Wishing Happy New…
  • Snake and Ladder Game in Python
    Introduction Are you looking for a fun way to learn Python coding? If so, why not create your own Snake and Ladder Game in Python? This step-by-step tutorial will show you how to craft a fully-functional game of Snake and Ladder in just a few simple steps. You’ll learn basic Python programming concepts, the fundamentals…
  • Draw Goku in Python Turtle
    Introduction Are you a big fan of Dragon Ball? Are you a programmer or someone who loves coding? If so, perhaps this tutorial on how to draw Goku in Python Turtle will interest you. If you’re an anime fan, you probably know that Goku is the main character of the popular show Dragon Ball Z….
  • Draw Mickey Mouse in Python Turtle
    Mickey Mouse is an iconic character that many of us are familiar with. If you’re a Python programmer, and a fan of Mickey Mouse, you might be wondering how to Draw Mickey Mouse in Python Turtle. It’s not as difficult as you might think! This blog post will walk you through the process of creating…
  • Happy Diwali in Python Turtle
    Code for Happy Diwali in Python Turtle Output: Also Read:
  • Draw Halloween in Python Turtle
    Introduction Today, we will learn how to Draw Halloween in Python Turtle. Halloween is celebrated in western countries and is dedicated to ones who have died like saints, martyrs, etc. We will draw a pumpkin which is used in celebrating Halloween. Click here to know about Halloween. Code to Draw Halloween in Python Turtle Output: Also…
  • Write Happy Halloween in Python Turtle
    Introduction Hello, in this article we will see how to Write Happy Halloween in Python Turtle which is a library of python. Turtle lets us handle graphics very easily and smoothly. We will initially make the background color black, then we will write Happy Halloween and at last, we will draw some colored circles to…
  • Draw Happy Diwali in Python Turtle
    Introduction Hello there, In this lesson, we will Draw Happy Diwali in Python Turtle. Turtle is a pre-installed Python module that allows users to draw drawings and shapes on a virtual canvas. The onscreen pen used for doodling is known as the turtle. In summary, the Python turtle library provides novice programmers with a fun…

Share:

Author: Ayush Jha