Esempio n. 1
0
def get_date_last_saturday():

    ### import with conventional class notation
    from datetime import timedelta as Timedelta
    from datetime import datetime as Datetime
    from dateutil import relativedelta

    today = Datetime.now()

    if today.weekday() == 5:
        weeks_back_in_time = 0  ### If today is saturday. we want "last saturday" to adtop today's value

    else:
        weeks_back_in_time = -1

    print('weeks_back_in_time: {}'.format(weeks_back_in_time))
    start = today - Timedelta((today.weekday() + 1) % 7)

    dtm_last_saturday = start + relativedelta.relativedelta(
        weekday=relativedelta.SA(weeks_back_in_time))
    dt_last_saturday = dtm_last_saturday.date()
    str_last_saturday = dt_last_saturday.strftime(format="%Y-%m-%d")
    print('Last Saturday was {}'.format(str_last_saturday))

    return str_last_saturday, dt_last_saturday
Esempio n. 2
0
 def test_next_weekly_digest_existing_digest(self):
     "The next digest date is correctly generated for a weekly digest with a last published date in the past"
     today_mon = date(year=2012, month=02,
                      day=01) + rd.relativedelta(weekday=rd.MO)
     last_sat = today_mon + rd.relativedelta(weekday=rd.SA(-1))
     digestobj, created = dh.get_create_digest_feed(self.get_user(),
                                                    self.feedobj, 'weekly',
                                                    'sat')
     digestobj.date_last_digest = last_sat
     digestobj.save()
     date_next_digest = dh.date_next_digest(digestobj, today=today_mon)
     this_sat = today_mon + rd.relativedelta(weekday=rd.SA)
     self.assertEqual(date_next_digest, this_sat)
Esempio n. 3
0
 def setUp(self):
     # create 28 entries over 14 days
     period = 14
     self.today = dt.today() + rd.relativedelta(weekday=rd.SA(-1))
     self.start_dt = self.today - timedelta(days=period)
     self.end_dt = self.today
     cbit = self.custom_feed_url(period * 2, self.start_dt,
                                 self.end_dt).lstrip('/')
     res = logic.pull_feed("http://localhost:8000/endtimes/%s" % cbit,
                           user=self.get_user(),
                           create_entries=True)
     feedobj = models.Feed.objects.get(pk=1)
     self.digestobj, created = dh.get_create_digest_feed(
         self.get_user(), feedobj, 'weekly', 'sat')
def meetup_day(year, month, targetday, targetweek):

    weekdays = dict(zip(list(calendar.day_name), range(7)))
    steps = dict(zip(['1st', '2nd', '3rd', '4th', 'last'], range(7, 36, 7)))
    steps['teenth'] = 19
    targetday = weekdays[targetday]
    getDay = {
        0: relativedelta.MO(-1),
        1: relativedelta.TU(-1),
        2: relativedelta.WE(-1),
        3: relativedelta.TH(-1),
        4: relativedelta.FR(-1),
        5: relativedelta.SA(-1),
        6: relativedelta.SU(-1),
    }

    d = datetime.date(year, month, 1) + relativedelta.relativedelta(
        day=steps[targetweek], weekday=getDay[targetday])
    return d
Esempio n. 5
0
def all_weekdays(year, month, week, day_of_week):
    dt = date(year, month, 1)

    if week == 'last':
        clean_week = int(-1)
        if month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12:
            day_offset = 31
        elif month == 2:
            day_offset = 28
        else:
            day_offset = 30
    else:
        clean_week = +int(week.strip("stndrh"))
        day_offset = 0

    if day_of_week == 'Monday':
        the_date = dt + rdelta.relativedelta(days=day_offset,
                                             weekday=rdelta.MO(clean_week))
        return the_date
    elif day_of_week == 'Tuesday':
        the_date = dt + rdelta.relativedelta(days=day_offset,
                                             weekday=rdelta.TU(clean_week))
        return the_date
    elif day_of_week == 'Wednesday':
        the_date = dt + rdelta.relativedelta(days=day_offset,
                                             weekday=rdelta.WE(clean_week))
        return the_date
    elif day_of_week == 'Thursday':
        the_date = dt + rdelta.relativedelta(days=day_offset,
                                             weekday=rdelta.TH(clean_week))
        return the_date
    elif day_of_week == 'Friday':
        the_date = dt + rdelta.relativedelta(days=day_offset,
                                             weekday=rdelta.FR(clean_week))
        return the_date
    elif day_of_week == 'Saturday':
        the_date = dt + rdelta.relativedelta(days=day_offset,
                                             weekday=rdelta.SA(clean_week))
        return the_date
    elif day_of_week == 'Sunday':
        the_date = dt + rdelta.relativedelta(days=day_offset,
                                             weekday=rdelta.SU(clean_week))
        return the_date
Esempio n. 6
0
    def _last_interval(self):

        dr = self.date_range

        if 'days' in dr:
            __, num, interval = dr.split()
            end = end_of_day(self.pivot)
            start = start_of_day(end + timezone.timedelta(days=-int(num)))
        elif 'week' in dr:
            start = start_of_day(
                self.pivot) + rdelta.relativedelta(weekday=rdelta.SU(-2))
            end = end_of_day(self.pivot) + rdelta.relativedelta(
                weeks=-1, weekday=rdelta.SA(1))
        elif 'month' in dr:
            start = start_of_day(self.pivot) + rdelta.relativedelta(months=-1,
                                                                    day=1)
            end = end_of_day(self.pivot) + rdelta.relativedelta(months=-1,
                                                                day=31)
        elif 'year' in dr:
            start = start_of_day(self.pivot) + rdelta.relativedelta(
                years=-1, month=1, day=1)
            end = end_of_day(self.pivot) + rdelta.relativedelta(
                years=-1, month=12, day=31)
        return start, end
Esempio n. 7
0
web_path = "https://www.antiochca.gov/fc/police/crime-maps/"
# domain = https://www.antiochca.gov


cur_dir = os.getcwd()
save_dir = cur_dir + "/data/arrests/"

if not os.path.exists(save_dir):
    os.makedirs(save_dir)

html_page = requests.get(webpage).text


today = datetime.datetime.now()
start = today - datetime.timedelta((today.weekday() + 1) % 7)
sat = start + relativedelta.relativedelta(weekday=relativedelta.SA(-1))
sun = sat + relativedelta.relativedelta(weekday=relativedelta.SU(-1))
# Gave up on trying to use strptime to strip the time
sat_date = str(sat).replace("-", "_").split()
sun_date = str(sun).replace("-", "_").split()

file_name = "arrest_reports_" + str(sun_date[0]) + "_" + str(sat_date[0])
print(file_name)


def get_files():
    # file_name = line_list[1].replace(" ", "_")[:-1]
    # file_name = save_dir + file_name
    # document = requests.get(url_2, allow_redirects=True)
    pdf = urllib.request.urlopen(webpage)
    with open(save_dir + file_name + ".pdf", "wb") as file:
Esempio n. 8
0
def following_saturday(dt):
    rd = reldate.relativedelta(weekday=reldate.SA(+1), hours=+8)
    rd2 = reldate.relativedelta(hour=8, minute=0, second=0, microsecond=0)
    return dt + rd + rd2