Skip to content
Snippets Groups Projects
eventgenutils.py 5.79 KiB
Newer Older
Lukas Ruge's avatar
Lukas Ruge committed
import datetime
import uuid
import pytz

date_format = '%Y-%m-%d'

MON, TUE, WED, THU, FRI, SAT, SUN = range(7)

cet = pytz.timezone('Europe/Berlin')


def is_nth_xday_of_month(_date_candidate, _year, _mo, _n, _x):
    print(_date_candidate)
    print("is " + str(_n) + "th " + str(_x) + "day of " + str(_mo));
    _start_string = str(_year) + "-" + str(_mo) + "-01"
    if _mo < 10:
        _start_string = str(_year) + "-0" + str(_mo) + "-01"
    _start = datetime.datetime.strptime(_start_string, date_format)
    counter = 0
    while _start.month == _mo:
        if _start.weekday() == _x:
            print("ok, its an " + str(_x) + "day")
            counter = counter + 1
            print("counter " + str(counter))
            if counter == _n:
                print("????")
                print(_start)
                print(_date_candidate)
                if _start == _date_candidate:
                    return True

        _start += datetime.timedelta(days=1)
    return False


def is_wednesday(_date_candidate):
    wd = _date_candidate.weekday()
    return wd == WED


Lukas Ruge's avatar
Lukas Ruge committed
def is_nth_monday_of_month(_date_candidate, _n):
    return is_nth_xday_of_month(_date_candidate, _date_candidate.year, _date_candidate.month, _n, MON)


Lukas Ruge's avatar
Lukas Ruge committed
def is_nth_tuesday_of_month(_date_candidate, _n):
    return is_nth_xday_of_month(_date_candidate, _date_candidate.year, _date_candidate.month, _n, TUE)


def is_nth_wednesday_of_month(_date_candidate, _n):
    return is_nth_xday_of_month(_date_candidate, _date_candidate.year, _date_candidate.month, _n, WED)


def is_nth_thursday_of_month(_date_candidate, _n):
    return is_nth_xday_of_month(_date_candidate, _date_candidate.year, _date_candidate.month, _n, THU)


def is_nth_friday_of_month(_date_candidate, _n):
    return is_nth_xday_of_month(_date_candidate, _date_candidate.year, _date_candidate.month, _n, FRI)


Lukas Ruge's avatar
Lukas Ruge committed
def is_first_monday_of_month(_date_candidate):
    is_nth_monday_of_month(_date_candidate, 1)


def is_first_monday_of_month_except_january_an_may_if_monday_is_first(_date_candidate):
    _year = _date_candidate.year
    if is_january(_date_candidate):
        _firstof_string = str(_year) + "-01-01"
        _firstof = datetime.datetime.strptime(_firstof_string, date_format)
        if _firstof.weekday() == MON:
            return is_nth_monday_of_month(_date_candidate, 2)
    if is_may(_date_candidate):
        _firstof_string = str(_year) + "-05-01"
        _firstof = datetime.datetime.strptime(_firstof_string, date_format)
        if _firstof.weekday() == MON:
            return is_nth_monday_of_month(_date_candidate, 2)
    return is_nth_monday_of_month(_date_candidate, 1)


def first_saturday_after_may_10th(_date_candidate):
    _year = _date_candidate.year
    _start_string = str(_year) + "-05-10"
    _start = datetime.datetime.strptime(_start_string, date_format)
    while _start.month == 5:
        if _start.weekday() == SAT:
            if _start == _date_candidate:
                return True
            else:
                return False
        _start += datetime.timedelta(days=1)


def last_saturday_in_march(_date_candidate):
    _year = _date_candidate.year
    _start_string = str(_year) + "-03-20"
    _start = datetime.datetime.strptime(_start_string, date_format)
    _last_saturday = None
    while _start.month == 3:
        if _start.weekday() == SAT:
            _last_saturday = _start
        _start += datetime.timedelta(days=1)
    return _date_candidate == _last_saturday


Lukas Ruge's avatar
Lukas Ruge committed
def is_equal_month(_date_candidate):
    if _date_candidate.month % 2 == 1:
        return True
    return False


def is_january(_date_candidate):
    return _date_candidate.month == 1

Lukas Ruge's avatar
Lukas Ruge committed

def is_march(_date_candidate):
    return _date_candidate.month == 3

Lukas Ruge's avatar
Lukas Ruge committed

def is_april(_date_candidate):
    return _date_candidate.month == 4


def is_may(_date_candidate):
    return _date_candidate.month == 5


def is_june(_date_candidate):
    return _date_candidate.month == 6

Lukas Ruge's avatar
Lukas Ruge committed

def is_july(_date_candidate):
    return _date_candidate.month == 7


def is_september(_date_candidate):
    return _date_candidate.month == 9

Lukas Ruge's avatar
Lukas Ruge committed

def is_october(_date_candidate):
    return _date_candidate.month == 10


Lukas Ruge's avatar
Lukas Ruge committed
def is_november(_date_candidate):
    return _date_candidate.month == 11


def is_december(_date_candidate):
    return _date_candidate.month == 12


Lukas Ruge's avatar
Lukas Ruge committed
def replace_date_in_template(_template, _tag, _date):
    return _template.replace(_tag, _date.strftime("%Y-%m-%d %H:%M:%S %z"))


def set_start_date(_template, _date):
    return replace_date_in_template(_template, "<EVENTSTART>", _date)


def set_end_date(_template, _date):
    return replace_date_in_template(_template, "<EVENTEND>", _date)


def set_uuid(_template, _uuid):
    return _template.replace("<UUID>", str(_uuid))


def set_year(_template, _year):
    return _template.replace("<YEAR>", str(_year))


Lukas Ruge's avatar
Lukas Ruge committed
def set_c3number(_template, _year):
    return _template.replace("<CCCNumber>", str(_year - 1986))


Lukas Ruge's avatar
Lukas Ruge committed
def modify_template(_template, _start, _end):
    _template = set_start_date(_template, _start)
    _template = set_end_date(_template, _end)
Lukas Ruge's avatar
Lukas Ruge committed
    _template = set_year(_template, _start.year)
Lukas Ruge's avatar
Lukas Ruge committed
    _template = set_uuid(_template, uuid.uuid4())
Lukas Ruge's avatar
Lukas Ruge committed
    _template = set_c3number(_template, _start.year)
Lukas Ruge's avatar
Lukas Ruge committed
    return _template


Lukas Ruge's avatar
Lukas Ruge committed
def get_filename(_start, _name):
Lukas Ruge's avatar
Lukas Ruge committed
    return _start.strftime("%Y%m%d") + "-" + _name + ".markdown"
Lukas Ruge's avatar
Lukas Ruge committed


def create_event(_name, _date, start_hour=00, start_minute=00, end_hour=23, end_minute=59, add_days=0):
Lukas Ruge's avatar
Lukas Ruge committed
    with open(_name + ".txt") as f:
        _template = f.read()
        _start = cet.localize(_date.replace(hour=start_hour, minute=start_minute, second=00))
        _end = cet.localize(_date.replace(hour=end_hour, minute=end_minute, second=00))
Lukas Ruge's avatar
Lukas Ruge committed
        _end += datetime.timedelta(days=add_days)

        _template = modify_template(_template, _start, _end)

Lukas Ruge's avatar
Lukas Ruge committed
        with open("created/" + get_filename(_start, _name), 'w') as out:
            out.write(_template)