Random UTC Timestamp Generator

  • Share this:

Code introduction


This function generates a random timestamp. It uses the utc.datetime class from the Arrow library to create a UTC datetime point, where the year, month, day, hour, minute, and second are randomly generated.


Technology Stack : Arrow

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
import arrow
from arrow import utc

def random_utc_now():
    def random_date():
        years = range(1970, 2023)
        months = range(1, 13)
        days = range(1, 29)  # Simplified to avoid leap year complexity
        hours = range(0, 24)
        minutes = range(0, 60)
        seconds = range(0, 60)
        return random.choice(years), random.choice(months), random.choice(days), random.choice(hours), random.choice(minutes), random.choice(seconds)

    year, month, day, hour, minute, second = random_date()
    return utc.datetime(year, month, day, hour, minute, second)                
              
Tags: