Genelde şimdi bir cevabı unuttun ama belki benim oluşturduğum sınıfım da faydalı olacaktır. Benim için Pyhon projelerimde sahip olduğum tüm gereksinimleri karşılıyor.
class GetDate:
def __init__(self, date, format="%Y-%m-%d"):
self.tz = pytz.timezone("Europe/Warsaw")
if isinstance(date, str):
date = datetime.strptime(date, format)
self.date = date.astimezone(self.tz)
def time_delta_days(self, days):
return self.date + timedelta(days=days)
def time_delta_hours(self, hours):
return self.date + timedelta(hours=hours)
def time_delta_seconds(self, seconds):
return self.date + timedelta(seconds=seconds)
def get_minimum_time(self):
return datetime.combine(self.date, time.min).astimezone(self.tz)
def get_maximum_time(self):
return datetime.combine(self.date, time.max).astimezone(self.tz)
def get_month_first_day(self):
return datetime(self.date.year, self.date.month, 1).astimezone(self.tz)
def current(self):
return self.date
def get_month_last_day(self):
lastDay = calendar.monthrange(self.date.year, self.date.month)[1]
date = datetime(self.date.year, self.date.month, lastDay)
return datetime.combine(date, time.max).astimezone(self.tz)
Bu nasıl kullanılır
self.tz = pytz.timezone("Europe/Warsaw") - burada projede kullanmak istediğiniz Saat Dilimini tanımlarsınız
GetDate("2019-08-08").current()- Bu, pt 1'de tanımladığınız saat dilimi ile dize tarihinizi zamana duyarlı nesneye dönüştürecektir. Varsayılan dize biçimi, format="%Y-%m-%d"ancak değiştirmekten çekinmeyin. (Örneğin.GetDate("2019-08-08 08:45", format="%Y-%m-%d %H:%M").current() )
GetDate("2019-08-08").get_month_first_day() verilen tarihi (dize veya nesne) ay ilk gün döndürür
GetDate("2019-08-08").get_month_last_day() geçen gün ay verilen gün
GetDate("2019-08-08").minimum_time() belirtilen gün başlangıç tarihi
GetDate("2019-08-08").maximum_time() verilen tarih gün sonu
GetDate("2019-08-08").time_delta_days({number_of_days})verilen tarihle döner + {gün sayısı} ekle ( GetDate(timezone.now()).time_delta_days(-1)dün için de arayabilirsiniz :)
GetDate("2019-08-08").time_delta_haours({number_of_hours}) pt 7'ye benzer ancak saatlerce çalışıyor
GetDate("2019-08-08").time_delta_seconds({number_of_seconds}) pt 7'ye benzer ancak saniyeler içinde çalışıyor
name 'timedelta' is not defined, butimedeltahiçbir yerde tanımlamadığınız anlamına gelir . Python genellikle hata mesajları hakkında oldukça bilgilendiricidir.