Exemple #1
0
 def timesForRange(self, start=None, end=None, grain=1):
     """
     returns a list of times where the spec matches within the
     range from start to end, stepped through by grain.
     """
     if start is None:
         start=M.now() + _second_zero
     elif isinstance(start, int):
         start=M.DateTimeFromTicks(start)
     if end is None:
         end=start+M.oneDay
     elif isinstance(end, int):
         end=M.DateTimeFromTicks(end)
     if start > end:
         raise ValueError, "start time %s greater than end time %s" % \
               (start.asctime(), end.asctime())
     if grain<1:
         raise ValueError, "grain must be at least one minute"
     incr=M.RelativeDateTime(minutes=grain)
     times=[]
     while start<=end:
         if self.matchTime(start):
             times.append(start)
         start=start+incr
     return times
Exemple #2
0
def _until_timespec_mx(curticks, hr, min, sec):
    """
    >>> t=time.strptime('200212312359', '%Y%m%d%H%M')
    >>> u=_until_timespec_mx(time.mktime(t), 4, 20, 32)
    >>> time.ctime(u)
    'Wed Jan  1 04:20:32 2003'
    """
    reldate = DateTime.RelativeDateTime(hour=hr, minute=min, second=sec)
    curdate = DateTime.DateTimeFromTicks(curticks)
    huh = curdate + reldate
    if huh <= curdate:
        huh = huh + 1
    return huh.ticks()
Exemple #3
0
 def matchTime(self, dt=None):
     """
     returns a boolean value indicating whether
     the cron specification matches the given time
     (or, if no time is given, the current time)
     """
     if dt is None: dt = M.now()
     elif isinstance(dt, int):
         dt = M.DateTimeFromTicks(dt)
     # otherwise assume it is an mx.DateTime
     (mis, hs, mos, dms, dws) = (self.minutes, self.hours, self.months,
                                 self.days_of_month, self.days_of_week)
     return (mis and dt.minute in mis) \
            and (hs and dt.hour in hs) \
            and (dms and dt.day in dms) \
            and (mos and dt.month in mos) \
            and (dws and dt.day_of_week in dws)
Exemple #4
0
 def load(self, hide_time=None):
     query = self.db.query('bulletins',
                           distinct=True,
                           order_by='post_date DESC')
     if 'ACCESSALL' not in self.credentials.rights:
         query.join('JOIN group_bulletins USING (bulletin_id)')
         query.join('JOIN unit_groups USING (group_id)')
         query.where('unit_groups.unit_id = %s',
                     self.credentials.unit.unit_id)
     if hide_time:
         query.where('bulletins.post_date > %s',
                     DateTime.DateTimeFromTicks(hide_time))
     sub = query.sub_expr('OR')
     sub.where('bulletins.post_date is null')
     sub.where('bulletins.post_date <= CURRENT_TIMESTAMP')
     sub = query.sub_expr('OR')
     sub.where('bulletins.expiry_date is null')
     sub.where('bulletins.expiry_date > CURRENT_TIMESTAMP')
     self.bulletins = query.fetchall()
Exemple #5
0
def convertUntil(until, curdate=None):
    curdate = _to_ticks(curdate)

    if isinstance(until, (int, long)):
        return until

    if isinstance(until, basestring):
        result = _untilString(until, curdate)

    ## if until is a DateTime object, consider it to be in local server
    ## time, and return the ticks.

    elif _is_mx_DateTime(until):
        result = until.ticks()

    ##  elif a RelativeDateTime, add it to the current date
    elif _is_mx_RelativeDateTime(until):
        result = (DateTime.DateTimeFromTicks(curdate) + until).ticks()

    ## elif a dateutil relativedelta, add to current date
    elif _is_dateutil_relativedelta(until):
        t = until + datetime.datetime.fromtimestamp(curdate)
        result = time.mktime(t.timetuple())

    ## elif a list or tuple of items, run convertUntil on each, take
    ## the minimum value in the result list, and return that.
    elif isinstance(until, list) or isinstance(until, tuple):
        lowres = None
        for i in until:
            thisone = convertUntil(i, curdate)
            if lowres is None or thisone < lowres:
                lowres = thisone
        result = lowres

    else:
        raise TimeException, \
              "unsupported type: %s (%s)" % (type(until), until)

    if result < curdate:
        raise PastTimeException, until
    return result
Exemple #6
0
def convertUntil(until, curdate=None):
    curdate = _to_ticks(curdate)

    # if until is a DateTime object, consider it to be in local server
    # time, and return the ticks.
    if _is_mx_DateTime(until):
        result = until.ticks()

    # elif a RelativeDateTime, add it to the current date
    elif _is_mx_RelativeDateTime(until):
        result = (DateTime.DateTimeFromTicks(curdate) + until).ticks()

    # elif a dateutil relativedelta, add to current date
    elif _have_dateutil and isinstance(until, relativedelta):
        result = time.mktime(
            (until + datetime.datetime.fromtimestamp(curdate)).timetuple())

    # elif until is a single string, let untilString handle it.
    elif _is_stringish(until):
        result = untilString(until, curdate)

    # elif a list or tuple of items, run convertUntil on each, take
    # the minimum value in the result list, and return that.
    elif isinstance(until, list) or isinstance(until, tuple):
        lowres = None
        for i in until:
            thisone = convertUntil(i, curdate)
            if lowres is None or thisone < lowres:
                lowres = thisone
        result = lowres

    else:
        raise TimeException, until

    if result < curdate:
        raise PastTimeException, until
    return result
Exemple #7
0
 def _ticks_to_date(self, ticks):
     """ Ticks to timestamp. """
     if ticks is None:
         return None
     return DateTime.DateTimeFromTicks(ticks)
Exemple #8
0
 def timestamp_generator():
     ticks = random.randint(bticks, eticks)
     d = DateTime.DateTimeFromTicks(ticks)
     return d.strftime("%Y-%m-%d %H:%M:%S")
Exemple #9
0
 def next_rowdict(self):
     record = self.lines.next()
     t = record['t']
     record['date'] = DateTime.DateFromTicks(t)
     record['datetime'] = DateTime.DateTimeFromTicks(t)
     return record
Exemple #10
0
def _arpa_mx(timet):
    return DateTime.ARPA.str(DateTime.DateTimeFromTicks(timet))