def fromgregorian(**kw): """Convert gregorian to jalali and return jdatetime.date jdatetime.date.fromgregorian(day=X,month=X,year=X) jdatetime.date.fromgregorian(date=datetime.date) jdatetime.date.fromgregorian(date=datetime.date, locale='fa_IR') """ locale = kw.get('locale') if 'date' in kw: d = kw['date'] try: (y, m, d) = GregorianToJalali(d.year, d.month, d.day).getJalaliList() return date(y, m, d, locale=locale) except AttributeError: raise ValueError( 'When calling fromgregorian(date=) the parameter should be a date like object.' ) if 'day' in kw and 'month' in kw and 'year' in kw: (year, month, day) = (kw['year'], kw['month'], kw['day']) (y, m, d) = GregorianToJalali(year, month, day).getJalaliList() return date(y, m, d, locale=locale) error_msg = ["fromgregorian have to be be called"] error_msg += ["or"] error_msg += ["fromgregorian(day=X,month=X,year=X)"] error_msg += ["fromgregorian(date=datetime.date)"] raise ValueError(" ".join(error_msg))
def fromgregorian(**kw): """Convert gregorian to jalali and return jdatetime.datetime jdatetime.date.fromgregorian(day=X,month=X,year=X,[hour=X, [minute=X, [second=X, [tzinfo=X]]]]) jdatetime.date.fromgregorian(date=datetime.date) jdatetime.date.fromgregorian(datetime=datetime.datetime) jdatetime.date.fromgregorian(datetime=datetime.datetime, locale='fa_IR') """ locale = kw.get('locale') if 'date' in kw and isinstance(kw['date'], py_datetime.date): d = kw['date'] (y, m, d) = GregorianToJalali(d.year, d.month, d.day).getJalaliList() return datetime(y, m, d, locale=locale) if 'datetime' in kw and isinstance(kw['datetime'], py_datetime.datetime): dt = kw['datetime'] (y, m, d) = GregorianToJalali(dt.year, dt.month, dt.day).getJalaliList() return datetime(y, m, d, dt.hour, dt.minute, dt.second, dt.microsecond, dt.tzinfo, locale=locale) if 'day' in kw and 'month' in kw and 'year' in kw: (year, month, day) = (kw['year'], kw['month'], kw['day']) (y, m, d) = GregorianToJalali(year, month, day).getJalaliList() hour = None minute = None second = None microsecond = None tzinfo = None if 'hour' in kw: hour = kw['hour'] if 'minute' in kw: minute = kw['minute'] if 'second' in kw: second = kw['second'] if 'microsecond' in kw: microsecond = kw['microsecond'] if 'tzinfo' in kw: tzinfo = kw['tzinfo'] return datetime(y, m, d, hour, minute, second, microsecond, tzinfo, locale=locale) raise ValueError( "fromgregorian have to called fromgregorian(day=X,month=X,year=X, [hour=X, [minute=X, [second=X, [tzinfo=X]]]]) or fromgregorian(date=datetime.date) or fromgregorian(datetime=datetime.datetime)" )
def fromordinal(ordinal): """int -> date corresponding to a proleptic Jalali ordinal. it starts from Farvardin 1 of year 1, which is equal to 622-3-21 of Gregorian""" if ordinal < 1: raise ValueError("ordinal must be >= 1") d = py_datetime.date.fromordinal(226894 + ordinal) (y, m, d) = GregorianToJalali(d.year, d.month, d.day).getJalaliList() return date(y, m, d)
def today(): """Current date or datetime: same as self.__class__.fromtimestamp(time.time()).""" to = py_datetime.date.today() (y, m, d) = GregorianToJalali(to.year, to.month, to.day).getJalaliList() return date(y, m, d)
def fromgregorian(**kw): """Convert gregorian to jalali and return jdatetime.date jdatetime.date.fromgregorian(day=X,month=X,year=X) jdatetime.date.fromgregorian(date=datetime.date) """ if 'date' in kw and isinstance(kw['date'], py_datetime.date): d = kw['date'] (y, m, d) = GregorianToJalali(d.year, d.month, d.day).getJalaliList() return date(y, m, d) if 'day' in kw and 'month' in kw and 'year' in kw: (year, month, day) = (kw['year'], kw['month'], kw['day']) (y, m, d) = GregorianToJalali(year, month, day).getJalaliList() return date(y, m, d) error_msg = ["fromgregorian have to be be called"] error_msg += ["fromgregorian(day=X,month=X,year=X)"] error_msg += ["or"] error_msg += ["fromgregorian(date=datetime.date)"] raise ValueError(" ".join(error_msg))
def fromtimestamp(timestamp): d = py_datetime.date.fromtimestamp(timestamp) (y, m, d) = GregorianToJalali(d.year, d.month, d.day).getJalaliList() return date(y, m, d)
def fromgregorian(**kw): """Convert gregorian to jalali and return jadatetime.datetime jadatetime.date.fromgregorian(day=X,month=X,year=X,[hour=X, [minute=X, [second=X, [tzinfo=X]]]]) jadatetime.date.fromgregorian(date=datetime.date) jadatetime.date.fromgregorian(datetime=datetime.date) jadatetime.date.fromgregorian(datetime=datetime.datetime) jadatetime.date.fromgregorian(datetime=datetime.datetime, locale='fa_IR') """ locale = kw.get('locale') date_param = kw.get('date') or kw.get('datetime') if date_param: try: (y, m, d) = GregorianToJalali(date_param.year, date_param.month, date_param.day).getJalaliList() except AttributeError: raise ValueError( 'When calling fromgregorian(date=) or fromgregorian(datetime=) the parameter should be date like.' ) try: return datetime(y, m, d, date_param.hour, date_param.minute, date_param.second, date_param.microsecond, date_param.tzinfo, locale=locale) except AttributeError: return datetime(y, m, d, locale=locale) if 'day' in kw and 'month' in kw and 'year' in kw: (year, month, day) = (kw['year'], kw['month'], kw['day']) (y, m, d) = GregorianToJalali(year, month, day).getJalaliList() hour = None minute = None second = None microsecond = None tzinfo = None if 'hour' in kw: hour = kw['hour'] if 'minute' in kw: minute = kw['minute'] if 'second' in kw: second = kw['second'] if 'microsecond' in kw: microsecond = kw['microsecond'] if 'tzinfo' in kw: tzinfo = kw['tzinfo'] return datetime(y, m, d, hour, minute, second, microsecond, tzinfo, locale=locale) raise ValueError( "fromgregorian have to called fromgregorian(day=X,month=X,year=X, [hour=X, [minute=X, [second=X, [tzinfo=X]]]]) or fromgregorian(date=datetime.date) or fromgregorian(datetime=datetime.datetime)" )