Return an aware or naive datetime.datetime, depending on settings.USE_TZ.
167075
def now(tzinfo=True):
    
    if dj_now:
        return dj_now()

    if tzinfo:
        # timeit shows that datetime.now(tz=utc) is 24% slower
        return datetime.utcnow().replace(tzinfo=utc)
    return datetime.now()
1204504
def dtstr_to_datetime(dtstr, to_tz=None, fail_silently=True):
    
    try:
        dt = datetime.datetime.utcfromtimestamp(int(dtstr, 36) / 1e3)
        if to_tz:
            dt = timezone.make_aware(dt, timezone=pytz.UTC)
            if to_tz != pytz.UTC:
                dt = dt.astimezone(to_tz)
        return dt
    except ValueError, e:
        if not fail_silently:
            raise e
        return None
329328
def convert_2_utc(self, datetime_, timezone):
        """"""

        datetime_ = self.tz_mapper[timezone].localize(datetime_)
        return datetime_.astimezone(pytz.UTC)
282977
def now(cls, tzinfo=None):
    """"""
    obj = cls.utcnow()
    if tzinfo is None:
      tzinfo = localtz()
    return obj.astimezone(tzinfo)
393604
def localize(dt, tz):
    
    if not isinstance(tz, tzinfo):
        tz = pytz.timezone(tz)

    return tz.localize(dt)
23519
def _create_timezone_static(tz):
    
    timezone = icalendar.Timezone()
    timezone.add('TZID', tz)
    subcomp = icalendar.TimezoneStandard()
    subcomp.add('TZNAME', tz)
    subcomp.add('DTSTART', dt.datetime(1601, 1, 1))
    subcomp.add('RDATE', dt.datetime(1601, 1, 1))
    subcomp.add('TZOFFSETTO', tz._utcoffset)
    subcomp.add('TZOFFSETFROM', tz._utcoffset)
    timezone.add_component(subcomp)
    return timezone
222521
def _getFromTime(self, atDate=None):
        
        return getLocalTime(self.date, self.time_from, self.tz)
913749
def datetime(self):
        """"""
        return datetime.fromtimestamp(int(self.timestamp) / 1000, tz=pytz.utc)
921025
def utc_to_tz(utc_datetime, tzinfo, keep_tzinfo=False):
    
    tz_awared_datetime = utc_datetime.replace(tzinfo=utc).astimezone(tzinfo)
    if keep_tzinfo is False:
        tz_awared_datetime = tz_awared_datetime.replace(tzinfo=None)
    return tz_awared_datetime
650381
def _tzinfome(tzinfo):
  
  if not isinstance(tzinfo, datetime.tzinfo):
    try:
      tzinfo = pytz.timezone(tzinfo)
      assert tzinfo.zone in pytz.all_timezones
    except AttributeError:
      raise pytz.UnknownTimeZoneError("Unknown timezone! %s" % tzinfo)
  return tzinfo
1204714
def get_windows_tz(iana_tz):
    
    timezone = IANA_TO_WIN.get(
        iana_tz.zone if isinstance(iana_tz, tzinfo) else iana_tz)
    if timezone is None:
        raise pytz.UnknownTimeZoneError(
            "Can't find Iana TimeZone " + iana_tz.zone)

    return timezone
899978
def normalize(self, dt, is_dst=False):
        """"""
        if dt.tzinfo is self:
            return dt
        if dt.tzinfo is None:
            raise ValueError('Naive time - no tzinfo set')
        return dt.astimezone(self)
902065
def to_utc_datetime(self, value):
        
        value = self.to_naive_datetime(value)

        if timezone.is_naive(value):
            value = timezone.make_aware(value, timezone.utc)
        else:
            value = timezone.localtime(value, timezone.utc)
        return value
1374815
def _convert(value, tzto, defaulttz):
    """"""
    if not isinstance(value, datetime):
        raise ValueError('value must be a datetime.datetime object')

    if value.tzinfo is None:
        value = value.replace(tzinfo=defaulttz)

    return value.astimezone(tzto)
537401
def setup_timezone(timezone: str) -> None:
    
    if timezone and hasattr(time, 'tzset'):
        tz_root = '/usr/share/zoneinfo'
        tz_filename = os.path.join(tz_root, *(timezone.split('/')))

        if os.path.exists(tz_root) and not os.path.exists(tz_filename):
            raise ValueError('Incorrect timezone value: {0}'.format(timezone))

        os.environ['TZ'] = timezone
        time.tzset()
412823
def timezone(zone):
    
    try:
        import pytz
        return pytz.timezone(zone)
    except ImportError:
        pass
    try:
        from dateutil.tz import gettz
        return gettz(zone)
    except ImportError:
        return None
470875
def _get_system_tz(self):
        '''
        
        '''
        if not HAS_PYTZ:
            return None

        def _etc_localtime():
            try:
                with open('/etc/localtime', 'rb') as fp:
                    return pytz.tzfile.build_tzinfo('system', fp)
            except OSError as exc:
                if exc.errno != errno.ENOENT:
                    self.logger.error(
                        'Unable to read from /etc/localtime: %s', exc.strerror
                    )
            except pytz.UnknownTimeZoneError:
                self.logger.error(
                    '/etc/localtime contains unrecognized tzinfo'
                )
            return None

        def _etc_timezone():
            try:
                with open('/etc/timezone', 'r') as fp:
                    tzname = fp.read().strip()
                return pytz.timezone(tzname)
            except OSError as exc:
                if exc.errno != errno.ENOENT:
                    self.logger.error(
                        'Unable to read from /etc/localtime: %s', exc.strerror
                    )
            except pytz.UnknownTimeZoneError:
                self.logger.error(
                    '/etc/timezone contains unrecognized timezone \'%s\'',
                    tzname
                )
            return None

        return _etc_localtime() or _etc_timezone()