Created
October 22, 2021 14:48
-
-
Save pixelead0/e0c24e4e3a58790fafb5a4ba8f73915e to your computer and use it in GitHub Desktop.
Funciones utiles para trabajar con fechas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import logging | |
logger = logging.getLogger(__name__) | |
class DatesHelper(object): | |
def convert_string_to_datetime(self, date=datetime.datetime.now()): | |
if type(date) == str or type(date) == unicode: | |
date = datetime.datetime.strptime(date, "%Y-%m-%d") | |
return date | |
def convert_to_initial_datetime(self, date=datetime.datetime.now()): | |
""" | |
Convierte una fecha a la misma fecha con la primer hora del día. | |
""" | |
return datetime.datetime(date.year, date.month, date.day, 0, 0, 0, 0) | |
def convert_to_end_datetime(self, date=datetime.datetime.now()): | |
""" | |
Convierte una fecha a la misma fecha con la ultima hora del día. | |
Se va al siguiente día y resta un microsegudo | |
""" | |
from dateutil.relativedelta import relativedelta | |
current_day = self.convert_to_initial_datetime(date=date) | |
return current_day + relativedelta(days=1, microseconds=-1) | |
def get_first_day_of_month(self, date=datetime.datetime.now()): | |
return datetime.datetime(date.year, date.month, 1, 0, 0, 0) | |
def get_last_day_of_month(self, date=datetime.datetime.now()): | |
import calendar | |
last_day = calendar.monthrange(date.year, date.month)[1] | |
return datetime.datetime(date.year, date.month, last_day, 0, 0, 0) | |
def calculate_months(self, date=datetime.datetime.now(), months=1): | |
from dateutil.relativedelta import relativedelta | |
return self.get_first_day_of_month(date) + relativedelta(months=months) | |
def get_current_year(self, date=datetime.datetime.now()): | |
import calendar | |
last_day = calendar.monthrange(date.year, date.month)[1] | |
return datetime.datetime(date.year, 1, 1, 0, 0, 0) | |
def calculate_years(self, date=datetime.datetime.now(), years=-1): | |
from dateutil.relativedelta import relativedelta | |
return self.get_current_year(date) + relativedelta(years=years) | |
def generate_range_dates_list(self, date_start=None, date_end=None): | |
if not date_start: | |
date_start = self.get_first_day_of_month() | |
if not date_end: | |
date_end = self.get_last_day_of_month() | |
current_date = date_start | |
dateList = [] | |
#logger.info("start: %s end:%s" % (date_start, date_end)) | |
while (current_date <= date_end): | |
p_date = current_date | |
dateList.append(current_date) | |
current_date = (current_date + datetime.timedelta(days=1)) | |
return dateList | |
def check_leap_year(self, date_to_check): | |
import calendar | |
day_of_year = date_to_check.timetuple().tm_yday | |
# 60 = dia 29/feb (año bisiesto) | |
if calendar.isleap(date_to_check.year) and day_of_year >= 60: | |
day_of_year -= 1 | |
return (day_of_year) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment