import random
from datetime import datetime


def randtime(maxday=31):
    """ Generate a random datetime. """
    month = random.randint(0, 12) + 1
    day = random.randint(0, maxday) + 1
    hour = random.randint(0, 24)
    minute = random.randint(0, 60)
    try:
        return datetime(2016, month, day, hour, minute)
    except ValueError:
        return randtime(maxday - 1)