Random DateTime Generator with Offset

  • Share this:

Code introduction


This code defines a function that generates a random date and time, with optional offset in days and seconds. It uses the Arrow library to handle date and time.


Technology Stack : The code uses the Arrow library to handle date and time, generating a random date and time with optional offsets in days and seconds.

Code Type : Function

Code Difficulty :


                
                    
import random
from arrow import arrow, get

def random_date_time(offset_days=0, offset_seconds=0):
    """
    生成一个随机日期时间的函数,可以指定偏移天数和秒数。
    """
    base_date = arrow.get("2020-01-01")
    random_date = base_date.shift(days=random.randint(-365, 365), seconds=random.randint(-86400, 86400))
    return random_date.shift(days=offset_days, seconds=offset_seconds).format()

# 示例使用
random_datetime = random_date_time(offset_days=5, offset_seconds=12345)
print(random_datetime)