def strftime(self, fmt, locale=None): if locale is None or locale not in ["fa", "en"]: locale = self._locale datetime = self.to_gregorian() format_time = { "%H": "%02d" % self._hour, "%I": "%02d" % (self._hour if self._hour <= 12 else self._hour - 12), "%p": "AM" if self._hour < 12 else "PM", "%M": "%02d" % self._minute, "%S": "%02d" % self._second, "%f": "%06d" % self._microsecond, "%z": datetime.strftime("%z"), "%Z": ("" if not self._tzinfo else self._tzinfo.tzname(self)), "%X": "%02d:%02d:%02d" % (self._hour, self._minute, self._second), } if "%c" in fmt: fmt = utils.replace(fmt, {"%c": "%A %d %B %Y %X"}) result = utils.replace(fmt, format_time) result = super().strftime(result, locale) return result
def strftime(self, fmt): datetime = self.to_gregorian() format_time = { "%H": "%02d" % self._hour, "%I": "%02d" % (self._hour if self._hour <= 12 else self._hour - 12), "%p": "AM" if self._hour < 12 else "PM", "%M": "%02d" % self._minute, "%S": "%02d" % self._second, "%f": "%06d" % self._microsecond, "%z": datetime.strftime("%z"), "%Z": ("" if not self._tzinfo else self._tzinfo.tzname(self)), "%X": "%02d:%02d:%02d" % (self._hour, self._minute, self._second), } if "%c" in fmt: fmt = utils.replace(fmt, {"%c": self.strftime("%A %d %B %Y %X")}) result = utils.replace(fmt, format_time) result = super(JalaliDateTime, self).strftime(result, "en") return result
def test_replace(self): self.assertEqual( utils.replace("Persian Tools", { "Persian": "Parsi", " ": "_" }), "Parsi_Tools", ) self.assertEqual( utils.replace("آب بی فلسفه میخوردم", { "آب": "آآآب", " ": "_" }), "آآآب_بی_فلسفه_میخوردم", ) self.assertEqual( utils.replace( "ای بس که نباشیم و جهان خواهد بود", { "ای": "اایی", "خواهد": "خخووااههدد" }, ), "اایی بس که نباشیم و جهان خخووااههدد بود", )
def ar_to_fa(string): """Convert Arabic digits to Persian Usage:: >>> from persiantools import digits >>> converted = digits.ar_to_fa("٠١٢٣٤٥٦٧٨٩") :param string: A string, will be converted :rtype: str """ digits_map = { "٠": "۰", "١": "۱", "٢": "۲", "٣": "۳", "٤": "۴", "٥": "۵", "٦": "۶", "٧": "۷", "٨": "۸", "٩": "۹", } if PY2: if isinstance(string, unicode): digits_map = { unicode(a, "utf8"): unicode(f, "utf8") for a, f in digits_map.iteritems() } return utils.replace(string, digits_map)
def fa_to_en(string): """Convert Persian digits to EN Usage:: >>> from persiantools import digits >>> converted = digits.fa_to_en("۰۱۲۳۴۵۶۷۸۹") :param string: A string, will be converted :rtype: str """ digits_map = { "۰": "0", "۱": "1", "۲": "2", "۳": "3", "۴": "4", "۵": "5", "۶": "6", "۷": "7", "۸": "8", "۹": "9", } if PY2: if isinstance(string, unicode): digits_map = { unicode(f, "utf8"): unicode(e, "utf8") for f, e in digits_map.iteritems() } return utils.replace(string, digits_map)
def en_to_fa(string): """Convert EN digits to Persian Usage:: >>> from persiantools import digits >>> converted = digits.en_to_fa("0123456789") :param string: A string, will be converted :rtype: str """ digits_map = { "0": "۰", "1": "۱", "2": "۲", "3": "۳", "4": "۴", "5": "۵", "6": "۶", "7": "۷", "8": "۸", "9": "۹", } if PY2: if isinstance(string, unicode): digits_map = { unicode(e, "utf8"): unicode(f, "utf8") for e, f in digits_map.iteritems() } return utils.replace(string, digits_map)
def ar_to_fa(string): """Convert Arabic characters to Persian Usage:: from persiantools import characters converted = characters.ar_to_fa("السلام عليكم") :param string: A string, will be converted :rtype: str """ characters_map = { "دِ": "د", "بِ": "ب", "زِ": "ز", "ذِ": "ذ", "شِ": "ش", "سِ": "س", "ى": "ی", "ي": "ی", "ك": "ک", } if PY2: if isinstance(string, unicode): characters_map = { unicode(a, "utf8"): unicode(f, "utf8") for a, f in characters_map.iteritems() } return utils.replace(string, characters_map)
def fa_to_ar(string): """Convert Persian digits to Arabic Usage:: >>> from persiantools import digits >>> converted = digits.fa_to_ar("۰۱۲۳۴۵۶۷۸۹") :param string: A string, will be converted :rtype: str """ digits_map = { "۰": "٠", "۱": "١", "۲": "٢", "۳": "٣", "۴": "٤", "۵": "٥", "۶": "٦", "۷": "٧", "۸": "٨", "۹": "٩", } if PY2: if isinstance(string, unicode): digits_map = { unicode(f, "utf8"): unicode(a, "utf8") for f, a in digits_map.iteritems() } return utils.replace(string, digits_map)
def strftime(self, fmt, locale=None): if locale is None or locale not in ["fa", "en"]: locale = self._locale month_names = MONTH_NAMES_EN if locale == "en" else MONTH_NAMES_FA month_names_abbr = MONTH_NAMES_ABBR_EN if locale == "en" else MONTH_NAMES_ABBR_FA day_names = WEEKDAY_NAMES_EN if locale == "en" else WEEKDAY_NAMES_FA day_names_abbr = WEEKDAY_NAMES_ABBR_EN if locale == "en" else WEEKDAY_NAMES_ABBR_FA am = "AM" if locale == "en" else "ق.ظ" format_time = { "%a": day_names_abbr[self.weekday()], "%A": day_names[self.weekday()], "%w": str(self.weekday()), "%d": "%02d" % self._day, "%b": month_names_abbr[self._month], "%B": month_names[self._month], "%m": "%02d" % self._month, "%y": "%02d" % (self._year % 100), "%Y": "%04d" % self._year, "%H": "00", "%I": "00", "%p": am, "%M": "00", "%S": "00", "%f": "000000", "%z": "", "%Z": "", "%j": "%03d" % (self.days_before_month(self._month) + self._day), "%U": "%02d" % self.week_of_year(), "%W": "%02d" % self.week_of_year(), "%X": "00:00:00", "%%": "%", } if "%c" in fmt: fmt = utils.replace(fmt, {"%c": self.strftime("%A %d %B %Y")}) if "%x" in fmt: fmt = utils.replace(fmt, {"%x": self.strftime("%y/%m/%d")}) result = utils.replace(fmt, format_time) if locale == "fa": result = digits.en_to_fa(result) return result
def fa_to_ar(string: str) -> str: """Convert Persian digits to Arabic Usage:: >>> from persiantools import digits >>> converted = digits.fa_to_ar("۰۱۲۳۴۵۶۷۸۹") :param string: A string, will be converted :rtype: str """ return utils.replace(string, FA_TO_AR_MAP)
def en_to_fa(string: str) -> str: """Convert EN digits to Persian Usage:: >>> from persiantools import digits >>> converted = digits.en_to_fa("0123456789") :param string: A string, will be converted :rtype: str """ return utils.replace(string, EN_TO_FA_MAP)
def ar_to_fa(string: str) -> str: """Convert Arabic digits to Persian Usage:: >>> from persiantools import digits >>> converted = digits.ar_to_fa("٠١٢٣٤٥٦٧٨٩") :param string: A string, will be converted :rtype: str """ return utils.replace(string, AR_TO_FA_MAP)
def fa_to_ar(string: str) -> str: """Convert Persian characters to Arabic Usage:: from persiantools import characters converted = characters.ar_to_fa("ای چرخ فلک خرابی از کینه تست") :param string: A string, will be converted :rtype: str """ characters_map = {"ی": "ي", "ک": "ك"} return utils.replace(string, characters_map)
def strftime(self, fmt): datetime = self.to_gregorian() format_time = { "%H": "%02d" % self._hour, "%I": "%02d" % (self._hour if self._hour <= 12 else self._hour - 12), "%p": "AM" if self._hour < 12 else "PM", "%M": "%02d" % self._minute, "%S": "%02d" % self._second, "%f": "%06d" % self._microsecond, "%z": datetime.strftime("%z"), "%Z": ("" if not self._tzinfo else self._tzinfo.tzname(self)), "%X": "%02d:%02d:%02d" % (self._hour, self._minute, self._second), } if "%c" in fmt: fmt = utils.replace(fmt, {"%c": self.strftime("%A %d %B %Y %X")}) result = utils.replace(fmt, format_time) result = super(JalaliDateTime, self).strftime(result, "en") return result
def fa_to_ar(string): """Convert Persian characters to Arabic Usage:: from persiantools import characters converted = characters.ar_to_fa("ای چرخ فلک خرابی از کینه تست") :param string: A string, will be converted :rtype: str """ dic = { 'ی': 'ي', 'ک': 'ك' } return utils.replace(string, dic)
def ar_to_fa(string): """Convert Arabic characters to Persian Usage:: from persiantools import characters converted = characters.ar_to_fa("السلام عليكم") :param string: A string, will be converted :rtype: str """ dic = { 'ي': 'ی', 'ك': 'ک' } return utils.replace(string, dic)
def fa_to_ar(string): """Convert Persian characters to Arabic Usage:: from persiantools import characters converted = characters.ar_to_fa("ای چرخ فلک خرابی از کینه تست") :param string: A string, will be converted :rtype: str """ characters_map = {"ی": "ي", "ک": "ك"} if PY2: if isinstance(string, unicode): characters_map = { unicode(f, "utf8"): unicode(a, "utf8") for f, a in characters_map.iteritems() } return utils.replace(string, characters_map)
def ar_to_fa(string: str) -> str: """Convert Arabic characters to Persian Usage:: from persiantools import characters converted = characters.ar_to_fa("السلام عليكم") :param string: A string, will be converted :rtype: str """ characters_map = { "دِ": "د", "بِ": "ب", "زِ": "ز", "ذِ": "ذ", "شِ": "ش", "سِ": "س", "ى": "ی", "ي": "ی", "ك": "ک", } return utils.replace(string, characters_map)
def fa_to_ar(string: str) -> str: """Convert Persian digits to Arabic Usage:: >>> from persiantools import digits >>> converted = digits.fa_to_ar("۰۱۲۳۴۵۶۷۸۹") :param string: A string, will be converted :rtype: str """ digits_map = { "۰": "٠", "۱": "١", "۲": "٢", "۳": "٣", "۴": "٤", "۵": "٥", "۶": "٦", "۷": "٧", "۸": "٨", "۹": "٩", } return utils.replace(string, digits_map)
def fa_to_en(string: str) -> str: """Convert Persian digits to EN Usage:: >>> from persiantools import digits >>> converted = digits.fa_to_en("۰۱۲۳۴۵۶۷۸۹") :param string: A string, will be converted :rtype: str """ digits_map = { "۰": "0", "۱": "1", "۲": "2", "۳": "3", "۴": "4", "۵": "5", "۶": "6", "۷": "7", "۸": "8", "۹": "9", } return utils.replace(string, digits_map)
def ar_to_fa(string: str) -> str: """Convert Arabic digits to Persian Usage:: >>> from persiantools import digits >>> converted = digits.ar_to_fa("٠١٢٣٤٥٦٧٨٩") :param string: A string, will be converted :rtype: str """ digits_map = { "٠": "۰", "١": "۱", "٢": "۲", "٣": "۳", "٤": "۴", "٥": "۵", "٦": "۶", "٧": "۷", "٨": "۸", "٩": "۹", } return utils.replace(string, digits_map)
def fa_to_ar(string): """Convert Persian digits to Arabic Usage:: >>> from persiantools import digits >>> converted = digits.fa_to_ar("۰۱۲۳۴۵۶۷۸۹") :param string: A string, will be converted :rtype: str """ dic = { '۰': '٠', '۱': '١', '۲': '٢', '۳': '٣', '۴': '٤', '۵': '٥', '۶': '٦', '۷': '٧', '۸': '٨', '۹': '٩' } return utils.replace(string, dic)
def en_to_fa(string: str) -> str: """Convert EN digits to Persian Usage:: >>> from persiantools import digits >>> converted = digits.en_to_fa("0123456789") :param string: A string, will be converted :rtype: str """ digits_map = { "0": "۰", "1": "۱", "2": "۲", "3": "۳", "4": "۴", "5": "۵", "6": "۶", "7": "۷", "8": "۸", "9": "۹", } return utils.replace(string, digits_map)
def fa_to_en(string): """Convert Persian digits to EN Usage:: >>> from persiantools import digits >>> converted = digits.fa_to_en("۰۱۲۳۴۵۶۷۸۹") :param string: A string, will be converted :rtype: str """ dic = { '۰': '0', '۱': '1', '۲': '2', '۳': '3', '۴': '4', '۵': '5', '۶': '6', '۷': '7', '۸': '8', '۹': '9' } return utils.replace(string, dic)
def en_to_fa(string): """Convert EN digits to Persian Usage:: >>> from persiantools import digits >>> converted = digits.en_to_fa("0123456789") :param string: A string, will be converted :rtype: str """ dic = { '0': '۰', '1': '۱', '2': '۲', '3': '۳', '4': '۴', '5': '۵', '6': '۶', '7': '۷', '8': '۸', '9': '۹' } return utils.replace(string, dic)
def ar_to_fa(string): """Convert Arabic digits to Persian Usage:: >>> from persiantools import digits >>> converted = digits.ar_to_fa("٠١٢٣٤٥٦٧٨٩") :param string: A string, will be converted :rtype: str """ dic = { '٠': '۰', '١': '۱', '٢': '۲', '٣': '۳', '٤': '۴', '٥': '۵', '٦': '۶', '٧': '۷', '٨': '۸', '٩': '۹' } return utils.replace(string, dic)
def strftime(self, fmt, locale=None): if locale is None or locale not in ["fa", "en"]: locale = self._locale month_names = MONTH_NAMES_EN if locale == "en" else MONTH_NAMES_FA month_names_abbr = MONTH_NAMES_ABBR_EN if locale == "en" \ else MONTH_NAMES_ABBR_FA day_names = WEEKDAY_NAMES_EN if locale == "en" \ else WEEKDAY_NAMES_FA day_names_abbr = WEEKDAY_NAMES_ABBR_EN if locale == "en" \ else WEEKDAY_NAMES_ABBR_FA am = "AM" if locale == "en" else "ق.ظ" format_time = { "%a": day_names_abbr[self.weekday()], "%A": day_names[self.weekday()], "%w": str(self.weekday()), "%d": "%02d" % self._day, "%b": month_names_abbr[self._month], "%B": month_names[self._month], "%m": "%02d" % self._month, "%y": "%02d" % (self._year % 100), "%Y": "%04d" % self._year, "%H": "00", "%I": "00", "%p": am, "%M": "00", "%S": "00", "%f": "000000", "%z": "", "%Z": "", "%j": "%03d" % (self.days_before_month(self._month) + self._day), "%U": "%02d" % self.week_of_year(), "%W": "%02d" % self.week_of_year(), "%X": "00:00:00", "%%": "%", } if "%c" in fmt: fmt = utils.replace(fmt, {"%c": self.strftime("%A %d %B %Y")}) if "%x" in fmt: fmt = utils.replace(fmt, {"%x": self.strftime("%y/%m/%d")}) result = utils.replace(fmt, format_time) if locale == "fa": result = digits.en_to_fa(result) return result
def test_replace(self): self.assertEqual(utils.replace("Persian Tools", {"Persian": "Parsi", " ": "_"}), "Parsi_Tools") self.assertEqual(utils.replace("آب بی فلسفه میخوردم", {"آب": "آآآب", " ": "_"}), "آآآب_بی_فلسفه_میخوردم")