Пример #1
0
def parse_next_event_from_string(s):
    r = RecurringEvent(now_date=datetime.datetime.now())
    r.parse(s)
    if r.is_recurring:
        rr = rrule.rrulestr(r.get_RFC_rrule())
        return time.mktime(rr.after(datetime.datetime.now()).timetuple())
    else:
        cal = parsedatetime.Calendar()
        if time.mktime(datetime.datetime.now().timetuple()) == time.mktime(
                cal.parse(s)[0]):
            raise Exception("Can't understand this time expression")
        return time.mktime(cal.parse(s)[0])
Пример #2
0
def get_dates(frequency, today, end):
    try:
        return [pd.Timestamp(frequency).normalize()]
    except ValueError:
        pass
    try:
        r = RecurringEvent()
        r.parse(frequency)
        rr = rrule.rrulestr(r.get_RFC_rrule())
        return [
            pd.to_datetime(date).normalize()
            for date in rr.between(today, end)
        ]
    except ValueError as e:
        raise ValueError('Invalid Frequecy')
Пример #3
0
 def next_pickup(self):
     r = RecurringEvent(now_date=datetime.now())
     r.parse(self.pickup_time)
     rr = rrule.rrulestr(r.get_RFC_rrule())
     next_date = rr.after(datetime.now())
     try:
         date_exception = DistrictExceptions.objects.get(date=next_date,
                                                         district=self)
     except:
         date_exception = None
     if date_exception:
         new_date = date_exception.new_date
         if not new_date:
             next_date = rr.after(next_date)
         else:
             next_date = new_date
     return next_date.date()
Пример #4
0
 def next_pickup(self):
     r = RecurringEvent(now_date=datetime.now())
     r.parse(self.pickup_time)
     rr = rrule.rrulestr(r.get_RFC_rrule())
     next_date = rr.after(datetime.now())
     try:
         date_exception = DistrictExceptions.objects.get(date=next_date,
                                                         district=self)
     except:
         date_exception = None
     if date_exception:
         new_date = date_exception.new_date
         if not new_date:
             next_date = rr.after(next_date)
         else:
             next_date = new_date
     return next_date.date()
Пример #5
0
def get_dates(freq, Start, End):
    try:
        return [pd.Timestamp(freq).normalize()]
    except ValueError:
        pass
    try:
        return pd.date_range(start=Start, end=End, freq=freq)
    except ValueError:
        pass
    try:
        r = RecurringEvent()
        r.parse(freq)
        rr = rrule.rrulestr(r.get_RFC_rrule())
        return [
            pd.to_datetime(date).normalize()
            for date in rr.between(Start, End)
        ]
    except ValueError as e:
        raise ValueError('Invalid frequency')
Пример #6
0
    def parse(self, goal_text):
        goal_text = goal_text.rstrip(string.punctuation)

        self.creation_text = goal_text

        index = goal_text.lower().find(self.EVERY)

        if index == -1:
            raise InvalidInput("Could not find the word '%s' in input" % self.EVERY)

        self.description = goal_text[:index].strip()

        try:
            goal_amount = self.incremental_parse(self.description)

            if goal_amount:
                self.incremental = True
                self.goal_amount = goal_amount
        except InvalidInput as i:
            raise i

        recurring_text = goal_text[index:].strip()

        recurring_event = RecurringEvent()

        result = None

        try:
            result = recurring_event.parse(recurring_text)
        except ValueError:
            raise InvalidInput("Not a recurring rule or not valid input")

        if type(result) != type('str'):
            raise InvalidInput("Not a recurring rule or not valid input")

        params = recurring_event.get_params()

        if params['freq'] in ['hourly', 'minutely', 'secondly']:
            raise InvalidInput("Not a recurring rule or not valid input")

        
        if params.has_key('dtstart'):
            naive = datetime.datetime.strptime(params['dtstart'], "%Y%m%d") 

            user_tz = pytz.timezone(self.user.userprofile.timezone)

            local_dt = user_tz.localize(naive, is_dst=None)
            utc_dt = local_dt.astimezone (pytz.utc)
            
            self.dtstart = utc_dt
        else:
            self.dtstart = Goal.beginning_today(self.user)

        self.rrule = recurring_event.get_RFC_rrule()

        if not self.rrule.startswith("DTSTART:"):
            self.rrule = "DTSTART:" + self.dtstart.strftime("%Y%m%d") + "\n" + self.rrule

        if params.get('freq', None):
            self.freq = params['freq']

        if params.get('byday', None):
            self.byday = params['byday']