Get next valid day for timerange

        :param timestamp: time we compute from
        :type timestamp: int
        :return: timestamp of the next valid day (midnight) in LOCAL time.
        :rtype: int | None
182423
def get_next_valid_day(self, timestamp):
        
        if self.get_next_future_timerange_valid(timestamp) is None:
            # this day is finish, we check for next period
            (start_time, _) = self.get_start_and_end_time(get_day(timestamp) + 86400)
        else:
            (start_time, _) = self.get_start_and_end_time(timestamp)

        if timestamp <= start_time:
            return get_day(start_time)

        if self.is_time_day_valid(timestamp):
            return get_day(timestamp)

        return None
94571
def timestamp_to_days(self, timestamp):
        ''''''
        if self.tday_base is None:
            try:
                self.tday_base = matplotlib.dates.date2num(datetime.datetime.fromtimestamp(timestamp+self.timeshift))
                self.tday_basetime = timestamp
            except ValueError:
                # this can happen if the log is corrupt
                # ValueError: year is out of range
                return 0
        sec_to_days = 1.0 / (60*60*24)
        return self.tday_base + (timestamp - self.tday_basetime) * sec_to_days
1196503
def next_working_day(count = 1)
      return self if count == 0
      negative = count < 0
      count = count.abs
      date = negative ? yesterday : tomorrow
      
      loop do
        count -= 1 if date.working_day?
        return date if count.zero? 
        
        date += (negative ? -1 : 1).day
      end
    end
857386
def fromtimestamp(timestamp):
    
    if timestamp >= 0:
        return datetime.fromtimestamp(timestamp)
    else:
        return datetime(1969, 12, 31, 20, 0) + timedelta(seconds=timestamp)
507246
def _dfromtimestamp(timestamp):
    
    try:
        return datetime.date.fromtimestamp(timestamp)
    except OSError:
        timestamp -= time.timezone
        d = datetime.date(1970, 1, 1) + datetime.timedelta(seconds=timestamp)
        if _isdst(d):
            timestamp += 3600
            d = datetime.date(1970, 1, 1) + datetime.timedelta(seconds=timestamp)
        return d
265281
def format_date(cls, timestamp):
        
        if not timestamp:
            raise DateTimeFormatterException('timestamp must a valid string {}'.format(timestamp))

        return timestamp.strftime(cls.DATE_FORMAT)
831180
def get_next_future_timerange_invalid(self, timestamp):
        
        sec_from_morning = get_sec_from_morning(timestamp)
        ends = []
        for timerange in self.timeranges:
            tr_end = timerange.hend * 3600 + timerange.mend * 60
            if tr_end >= sec_from_morning:
                # Remove the last second of the day for 00->24h"
                if tr_end == 86400:
                    tr_end = 86399
                ends.append(tr_end)
        if ends != []:
            return min(ends)

        return None
385569
def timestamp_normalized(self):
        
        if not self.timestamp:
            return None

        if isinstance(self.timestamp, six.integer_types):
            return self.timestamp

        if isinstance(self.timestamp, timedelta):
            tmp = datetime.now() + self.timestamp
        else:
            tmp = self.timestamp

        return int(time.mktime(tmp.timetuple()) * 1e+6 + tmp.microsecond)
34728
def is_valid_timestamp(date, unit='millis'):
    
    assert isinstance(date, int), "Input is not instance of int"

    if unit is 'millis':
        return is_positive(date) and len(str(date)) == 13
    elif unit is 'seconds':
        return is_positive(date) and len(str(date)) == 10
    else:
        raise ValueError('Unknown unit "%s"' % unit)
1366841
def from_timestamp(
    timestamp, tz=UTC  # type: Union[int, float]  # type: Union[str, _Timezone]
):  # type: (...) -> DateTime
    
    dt = _datetime.datetime.utcfromtimestamp(timestamp)

    dt = datetime(
        dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.microsecond
    )

    if tz is not UTC or tz != "UTC":
        dt = dt.in_timezone(tz)

    return dt
1359667
def timestamp(self):
        ""
        if self._tzinfo is None:
            return _time.mktime((self.year, self.month, self.day,
                                 self.hour, self.minute, self.second,
                                 -1, -1, -1)) + self.microsecond / 1e6
        else:
            return (self - _EPOCH).total_seconds()
410884
def timestamp(self, name, value):
        
        counter = self.get(name, CounterType.Timestamp)
        counter.time = value if value != None else datetime.datetime.utcnow()
        self._update()
496797
def ttl(self, steps, relative_time=None):
    '''
    
    '''
    if steps:
      # Approximate the ttl based on number of seconds, since it's
      # "close enough"
      if relative_time:
        rtime = self.to_bucket(relative_time)
        ntime = self.to_bucket(time.time())

        # Convert to number of days
        day_diff = (self.from_bucket(ntime, native=True) - self.from_bucket(rtime, native=True)).days
        # Convert steps to number of days as well
        step_diff = (steps*SIMPLE_TIMES[self._step[0]]) / SIMPLE_TIMES['d']

        # The relative time is beyond our TTL cutoff
        if day_diff > step_diff:
          return 0
        # The relative time is in "recent" past or future
        else:
          return (step_diff - day_diff) * SIMPLE_TIMES['d']
      return steps * SIMPLE_TIMES[ self._step[0] ]

    return None
603914
def timestamp_from_datetime(dt):
    
    dt = dt.replace(tzinfo=utc)
    if hasattr(dt, "timestamp") and callable(dt.timestamp):
        return dt.replace(tzinfo=utc).timestamp()
    return (dt - datetime(1970, 1, 1, tzinfo=utc)).total_seconds()
579784
def QA_util_stamp2datetime(timestamp):
    
    try:
        return datetime.datetime.fromtimestamp(timestamp)
    except Exception as e:
        # it won't work ??
        try:
            return datetime.datetime.fromtimestamp(timestamp / 1000)
        except:
            try:
                return datetime.datetime.fromtimestamp(timestamp / 1000000)
            except:
                return datetime.datetime.fromtimestamp(timestamp / 1000000000)
740836
def get_timestamp(str_len=13):
    
    if isinstance(str_len, integer_types) and 0 < str_len < 17:
        return builtin_str(time.time()).replace(".", "")[:str_len]

    raise ParamsError("timestamp length can only between 0 and 16.")
713388
def get_timedelta(self, now=None):
        
        def datetime_to_time(timestamp):
            atime = time.mktime(timestamp.timetuple())
            atime += timestamp.microsecond / 10.0**6
            return atime
        if self.started is not None:
            now = now or datetime.datetime.now()
            started_time = datetime_to_time(self.started)
            now_time = datetime_to_time(now)
            return now_time - started_time
        return None