def __localtime_offset_decreased_jtm(self): # workaround for strftime GMT offset dependecy njtm = self.__jtm.copy() if self.tzinfo is None: njtm.tm_gmtoff = 0 njtm.tm_sec -= jlocaltime(int(_timestamp())).tm_gmtoff njtm.tm_zone = '' normalize_jtm(njtm) return njtm
def datetime_from_ts(ts, local, tz=None): """Return :class:`.datetime` from provided timestamp `ts`. :param int ts: timestamp :param bool local: if True, return local date else return date in UTC :param `datetime.tzinfo` tz: if provided, make timezone aware datetime discarding effect of local parameter """ uts = int(ts % 1 * 1000000) tts = int(ts) if local and tz is None: jtm = jlocaltime(tts) else: if local: return tz.fromutc(datetime.utcfromtimestamp(ts)) jtm = jgmtime(tts) return datetime_from_jtm(jtm, uts, tz)
def strftime(self, format, _decrease_gmtoff_from_secs=False): """Return a string representing the date and time, controlled by an explicit format string. .. Note :: To show correct value for some formatting specials, e.g.'%s', libjalali's :func:`jstrftime` needs timezone informations filled in :attr:`.jtm.tm_gmtoff` (`issue 4`_) which we could not depend on here, since naive datetime objects have zero knowledge about it. Storing these timezone information in a naive datetime object, make datetime implementation heavily depended on :func:`jmktime` which have several issues itself. Additionally it makes :class:`~pyjalali.datetime.datetime` less like :class:`python:datetime.datetime` since it should store more information and change method signatures. For those directives, a possible workaround is to change :attr:`.jtm` by detecting local zone and putting its effect in other field values, but this makes other directives (hour, minute, etc) incorrect. You can use this workaround by passing True as second argument. .. _issue 4: https://github.com/ashkang/jcal/issues/4 """ self.__date._compute_yday_wday_if_necessary() if self.tzinfo is not None: njtm = self.__jtm.copy() njtm.tm_gmtoff = int(self.utcoffset().total_seconds()) njtm.tm_zone = self.tzname() if _decrease_gmtoff_from_secs: njtm.tm_sec += (njtm.tm_gmtoff - jlocaltime(int(_timestamp())).tm_gmtoff) return jstrftime(format, njtm) if _decrease_gmtoff_from_secs: return jstrftime(format, self.__localtime_offset_decreased_jtm()) return jstrftime(format, self.jtm)
def fromtimestamp(self, ts): """Return the local date corresponding to the POSIX timestamp""" jtm = jlocaltime(int(ts)) return date(jtm.tm_year, jtm.tm_mon + 1, jtm.tm_mday)