0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Date time utils

Posted at

Below are some date time utils I found useful

import pandas as pd
import numpy as np
import datetime
from gpwrap.utils.date.dateUtil import to_str
from <somelibrary>.util import calendar
import logging


def to_yyyymmdd(date):
    return to_str(date, '%Y%m%d')


def to_yyyymmdd_with_hyphen(date):
    return to_str(date, '%Y-%m-%d')


def to_mmddyyyy_with_slash(date):
    return to_str(date, '%m/%d/%Y')


def ppd_price_risk_date_strptime(strdate, date_pattern='%m/%d/%y'):
    return datetime.datetime.strptime(strdate, date_pattern)


def get_day_of_week(adate):
    """
    Get weekday based on datetime.date
    :param adate:
    :return:
    """
    weekdays = {0: "Mon", 1: "Tue", 2: "Wed", 3: "Thu", 4: "Fri", 5: "Sat", 6: "Sun"}
    return weekdays[adate.weekday()]


def get_holidays_for_calendar(calendar_name, year):
    cal = calendar.Calendar(calendar_name)
    holidays = []
    next_holiday = cal.next_holiday(datetime.date(year - 1, 12, 31))
    holidays.append(next_holiday)
    while next_holiday and next_holiday < datetime.date(year, 12, 31):
        try:
            next_holiday = cal.next_holiday(next_holiday)
            holidays.append(next_holiday)
        except Exception as e:
            logging.info(f"exception was raised {e}")
            break
    return holidays


def get_npdatetime64_business_monthends(start, end, cal):
    recent_monthends = pd.date_range(start, end, freq='M')
    monthends = [np.datetime64(cal.final_businessday_of_month(m)) for m in recent_monthends]
    logging.info(f"Finished getting np.datetime64 business month ends from {start} to {end} based on {cal}")
    return monthends


def get_datetime_business_monthends(start, end, cal):
    recent_monthends = pd.date_range(start, end, freq='M')
    monthends = [cal.final_businessday_of_month(m) for m in recent_monthends]
    logging.info(f"Finished getting datetime business month ends from {start} to {end} based on {cal}")
    return monthends


def get_datetime_business_days(start, end, cal):
    """
    Return business days based on calendar
    :param start:
    :param end:
    :param cal:
    :return:
    """
    cal_days = pd.date_range(start, end)
    bus_days = [d for d in cal_days if cal.is_business_day(d)]
    logging.info(f"Finished getting business days from {start} to {end} based on {cal}")
    return bus_days


def get_datetime_days(start: datetime.date, end: datetime.date):
    """
    Get date range between two dates
    :param start:
    :param end:
    :return:
    """
    return [d for d in pd.date_range(start, end)]


def generate_now_str_timestamp():
    """
    Generate current timestampe in YYYYMMDD_HHMMSS format and return as string
    :return:
    """
    return datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d_%H%M%S')
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?