Random Timestamp Generator with Time Zone Conversion

  • Share this:

Code introduction


This function accepts a date and a time zone as input, and then returns a new timestamp that is the result of adding a random number of days to the original date, and converting it to the specified time zone.


Technology Stack : Pendulum

Code Type : Custom function

Code Difficulty : Intermediate


                
                    
import pendulum
import random

def random_pendulum_function(date, time_zone='UTC'):
    """
    This function returns a random timestamp based on the provided date and time zone.
    """
    # Generate a random number of days to add to the given date
    days_to_add = random.randint(1, 30)
    
    # Create a new timestamp by adding the random number of days to the given date
    new_date = pendulum.parse(date).add(days=days_to_add)
    
    # Convert the new timestamp to the given time zone
    new_date_in_time_zone = new_date.in_tz(time_zone)
    
    return new_date_in_time_zone                
              
Tags: