Example #1
0
    def test_parse_line_date(self):

        data = '2015-06-23   aaa\n2044-12-29   bbb'
        date = Date(13, 'date')
        str = String(3, 'str')
        definition = (date, str)

        expected1 = [
            {'key':'date', 'value':datetime_date(2015, 6, 23)},
            {'key':'str', 'value':'aaa'},
        ]

        expected2 = [
            {'key':'date', 'value':datetime_date(2044, 12, 29)},
            {'key':'str', 'value':'bbb'},
        ]

        fw = FixedWidth(definition, data)
        elems = list(fw)

        actual1 = elems[0]
        actual2 = elems[1]

        self.compare_line(expected1, actual1)
        self.compare_line(expected2, actual2)
Example #2
0
def yearmonth_to_date(x, day=1):
    if type(x) is int:
        year = x // 100
        month = x % 100
    elif type(x) is str:
        x = str(x)
        year = int(re.findall(pattern='^\d+', string=x)[0][:4])
        month = int(re.findall(pattern='\d+$', string=x)[0][-2:])
    elif type(x) is YearMonth:
        year = x.year.value
        month = x.month.value
    else:
        return None

    try:
        result = datetime_date(year=year, month=month, day=day)
    except:
        # There is an exception if the day of the month we're in does not exist in the target month
        # Go to the FIRST of the month AFTER, then go back one day.
        first_day_of_the_month = datetime_date(year=year, month=month, day=1)
        first_day_of_next_month = add_months(date=first_day_of_the_month,
                                             months=1)
        last_day_of_the_month = add_days(date=first_day_of_next_month, days=-1)
        result = last_day_of_the_month

    return result
Example #3
0
    def test_parse_line_date_custom_format(self):

        class MyDate(Date):
            parse_kwargs = {
                'date_formats': ['%y%m---%d']
            }

        data = '9901---03aaa\n6612---23bbb'
        date = MyDate(9, 'date')
        str = String(3, 'str')
        definition = (date, str)

        expected1 = [
            {'key':'date', 'value':datetime_date(1999, 1, 3)},
            {'key':'str', 'value':'aaa'},
        ]

        expected2 = [
            {'key':'date', 'value':datetime_date(2066, 12, 23)},
            {'key':'str', 'value':'bbb'},
        ]

        fw = FixedWidth(definition, data)
        elems = list(fw)

        actual1 = elems[0]
        actual2 = elems[1]

        self.compare_line(expected1, actual1)
        self.compare_line(expected2, actual2)
Example #4
0
 def populate_db(self):
     """ Populates the database """
     b1 = bookings.Booking(user=1, date=datetime_date(2019, 11, 1), movie=1)
     b2 = bookings.Booking(user=2, date=datetime_date(2019, 11, 2), movie=2)
     b3 = bookings.Booking(user=3, date=datetime_date(2019, 11, 3), movie=3)
     bookings.db.session.add(b1)
     bookings.db.session.add(b2)
     bookings.db.session.add(b3)
     bookings.db.session.commit()
Example #5
0
    def populate_db(self):
        """ Populates the database """
        s1 = showtimes.Showtime(date=datetime_date(2019, 11, 1), movie=1)
        s2 = showtimes.Showtime(date=datetime_date(2019, 11, 2), movie=2)
        s3 = showtimes.Showtime(date=datetime_date(2019, 11, 3), movie=3)

        showtimes.db.session.add(s1)
        showtimes.db.session.add(s2)
        showtimes.db.session.add(s3)
        showtimes.db.session.commit()
Example #6
0
def _calc_julian_from_V(iso_year, iso_week, iso_weekday):
    """Calculate the Julian day based on the ISO 8601 year, week, and weekday.
    ISO weeks start on Mondays, with week 01 being the week containing 4 Jan.
    ISO week days range from 1 (Monday) to 7 (Sunday).
    """
    correction = datetime_date(iso_year, 1, 4).isoweekday() + 3
    ordinal = iso_week * 7 + iso_weekday - correction
    if ordinal < 1:
        ordinal += datetime_date(iso_year, 1, 1).toordinal()
        iso_year -= 1
        ordinal -= datetime_date(iso_year, 1, 1).toordinal()
    return (iso_year, ordinal)
Example #7
0
    def test_serialize_line_date_from_date_input_with_output_format(self):

        a = Date(12, 'a', '%m-%d-%y')
        b = Date(12, 'b', '%m-%d-%y')
        definition = (a, b)

        response = Bunch()
        response.a = datetime_date(2017, 1, 25)
        response.b = datetime_date(2018, 2, 26)

        fw = FixedWidth(definition)
        result = fw.serialize(response)
        self.assertEquals(result, '01-25-17    02-26-18    ')
Example #8
0
    def test_serialize_line_date_from_date_input(self):

        a = Date(16, 'a')
        b = Date(16, 'b')
        definition = (a, b)

        response = Bunch()
        response.a = datetime_date(2017, 1, 25)
        response.b = datetime_date(2018, 2, 26)

        fw = FixedWidth(definition)
        result = fw.serialize(response)
        self.assertEquals(result, '2017-01-25      2018-02-26      ')
Example #9
0
 def _compute_date_from_to(self, date):
     self.ensure_one()
     date_from = date_to = fields.Date.from_string(date)
     if self.range_reset == 'weekly':
         date_from = date_from - timedelta(days=date_from.weekday())
         date_to = date_from + timedelta(days=6)
     elif self.range_reset == 'monthly':
         date_from = datetime_date(date_from.year, date_from.month, 1)
         date_to = date_from + relativedelta(months=1)
         date_to += relativedelta(days=-1)
     elif self.range_reset == 'yearly':
         date_from = datetime_date(date_from.year, 1, 1)
         date_to = datetime_date(date_from.year, 12, 31)
     return date_from.strftime('%Y-%m-%d'), date_to.strftime('%Y-%m-%d')
Example #10
0
 def _compute_date_from_to(self, date):
     self.ensure_one()
     date_from = date_to = date
     if self.range_reset == "weekly":
         date_from = date_from - timedelta(days=date_from.weekday())
         date_to = date_from + timedelta(days=6)
     elif self.range_reset == "monthly":
         date_from = datetime_date(date_from.year, date_from.month, 1)
         date_to = date_from + relativedelta(months=1)
         date_to += relativedelta(days=-1)
     elif self.range_reset == "yearly":
         date_from = datetime_date(date_from.year, 1, 1)
         date_to = datetime_date(date_from.year, 12, 31)
     return date_from, date_to
Example #11
0
def _calc_julian_from_V(iso_year, iso_week, iso_weekday):
    """Calculate the Julian day based on the ISO 8601 year, week, and weekday.
    ISO weeks start on Mondays, with week 01 being the week containing 4 Jan.
    ISO week days range from 1 (Monday) to 7 (Sunday).
    """
    correction = datetime_date(iso_year, 1, 4).isoweekday() + 3
    ordinal = (iso_week * 7) + iso_weekday - correction
    # ordinal may be negative or 0 now, which means the date is in the previous
    # calendar year
    if ordinal < 1:
        ordinal += datetime_date(iso_year, 1, 1).toordinal()
        iso_year -= 1
        ordinal -= datetime_date(iso_year, 1, 1).toordinal()
    return iso_year, ordinal
Example #12
0
def _calc_julian_from_V(iso_year, iso_week, iso_weekday):
    """Calculate the Julian day based on the ISO 8601 year, week, and weekday.
    ISO weeks start on Mondays, with week 01 being the week containing 4 Jan.
    ISO week days range from 1 (Monday) to 7 (Sunday).
    """
    correction = datetime_date(iso_year, 1, 4).isoweekday() + 3
    ordinal = (iso_week * 7) + iso_weekday - correction
    # ordinal may be negative or 0 now, which means the date is in the previous
    # calendar year
    if ordinal < 1:
        ordinal += datetime_date(iso_year, 1, 1).toordinal()
        iso_year -= 1
        ordinal -= datetime_date(iso_year, 1, 1).toordinal()
    return iso_year, ordinal
def read_c_timeseries(directory, stocks="All", days="All"):

    stock_dic = {}
    num_of_days = 0
    all_dates = []

    cur_dir = getcwd()
    chdir(directory)

    # Reading data from files - Keeping dates and close value
    for fname in glob("*.txt"):
        num_of_stocks = 0
        if days == "All" or num_of_days < days:
            file_iter = open(fname, "rU")
            for line in file_iter:
                if stocks == "All" or num_of_stocks < stocks:
                    temp = line.strip().rstrip(" ").split(",")
                    date = datetime_date(int(temp[0][0:4]), int(temp[0][4:6]), int(temp[0][6:8]))

                    if temp[1] not in stock_dic:
                        stock_dic[temp[1]] = [{"Date": date, "Close": float(temp[5])}]
                    else:
                        stock_dic[temp[1]].append({"Date": date, "Close": float(temp[5])})
                else:
                    break
                num_of_stocks += 1
            all_dates.append(date)
            file_iter.close()
            num_of_days += 1
    chdir(cur_dir)
    return (stock_dic, all_dates)
def read_p_timeseries(directory, name, days="All"):

    stock_dic = {}
    num_of_days = 0

    cur_dir = getcwd()
    chdir(directory)

    # Reading data from files - Keeping dates and close value
    for fname in glob("*.txt"):
        if days == "All" or num_of_days <= days - 1:
            file_iter = open(fname, "rU")
            for line in file_iter:
                temp = line.strip().rstrip(" ").split(",")
                if temp[1] == name:
                    date = datetime_date(int(temp[0][0:4]), int(temp[0][4:6]), int(temp[0][6:8]))

                    if temp[1] not in stock_dic:
                        stock_dic[temp[1]] = [{"Date": date, "Close": float(temp[5])}]
                    else:
                        stock_dic[temp[1]].append({"Date": date, "Close": float(temp[5])})
            file_iter.close()
            num_of_days += 1

    chdir(cur_dir)
    return stock_dic
Example #15
0
def read_p_timeseries(directory, name, days='All'):

    stock_dic = {}
    num_of_days = 0

    cur_dir = getcwd()
    chdir(directory)

    # Reading data from files - Keeping dates and close value
    for fname in glob("*.txt"):
        if days == 'All' or num_of_days <= days - 1:
            file_iter = open(fname, 'rU')
            for line in file_iter:
                temp = line.strip().rstrip(' ').split(',')
                if temp[1] == name:
                    date = datetime_date(int(temp[0][0:4]), int(temp[0][4:6]),
                                         int(temp[0][6:8]))

                    if temp[1] not in stock_dic:
                        stock_dic[temp[1]] = [{
                            "Date": date,
                            "Close": float(temp[5])
                        }]
                    else:
                        stock_dic[temp[1]].append({
                            "Date": date,
                            "Close": float(temp[5])
                        })
            file_iter.close()
            num_of_days += 1

    chdir(cur_dir)
    return (stock_dic)
def run():
    url = "https://open.senrigan.io/api/betterself/v2/productivity_logs/"

    df = pd.read_excel(
        "snapshots/personal_historical_pomodoros.xlsx", keep_default_na=""
    )
    token_key = f"Token {settings.BETTERSELF_PERSONAL_API_KEY}"
    headers = {"Authorization": token_key}

    for index, row in df.iterrows():
        date = row["Date"].date().isoformat()

        # interim when it failed on a na/string
        last_progress = datetime_date(2020, 5, 21)
        if row["Date"].date() > last_progress:
            continue

        notes = row["Description Of Day"]
        mistakes = row["Mistakes"]
        pomodoro_count = row["Pomodoros"]

        if not pomodoro_count:
            continue

        data = {
            "pomodoro_count": pomodoro_count,
            "date": date,
            "notes": notes,
            "mistakes": mistakes,
        }

        response = requests.post(url, json=data, headers=headers)
        if response.status_code != 200:
            print(response.content)
            assert response.status_code == 200
Example #17
0
def generate_year_folder_id(service, file_name):
    month, day, year = file_name
    date_beginning = datetime_date(year, 8, 1)
    league_date = datetime_date(year, month, day)

    if league_date < date_beginning:
        folder_name = '{}-{}'.format(year - 1, year)
    else:
        folder_name = '{}-{}'.format(year, year + 1)

    file_metadata = {
        'parents': [RESULTS_FOLDER_ID],
        'name': folder_name,
        'mimeType': 'application/vnd.google-apps.folder'
    }
    file = service.files().create(body=file_metadata, fields='id').execute()
    return file['id']
Example #18
0
    def __init__(self, *args, url: str, date: str, sheet_id: str, **kwargs):
        super().__init__(*args, **kwargs)

        self.page_url = url
        self.sheet_id = sheet_id

        day, month, year = map(int, date.split('.'))
        self.date = datetime_date(year=year, month=month, day=day)
Example #19
0
    def get(self, *args, **kwargs):
        date = kwargs.get('date')
        clan = Clan.objects.get(pk=int(self.request.GET.get('clan_id', 35039)))
        force_update = self.request.GET.get('force_update') == 'true'

        if force_update:
            from global_map.management.commands.fetchdata import update_clan
            update_clan(clan.id)

        pa_query = ProvinceAssault.objects.distinct('province').order_by(
            'province')
        if date:
            date = datetime_date(*[int(i) for i in date.split('-')])
            assaults = [
                assault.as_clan_json(clan, current_only=False)
                for assault in pa_query.filter(date=date).filter(
                    Q(battles__clan_a=clan) | Q(battles__clan_b=clan)
                    | Q(clans=clan) | Q(current_owner=clan))
            ]
        else:
            date = datetime.now().date()
            assaults = [
                assault.as_clan_json(clan) for assault in pa_query.filter(
                    date=date).filter(Q(clans=clan) | Q(current_owner=clan))
            ]

        # Remove assaults without battles
        for assault in assaults[::]:
            if not assault['battles']:
                logger.debug("Removing assault on province %s",
                             assault['province_info']['province_id'])
                assaults.remove(assault)

        times = [
            battle['planned_start_at'] for assault in assaults
            for battle in assault['battles']
        ]

        if times:
            min_time = min(times)
            min_time = min_time.replace(minute=min_time.minute -
                                        min_time.minute % 30)
            max_time = max(times)
        else:
            min_time = datetime.now().replace(hour=15, minute=00)
            max_time = datetime.now().replace(hour=2,
                                              minute=00) + timedelta(days=1)

        return JsonResponse({
            'time_range': [min_time, max_time],
            'assaults':
            sorted(
                assaults,
                key=lambda v:
                (v['province_info']['prime_time'].minute == 15, v['battles'][0]
                 ['planned_start_at'], v['province_info']['province_id'])),
        })
Example #20
0
def determine_year_folder_id(service, file_name):
    results = service.files().list(
        q="'{}' in parents".format(RESULTS_FOLDER_ID),
        fields="nextPageToken, files(id, name)").execute()
    year_folders = results.get('files', [])
    if year_folders:
        month, day, year = file_name
        for folder in year_folders:
            split_file_name = shared_functions.file_name_split(
                folder['name'].strip())
            if [element for element in split_file_name if element.isdigit()]:
                year_beginning, year_end = tuple(split_file_name)
                date_beginning = datetime_date(int(year_beginning), 8, 1)
                date_end = datetime_date(int(year_end), 7, 31)
                if date_beginning <= datetime_date(year, month,
                                                   day) <= date_end:
                    return folder['id']
    return generate_year_folder_id(service=service, file_name=file_name)
Example #21
0
def parse_metadata_time(sfield):
    sdate = sfield.split(' ')[1]
    if sdate:
        de = sdate.split('_')
        try:
            return datetime_date(int(de[0]), int(de[1]), int(de[2]))
        except ValueError:
            # Catches conversion to int failure, in case the date is 'unknown'
            pass
    return None
 def test_helper(ymd_tuple, test_reason):
     for year_week_format in ("%Y %W", "%Y %U", "%G %V"):
         for weekday_format in ("%w", "%u", "%a", "%A"):
             format_string = year_week_format + " " + weekday_format
             with self.subTest(test_reason, date=ymd_tuple, format=format_string):
                 dt_date = datetime_date(*ymd_tuple)
                 strp_input = dt_date.strftime(format_string)
                 strp_output = _strptime._strptime_time(strp_input, format_string)
                 msg = "%r: %s != %s" % (strp_input, strp_output[7], dt_date.timetuple()[7])
                 self.assertEqual(strp_output[:3], ymd_tuple, msg)
def parse_metadata_time(sfield):
    sdate = sfield.split(' ')[1]
    if sdate:
        de = sdate.split('_')
        try:
            return datetime_date(int(de[0]),int(de[1]),int(de[2]))
        except ValueError:
            # Catches conversion to int failure, in case the date is 'unknown'
            pass
    return None
def _calc_julian_from_U_or_W(year, week_of_year, day_of_week, week_starts_Mon):
    first_weekday = datetime_date(year, 1, 1).weekday()
    if not week_starts_Mon:
        first_weekday = (first_weekday + 1) % 7
        day_of_week = (day_of_week + 1) % 7
    week_0_length = (7 - first_weekday) % 7
    if week_of_year == 0:
        return 1 + day_of_week - first_weekday
    days_to_week = week_0_length + 7 * (week_of_year - 1)
    return 1 + days_to_week + day_of_week
Example #25
0
 def test_helper(ymd_tuple, test_reason):
     for directive in ('W', 'U'):
         format_string = "%%Y %%%s %%w" % directive
         dt_date = datetime_date(*ymd_tuple)
         strp_input = dt_date.strftime(format_string)
         strp_output = _strptime._strptime_time(strp_input, format_string)
         self.assertTrue(strp_output[:3] == ymd_tuple,
                 "%s(%s) test failed w/ '%s': %s != %s (%s != %s)" %
                     (test_reason, directive, strp_input,
                         strp_output[:3], ymd_tuple,
                         strp_output[7], dt_date.timetuple()[7]))
Example #26
0
def _calc_julian_from_U_or_W(year, week_of_year, day_of_week, week_starts_Mon):
    first_weekday = datetime_date(year, 1, 1).weekday()
    if not week_starts_Mon:
        first_weekday = (first_weekday + 1) % 7
        day_of_week = (day_of_week + 1) % 7
    week_0_length = (7 - first_weekday) % 7
    if week_of_year == 0:
        return 1 + day_of_week - first_weekday
    else:
        days_to_week = week_0_length + 7 * (week_of_year - 1)
        return 1 + days_to_week + day_of_week
Example #27
0
 def test_helper(ymd_tuple, test_reason):
     for directive in ('W', 'U'):
         format_string = "%%Y %%%s %%w" % directive
         dt_date = datetime_date(*ymd_tuple)
         strp_input = dt_date.strftime(format_string)
         strp_output = _strptime._strptime_time(strp_input, format_string)
         self.assertTrue(strp_output[:3] == ymd_tuple,
                 "%s(%s) test failed w/ '%s': %s != %s (%s != %s)" %
                     (test_reason, directive, strp_input,
                         strp_output[:3], ymd_tuple,
                         strp_output[7], dt_date.timetuple()[7]))
Example #28
0
def find_any_date(str_or_match) -> datetime.date or None:
    """Try to find a date in input string and return it as a date object.

    If there is no matching date, return None.
    The returned date can't be from the future.

    """
    if isinstance(str_or_match, str):
        match = ANYDATE_SEARCH(str_or_match)
    else:
        match = str_or_match
    if not match:
        return None
    groupdict = match.groupdict()
    day = int(groupdict['d'])
    year = int(groupdict['Y'])
    month = groupdict.get('jB')
    today = datetime.today().date()
    if month:
        date = jdate(year, jB_TO_NUM[month], day).togregorian()
        if date <= today:
            return date
        return
    month = groupdict.get('B')
    if month:
        date = datetime_date(year, B_TO_NUM[month.lower()], day)
        if date <= today:
            return date
        return
    month = groupdict.get('b')
    if month:
        date = datetime_date(year, b_TO_NUM[month.lower()], day)
        if date <= today:
            return date
        return
    month = groupdict.get('m')
    if month:
        date = datetime_date(year, int(month), day)
        if date <= today:
            return date
        return
Example #29
0
def crossref(doi) -> defaultdict:
    """Get the crossref.org json data for the given DOI. Return parsed data."""
    # See https://github.com/CrossRef/rest-api-doc/blob/master/api_format.md
    # for documentation.
    # Force using the version 1 of the API to prevent breakage. See:
    # https://github.com/CrossRef/rest-api-doc/blob/master/rest_api.md#how-to-manage-api-versions
    j = requests_get(
        'http://api.crossref.org/v1/works/' + doi, timeout=10
    ).json()
    assert j['status'] == 'ok'
    d = defaultdict(
        lambda: None, {k.lower(): v for k, v in j['message'].items()})

    d['cite_type'] = d.pop('type')

    for field in ('title', 'container-title', 'issn', 'isbn'):
        value = d[field]
        if value:
            d[field] = value[0]

    date = d['issued']['date-parts'][0]
    date_len = len(date)
    if date_len == 3:
        d['date'] = datetime_date(*date)
    elif date_len == 2:
        d['year'], d['month'] = str(date[0]), str(date[1])
    else:
        year = date[0]
        # date can be of the form [None]
        # https://github.com/CrossRef/rest-api-doc/issues/169
        if year:
            d['year'] = str(date[0])

    authors = d['author']
    if authors:
        d['authors'] = \
            [Name(name['given'], name['family']) for name in authors]

    editors = d['editor']
    if editors:
        d['editors'] = \
            [Name(name['given'], name['family']) for name in editors]

    translators = d['translator']
    if translators:
        d['translators'] = \
            [Name(name['given'], name['family']) for name in translators]

    page = d['page']
    if page:
        d['page'] = page.replace('-', '–')

    return d
Example #30
0
def find_any_date(str_or_match) -> datetime.date or None:
    """Try to find a date in input string and return it as a date object.

    If there is no matching date, return None.
    The returned date can't be from the future.
    """
    if isinstance(str_or_match, str):
        match = ANYDATE_SEARCH(str_or_match)
    else:
        match = str_or_match
    if not match:
        return None
    groupdict = match.groupdict()
    day = int(groupdict['d'])
    year = int(groupdict['Y'])
    month = groupdict.get('jB')
    today = datetime.today().date()
    if month:
        date = jdate(year, jB_TO_NUM[month], day).togregorian()
        if date <= today:
            return date
        return
    month = groupdict.get('B')
    if month:
        date = datetime_date(year, B_TO_NUM[month.lower()], day)
        if date <= today:
            return date
        return
    month = groupdict.get('b')
    if month:
        date = datetime_date(year, b_TO_NUM[month.lower()], day)
        if date <= today:
            return date
        return
    month = groupdict.get('m')
    if month:
        date = datetime_date(year, int(month), day)
        if date <= today:
            return date
        return
Example #31
0
def run():
    url = "https://open.senrigan.io/api/betterself/v2/productivity_logs/"

    df = pd.read_excel("snapshots/personal_historical_pomodoros.xlsx",
                       keep_default_na="")
    token_key = f"Token {settings.BETTERSELF_PERSONAL_API_KEY}"
    headers = {"Authorization": token_key}

    df.index = df["Date"]
    df = df.sort_index()

    start_import_period = datetime_date(2020, 7, 9)
    end_import_period = datetime_date(2020, 7, 28)

    for index, row in df.iterrows():
        date = row["Date"].date().isoformat()

        should_import = start_import_period <= index.date(
        ) <= end_import_period
        if not should_import:
            continue

        notes = row["Description Of Day"]
        mistakes = row["Mistakes"]
        pomodoro_count = row["Pomodoros"]

        if not pomodoro_count:
            continue

        data = {
            "pomodoro_count": pomodoro_count,
            "date": date,
            "notes": notes,
            "mistakes": mistakes,
        }

        response = requests.post(url, json=data, headers=headers)
        if response.status_code != 200:
            print(response.content)
            assert response.status_code == 200
Example #32
0
    def get(self, *args, **kwargs):
        date = kwargs.get('date')
        clan = Clan.objects.get(pk=int(self.request.GET.get('clan_id', 35039)))
        force_update = self.request.GET.get('force_update') == 'true'

        if force_update:
            from global_map.management.commands.fetchdata import update_clan
            update_clan(clan.id)

        pa_query = ProvinceAssault.objects.distinct('province').order_by('province')
        if date:
            date = datetime_date(*[int(i) for i in date.split('-')])
            assaults = [
                assault.as_clan_json(clan, current_only=False)
                for assault in pa_query.filter(date=date).filter(
                    Q(battles__clan_a=clan) | Q(battles__clan_b=clan) | Q(clans=clan) | Q(current_owner=clan))
            ]
        else:
            date = datetime.now().date()
            assaults = [
                assault.as_clan_json(clan)
                for assault in pa_query.filter(date=date).filter(Q(clans=clan) | Q(current_owner=clan))
            ]

        # Remove assaults without battles
        for assault in assaults[::]:
            if not assault['battles']:
                logger.debug("Removing assault on province %s", assault['province_info']['province_id'])
                assaults.remove(assault)

        times = [
            battle['planned_start_at']
            for assault in assaults
            for battle in assault['battles']
        ]

        if times:
            min_time = min(times)
            min_time = min_time.replace(minute=min_time.minute - min_time.minute % 30)
            max_time = max(times)
        else:
            min_time = datetime.now().replace(hour=15, minute=00)
            max_time = datetime.now().replace(hour=2, minute=00) + timedelta(days=1)

        return JsonResponse({
            'time_range': [min_time, max_time],
            'assaults': sorted(
                assaults,
                key=lambda v: (v['province_info']['prime_time'].minute == 15, v['battles'][0]['planned_start_at'],
                               v['province_info']['province_id'])
            ),
        })
Example #33
0
def _calc_julian_from_U_or_W(year, week_of_year, day_of_week, week_starts_Mon):
    """Calculate the Julian day based on the year, week of the year, and day of
    the week, with week_start_day representing whether the week of the year
    assumes the week starts on Sunday or Monday (6 or 0)."""
    first_weekday = datetime_date(year, 1, 1).weekday()
    if not week_starts_Mon:
        first_weekday = (first_weekday + 1) % 7
        day_of_week = (day_of_week + 1) % 7
    week_0_length = (7 - first_weekday) % 7
    if week_of_year == 0:
        return 1 + day_of_week - first_weekday
    days_to_week = week_0_length + 7 * (week_of_year - 1)
    return 1 + days_to_week + day_of_week
Example #34
0
def _calc_julian_from_U_or_W(year, week_of_year, day_of_week, week_starts_Mon):
    """Calculate the Julian day based on the year, week of the year, and day of
    the week, with week_start_day representing whether the week of the year
    assumes the week starts on Sunday or Monday (6 or 0)."""
    first_weekday = datetime_date(year, 1, 1).weekday()
    if not week_starts_Mon:
        first_weekday = (first_weekday + 1) % 7
        day_of_week = (day_of_week + 1) % 7
    week_0_length = (7 - first_weekday) % 7
    if week_of_year == 0:
        return 1 + day_of_week - first_weekday
    else:
        days_to_week = week_0_length + 7 * (week_of_year - 1)
        return 1 + days_to_week + day_of_week
Example #35
0
 def test_helper(ymd_tuple, test_reason):
     for year_week_format in ('%Y %W', '%Y %U', '%G %V'):
         for weekday_format in ('%w', '%u', '%a', '%A'):
             format_string = year_week_format + ' ' + weekday_format
             with self.subTest(test_reason,
                               date=ymd_tuple,
                               format=format_string):
                 dt_date = datetime_date(*ymd_tuple)
                 strp_input = dt_date.strftime(format_string)
                 strp_output = _strptime._strptime_time(
                     strp_input, format_string)
                 msg = "%r: %s != %s" % (strp_input, strp_output[7],
                                         dt_date.timetuple()[7])
                 self.assertEqual(strp_output[:3], ymd_tuple, msg)
Example #36
0
def showtimes_record(date):
    # maps a string to a datetime.date object
    date_object = datetime_date(*map(int, date.split('-')))
    showtimes = Showtime.query.filter_by(date=date_object).all()

    if not showtimes:
        raise NotFound

    serialized_objects = showtimes_schema.dumps(showtimes,
                                                sort_keys=True,
                                                indent=4)

    return Response(response=serialized_objects,
                    status=http_status.OK,
                    mimetype="application/json")
Example #37
0
 def test_helper(ymd_tuple, test_reason):
     for year_week_format in ('%Y %W', '%Y %U', '%G %V'):
         for weekday_format in ('%w', '%u', '%a', '%A'):
             format_string = year_week_format + ' ' + weekday_format
             with self.subTest(test_reason,
                               date=ymd_tuple,
                               format=format_string):
                 dt_date = datetime_date(*ymd_tuple)
                 strp_input = dt_date.strftime(format_string)
                 strp_output = _strptime._strptime_time(strp_input,
                                                        format_string)
                 msg = "%r: %s != %s" % (strp_input,
                                         strp_output[7],
                                         dt_date.timetuple()[7])
                 self.assertEqual(strp_output[:3], ymd_tuple, msg)
Example #38
0
    def getConferenceSessionsByTime(self, request):
        """Gets all the Sessions for a Conference within a time period"""

        conference_id = request.websafeConferenceKey
        conference_key = ndb.Key(urlsafe=request.websafeConferenceKey)
        if conference_key.kind() != 'Conference':
            raise endpoints.BadRequestException(
                'Key %s is not a valid conference key' % conference_id
            )

        data = {field.name: getattr(request, field.name) for field in request.all_fields()}

        # add this to all time objects to get the right datetime for querying datastore
        filler_date = datetime_date(1970, 1, 1)

        date = datetime.strptime(data['conferenceDate'], "%Y-%m-%d")

        startTime = datetime.strptime(data['startTime'], "%H:%M").time()
        startDateTime = datetime.combine(date=filler_date, time=startTime)

        endTime = datetime.strptime(data['endTime'], "%H:%M").time()
        endDateTime = datetime.combine(date=filler_date, time=endTime)

        dateFilter = ndb.query.FilterNode(
            'date',
            OPERATORS['EQ'],
            date,
        )

        startTimeFilter = ndb.query.FilterNode(
            'startTime',
            OPERATORS['GTEQ'],
            startDateTime,
        )

        endTimeFilter = ndb.query.FilterNode(
            'startTime',
            OPERATORS['LTEQ'],
            endDateTime,
        )

        sessions = Session.query(ancestor=conference_key).filter(dateFilter)\
           .filter(startTimeFilter).filter(endTimeFilter)

        return SessionForms(
            items=[self._copySessionToForm(session) for session in sessions]
        )
Example #39
0
def _calc_julian_from_U_or_W(year, week_of_year, day_of_week, week_starts_Mon):
    """Calculate the Julian day based on the year, week of the year, and day of
    the week, with week_start_day representing whether the week of the year
    assumes the week starts on Sunday or Monday (6 or 0)."""
    first_weekday = datetime_date(year, 1, 1).weekday()
    # If we are dealing with the %U directive (week starts on Sunday), it's
    # easier to just shift the view to Sunday being the first day of the
    # week.
    if not week_starts_Mon:
        first_weekday = (first_weekday + 1) % 7
        day_of_week = (day_of_week + 1) % 7
    # Need to watch out for a week 0 (when the first day of the year is not
    # the same as that specified by %U or %W).
    week_0_length = (7 - first_weekday) % 7
    if week_of_year == 0:
        return 1 + day_of_week - first_weekday
    days_to_week = week_0_length + (7 * (week_of_year - 1))
    return 1 + days_to_week + day_of_week
Example #40
0
def _calc_julian_from_U_or_W(year, week_of_year, day_of_week, week_starts_Mon):
    """Calculate the Julian day based on the year, week of the year, and day of
    the week, with week_start_day representing whether the week of the year
    assumes the week starts on Sunday or Monday (6 or 0)."""
    first_weekday = datetime_date(year, 1, 1).weekday()
    # If we are dealing with the %U directive (week starts on Sunday), it's
    # easier to just shift the view to Sunday being the first day of the
    # week.
    if not week_starts_Mon:
        first_weekday = (first_weekday + 1) % 7
        day_of_week = (day_of_week + 1) % 7
    # Need to watch out for a week 0 (when the first day of the year is not
    # the same as that specified by %U or %W).
    week_0_length = (7 - first_weekday) % 7
    if week_of_year == 0:
        return 1 + day_of_week - first_weekday
    else:
        days_to_week = week_0_length + (7 * (week_of_year - 1))
        return 1 + days_to_week + day_of_week
Example #41
0
 def test_helper(ymd_tuple, test_reason):
     for year_week_format in ('%Y %W', '%Y %U', '%G %V'):
         if (year_week_format in self._formats_excluded
                 and ymd_tuple in self._ymd_excluded):
             return
         # skip all dates for format %G %V
         if OPENVMS and year_week_format in self._formats_excluded:
             return
         for weekday_format in ('%w', '%u', '%a', '%A'):
             format_string = year_week_format + ' ' + weekday_format
             with self.subTest(test_reason,
                               date=ymd_tuple,
                               format=format_string):
                 dt_date = datetime_date(*ymd_tuple)
                 strp_input = dt_date.strftime(format_string)
                 strp_output = _strptime._strptime_time(
                     strp_input, format_string)
                 msg = "%r: %s != %s" % (strp_input, strp_output[7],
                                         dt_date.timetuple()[7])
                 self.assertEqual(strp_output[:3], ymd_tuple, msg)
Example #42
0
    def getSessionsExcludeTypeTime(self, request):
        """Gets all Sessions not of the specified type and before the specified time"""

        data = {field.name: getattr(request, field.name) for field in request.all_fields()}

        # add this to all time objects to get the right datetime for querying datastore
        filler_date = datetime_date(1970, 1, 1)

        # set time filter
        try:
            latestTime = datetime.strptime(data['latestTime'], "%H:%M").time()
            latestDateTime = datetime.combine(date=filler_date, time=latestTime)
            timeFilter = ndb.query.FilterNode('startTime', OPERATORS['LT'], latestDateTime)
        except (KeyError, TypeError):
            timeFilter = None

        # set type filter
        try:
            excludedSession = data['excludedSessionType']
            allowed_types = SessionType.to_dict().keys()
            allowed_types.remove(str(excludedSession))
        except (KeyError, ValueError):
            allowed_types = None

        sessions = Session.query()
        if timeFilter:
            sessions = sessions.filter(timeFilter)

        if allowed_types:
            correct_sessions = []
            for session in sessions:
                    if session.typeOfSession in allowed_types:
                        correct_sessions.append(session)
        else:
            correct_sessions = sessions

        return SessionForms(
            items=[self._copySessionToForm(session) for session in correct_sessions]
        )
def strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
    """Return a time struct based on the input data and the format string."""
    global _locale_cache
    global _regex_cache
    locale_time = _locale_cache.locale_time
    # If the language changes, caches are invalidated, so clear them
    if locale_time.lang != _getlang():
        _locale_cache = TimeRE()
        _regex_cache.clear()
    format_regex = _regex_cache.get(format)
    if not format_regex:
        # Limit regex cache size to prevent major bloating of the module;
        # The value 5 is arbitrary
        if len(_regex_cache) > 5:
            _regex_cache.clear()
        format_regex = _locale_cache.compile(format)
        _regex_cache[format] = format_regex
    found = format_regex.match(data_string)
    if not found:
        raise ValueError("time data did not match format:  data=%s  fmt=%s" %
                         (data_string, format))
    if len(data_string) != found.end():
        raise ValueError("unconverted data remains: %s" %
                          data_string[found.end():])
    year = 1900
    month = day = 1
    hour = minute = second = 0
    tz = -1
    # weekday and julian defaulted to -1 so as to signal need to calculate values
    weekday = julian = -1
    found_dict = found.groupdict()
    for group_key in found_dict.iterkeys():
        if group_key == 'y':
            year = int(found_dict['y'])
            # Open Group specification for strptime() states that a %y
            #value in the range of [00, 68] is in the century 2000, while
            #[69,99] is in the century 1900
            if year <= 68:
                year += 2000
            else:
                year += 1900
        elif group_key == 'Y':
            year = int(found_dict['Y'])
        elif group_key == 'm':
            month = int(found_dict['m'])
        elif group_key == 'B':
            month = _insensitiveindex(locale_time.f_month, found_dict['B'])
        elif group_key == 'b':
            month = _insensitiveindex(locale_time.a_month, found_dict['b'])
        elif group_key == 'd':
            day = int(found_dict['d'])
        elif group_key == 'H':
            hour = int(found_dict['H'])
        elif group_key == 'I':
            hour = int(found_dict['I'])
            ampm = found_dict.get('p', '').lower()
            # If there was no AM/PM indicator, we'll treat this like AM
            if ampm in ('', locale_time.am_pm[0].lower()):
                # We're in AM so the hour is correct unless we're
                # looking at 12 midnight.
                # 12 midnight == 12 AM == hour 0
                if hour == 12:
                    hour = 0
            elif ampm == locale_time.am_pm[1].lower():
                # We're in PM so we need to add 12 to the hour unless
                # we're looking at 12 noon.
                # 12 noon == 12 PM == hour 12
                if hour != 12:
                    hour += 12
        elif group_key == 'M':
            minute = int(found_dict['M'])
        elif group_key == 'S':
            second = int(found_dict['S'])
        elif group_key == 'A':
            weekday = _insensitiveindex(locale_time.f_weekday,
                                        found_dict['A'])
        elif group_key == 'a':
            weekday = _insensitiveindex(locale_time.a_weekday,
                                        found_dict['a'])
        elif group_key == 'w':
            weekday = int(found_dict['w'])
            if weekday == 0:
                weekday = 6
            else:
                weekday -= 1
        elif group_key == 'j':
            julian = int(found_dict['j'])
        elif group_key == 'Z':
            # Since -1 is default value only need to worry about setting tz if
            # it can be something other than -1.
            found_zone = found_dict['Z'].lower()
            if found_zone in ("utc", "gmt"):
                tz = 0
            elif time.tzname[0] == time.tzname[1] and \
               time.daylight:
                continue #Deals with bad locale setup where timezone info is
                         # the same; first found on FreeBSD 4.4.
            elif locale_time.timezone[2].lower() == found_zone:
                tz = 0
            elif time.daylight and \
                locale_time.timezone[3].lower() == found_zone:
                tz = 1

    # Cannot pre-calculate datetime_date() since can change in Julian
    #calculation and thus could have different value for the day of the week
    #calculation
    if julian == -1:
        # Need to add 1 to result since first day of the year is 1, not 0.
        julian = datetime_date(year, month, day).toordinal() - \
                  datetime_date(year, 1, 1).toordinal() + 1
    else:  # Assume that if they bothered to include Julian day it will
           #be accurate
        datetime_result = datetime_date.fromordinal((julian - 1) + datetime_date(year, 1, 1).toordinal())
        year = datetime_result.year
        month = datetime_result.month
        day = datetime_result.day
    if weekday == -1:
        weekday = datetime_date(year, month, day).weekday()
    return time.struct_time((year, month, day,
                             hour, minute, second,
                             weekday, julian, tz))
Example #44
0
                    if (time.tzname[0] == time.tzname[1] and
                       time.daylight and found_zone not in ("utc", "gmt")):
                        break
                    else:
                        tz = value
                        break
    # If we know the week of the year and what day of that week, we can figure
    # out the Julian day of the year.
    if julian == -1 and week_of_year != -1 and weekday != -1:
        week_starts_Mon = True if week_of_year_start == 0 else False
        julian = _calc_julian_from_U_or_W(year, week_of_year, weekday,
                                            week_starts_Mon)
    # Cannot pre-calculate datetime_date() since can change in Julian
    # calculation and thus could have different value for the day of the week
    # calculation.
    if julian == -1:
        # Need to add 1 to result since first day of the year is 1, not 0.
        julian = datetime_date(year, month, day).toordinal() - \
                  datetime_date(year, 1, 1).toordinal() + 1
    else:  # Assume that if they bothered to include Julian day it will
           # be accurate.
        datetime_result = datetime_date.fromordinal((julian - 1) + datetime_date(year, 1, 1).toordinal())
        year = datetime_result.year
        month = datetime_result.month
        day = datetime_result.day
    if weekday == -1:
        weekday = datetime_date(year, month, day).weekday()
    return time.struct_time((year, month, day,
                             hour, minute, second,
                             weekday, julian, tz))
Example #45
0
def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
    """Return a time struct based on the input string and the format string."""
    global _TimeRE_cache
    global _regex_cache
    with _cache_lock:
        if _getlang() != _TimeRE_cache.locale_time.lang:
            _TimeRE_cache = TimeRE()
            _regex_cache.clear()
        if len(_regex_cache) > _CACHE_MAX_SIZE:
            _regex_cache.clear()
        locale_time = _TimeRE_cache.locale_time
        format_regex = _regex_cache.get(format)
        if not format_regex:
            try:
                format_regex = _TimeRE_cache.compile(format)
            except KeyError as err:
                bad_directive = err.args[0]
                if bad_directive == "\\":
                    bad_directive = "%"
                del err
                raise ValueError("'%s' is a bad directive in format '%s'" % (bad_directive, format))
            except IndexError:
                raise ValueError("stray %% in format '%s'" % format)

            _regex_cache[format] = format_regex
    found = format_regex.match(data_string)
    if not found:
        raise ValueError("time data %r does not match format %r" % (data_string, format))
    if len(data_string) != found.end():
        raise ValueError("unconverted data remains: %s" % data_string[found.end() :])
    year = None
    month = day = 1
    hour = minute = second = fraction = 0
    tz = -1
    week_of_year = -1
    week_of_year_start = -1
    weekday = julian = -1
    found_dict = found.groupdict()
    for group_key in found_dict.iterkeys():
        if group_key == "y":
            year = int(found_dict["y"])
            if year <= 68:
                year += 2000
            else:
                year += 1900
        elif group_key == "Y":
            year = int(found_dict["Y"])
        elif group_key == "m":
            month = int(found_dict["m"])
        elif group_key == "B":
            month = locale_time.f_month.index(found_dict["B"].lower())
        elif group_key == "b":
            month = locale_time.a_month.index(found_dict["b"].lower())
        elif group_key == "d":
            day = int(found_dict["d"])
        elif group_key == "H":
            hour = int(found_dict["H"])
        elif group_key == "I":
            hour = int(found_dict["I"])
            ampm = found_dict.get("p", "").lower()
            if ampm in ("", locale_time.am_pm[0]):
                if hour == 12:
                    hour = 0
            elif ampm == locale_time.am_pm[1]:
                if hour != 12:
                    hour += 12
        elif group_key == "M":
            minute = int(found_dict["M"])
        elif group_key == "S":
            second = int(found_dict["S"])
        elif group_key == "f":
            s = found_dict["f"]
            s += "0" * (6 - len(s))
            fraction = int(s)
        elif group_key == "A":
            weekday = locale_time.f_weekday.index(found_dict["A"].lower())
        elif group_key == "a":
            weekday = locale_time.a_weekday.index(found_dict["a"].lower())
        elif group_key == "w":
            weekday = int(found_dict["w"])
            if weekday == 0:
                weekday = 6
            else:
                weekday -= 1
        elif group_key == "j":
            julian = int(found_dict["j"])
        elif group_key in ("U", "W"):
            week_of_year = int(found_dict[group_key])
            if group_key == "U":
                week_of_year_start = 6
            else:
                week_of_year_start = 0
        elif group_key == "Z":
            found_zone = found_dict["Z"].lower()
            for value, tz_values in enumerate(locale_time.timezone):
                if found_zone in tz_values:
                    if time.tzname[0] == time.tzname[1] and time.daylight and found_zone not in ("utc", "gmt"):
                        break
                    else:
                        tz = value
                        break

    leap_year_fix = False
    if year is None and month == 2 and day == 29:
        year = 1904
        leap_year_fix = True
    elif year is None:
        year = 1900
    if julian == -1 and week_of_year != -1 and weekday != -1:
        week_starts_Mon = True if week_of_year_start == 0 else False
        julian = _calc_julian_from_U_or_W(year, week_of_year, weekday, week_starts_Mon)
    if julian == -1:
        julian = datetime_date(year, month, day).toordinal() - datetime_date(year, 1, 1).toordinal() + 1
    else:
        datetime_result = datetime_date.fromordinal(julian - 1 + datetime_date(year, 1, 1).toordinal())
        year = datetime_result.year
        month = datetime_result.month
        day = datetime_result.day
    if weekday == -1:
        weekday = datetime_date(year, month, day).weekday()
    if leap_year_fix:
        year = 1900
    return (time.struct_time((year, month, day, hour, minute, second, weekday, julian, tz)), fraction)
Example #46
0
def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
    """Return a 2-tuple consisting of a time struct and an int containing
    the number of microseconds based on the input string and the
    format string."""

    for index, arg in enumerate([data_string, format]):
        if not isinstance(arg, str):
            msg = "strptime() argument {} must be str, not {}"
            raise TypeError(msg.format(index, type(arg)))

    global _TimeRE_cache, _regex_cache
    with _cache_lock:
        locale_time = _TimeRE_cache.locale_time
        if (_getlang() != locale_time.lang or
            time.tzname != locale_time.tzname or
            time.daylight != locale_time.daylight):
            _TimeRE_cache = TimeRE()
            _regex_cache.clear()
            locale_time = _TimeRE_cache.locale_time
        if len(_regex_cache) > _CACHE_MAX_SIZE:
            _regex_cache.clear()
        format_regex = _regex_cache.get(format)
        if not format_regex:
            try:
                format_regex = _TimeRE_cache.compile(format)
            # KeyError raised when a bad format is found; can be specified as
            # \\, in which case it was a stray % but with a space after it
            except KeyError as err:
                bad_directive = err.args[0]
                if bad_directive == "\\":
                    bad_directive = "%"
                del err
                raise ValueError("'%s' is a bad directive in format '%s'" %
                                    (bad_directive, format)) from None
            # IndexError only occurs when the format string is "%"
            except IndexError:
                raise ValueError("stray %% in format '%s'" % format) from None
            _regex_cache[format] = format_regex
    found = format_regex.match(data_string)
    if not found:
        raise ValueError("time data %r does not match format %r" %
                         (data_string, format))
    if len(data_string) != found.end():
        raise ValueError("unconverted data remains: %s" %
                          data_string[found.end():])

    iso_year = year = None
    month = day = 1
    hour = minute = second = fraction = 0
    tz = -1
    tzoffset = None
    # Default to -1 to signify that values not known; not critical to have,
    # though
    iso_week = week_of_year = None
    week_of_year_start = None
    # weekday and julian defaulted to None so as to signal need to calculate
    # values
    weekday = julian = None
    found_dict = found.groupdict()
    for group_key in found_dict.keys():
        # Directives not explicitly handled below:
        #   c, x, X
        #      handled by making out of other directives
        #   U, W
        #      worthless without day of the week
        if group_key == 'y':
            year = int(found_dict['y'])
            # Open Group specification for strptime() states that a %y
            #value in the range of [00, 68] is in the century 2000, while
            #[69,99] is in the century 1900
            if year <= 68:
                year += 2000
            else:
                year += 1900
        elif group_key == 'Y':
            year = int(found_dict['Y'])
        elif group_key == 'G':
            iso_year = int(found_dict['G'])
        elif group_key == 'm':
            month = int(found_dict['m'])
        elif group_key == 'B':
            month = locale_time.f_month.index(found_dict['B'].lower())
        elif group_key == 'b':
            month = locale_time.a_month.index(found_dict['b'].lower())
        elif group_key == 'd':
            day = int(found_dict['d'])
        elif group_key == 'H':
            hour = int(found_dict['H'])
        elif group_key == 'I':
            hour = int(found_dict['I'])
            ampm = found_dict.get('p', '').lower()
            # If there was no AM/PM indicator, we'll treat this like AM
            if ampm in ('', locale_time.am_pm[0]):
                # We're in AM so the hour is correct unless we're
                # looking at 12 midnight.
                # 12 midnight == 12 AM == hour 0
                if hour == 12:
                    hour = 0
            elif ampm == locale_time.am_pm[1]:
                # We're in PM so we need to add 12 to the hour unless
                # we're looking at 12 noon.
                # 12 noon == 12 PM == hour 12
                if hour != 12:
                    hour += 12
        elif group_key == 'M':
            minute = int(found_dict['M'])
        elif group_key == 'S':
            second = int(found_dict['S'])
        elif group_key == 'f':
            s = found_dict['f']
            # Pad to always return microseconds.
            s += "0" * (6 - len(s))
            fraction = int(s)
        elif group_key == 'A':
            weekday = locale_time.f_weekday.index(found_dict['A'].lower())
        elif group_key == 'a':
            weekday = locale_time.a_weekday.index(found_dict['a'].lower())
        elif group_key == 'w':
            weekday = int(found_dict['w'])
            if weekday == 0:
                weekday = 6
            else:
                weekday -= 1
        elif group_key == 'u':
            weekday = int(found_dict['u'])
            weekday -= 1
        elif group_key == 'j':
            julian = int(found_dict['j'])
        elif group_key in ('U', 'W'):
            week_of_year = int(found_dict[group_key])
            if group_key == 'U':
                # U starts week on Sunday.
                week_of_year_start = 6
            else:
                # W starts week on Monday.
                week_of_year_start = 0
        elif group_key == 'V':
            iso_week = int(found_dict['V'])
        elif group_key == 'z':
            z = found_dict['z']
            tzoffset = int(z[1:3]) * 60 + int(z[3:5])
            if z.startswith("-"):
                tzoffset = -tzoffset
        elif group_key == 'Z':
            # Since -1 is default value only need to worry about setting tz if
            # it can be something other than -1.
            found_zone = found_dict['Z'].lower()
            for value, tz_values in enumerate(locale_time.timezone):
                if found_zone in tz_values:
                    # Deal with bad locale setup where timezone names are the
                    # same and yet time.daylight is true; too ambiguous to
                    # be able to tell what timezone has daylight savings
                    if (time.tzname[0] == time.tzname[1] and
                       time.daylight and found_zone not in ("utc", "gmt")):
                        break
                    else:
                        tz = value
                        break
    # Deal with the cases where ambiguities arize
    # don't assume default values for ISO week/year
    if year is None and iso_year is not None:
        if iso_week is None or weekday is None:
            raise ValueError("ISO year directive '%G' must be used with "
                             "the ISO week directive '%V' and a weekday "
                             "directive ('%A', '%a', '%w', or '%u').")
        if julian is not None:
            raise ValueError("Day of the year directive '%j' is not "
                             "compatible with ISO year directive '%G'. "
                             "Use '%Y' instead.")
    elif week_of_year is None and iso_week is not None:
        if weekday is None:
            raise ValueError("ISO week directive '%V' must be used with "
                             "the ISO year directive '%G' and a weekday "
                             "directive ('%A', '%a', '%w', or '%u').")
        else:
            raise ValueError("ISO week directive '%V' is incompatible with "
                             "the year directive '%Y'. Use the ISO year '%G' "
                             "instead.")

    leap_year_fix = False
    if year is None and month == 2 and day == 29:
        year = 1904  # 1904 is first leap year of 20th century
        leap_year_fix = True
    elif year is None:
        year = 1900


    # If we know the week of the year and what day of that week, we can figure
    # out the Julian day of the year.
    if julian is None and weekday is not None:
        if week_of_year is not None:
            week_starts_Mon = True if week_of_year_start == 0 else False
            julian = _calc_julian_from_U_or_W(year, week_of_year, weekday,
                                                week_starts_Mon)
        elif iso_year is not None and iso_week is not None:
            year, julian = _calc_julian_from_V(iso_year, iso_week, weekday + 1)

    if julian is None:
        # Cannot pre-calculate datetime_date() since can change in Julian
        # calculation and thus could have different value for the day of
        # the week calculation.
        # Need to add 1 to result since first day of the year is 1, not 0.
        julian = datetime_date(year, month, day).toordinal() - \
                  datetime_date(year, 1, 1).toordinal() + 1
    else:  # Assume that if they bothered to include Julian day (or if it was
           # calculated above with year/week/weekday) it will be accurate.
        datetime_result = datetime_date.fromordinal(
                            (julian - 1) +
                            datetime_date(year, 1, 1).toordinal())
        year = datetime_result.year
        month = datetime_result.month
        day = datetime_result.day
    if weekday is None:
        weekday = datetime_date(year, month, day).weekday()
    # Add timezone info
    tzname = found_dict.get("Z")
    if tzoffset is not None:
        gmtoff = tzoffset * 60
    else:
        gmtoff = None

    if leap_year_fix:
        # the caller didn't supply a year but asked for Feb 29th. We couldn't
        # use the default of 1900 for computations. We set it back to ensure
        # that February 29th is smaller than March 1st.
        year = 1900

    return (year, month, day,
            hour, minute, second,
            weekday, julian, tz, tzname, gmtoff), fraction
Example #47
0
def strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
    """Return a time struct based on the input string and the format string."""
    global _TimeRE_cache
    _cache_lock.acquire()
    try:
        time_re = _TimeRE_cache
        locale_time = time_re.locale_time
        if _getlang() != locale_time.lang:
            _TimeRE_cache = TimeRE()
        if len(_regex_cache) > _CACHE_MAX_SIZE:
            _regex_cache.clear()
        format_regex = _regex_cache.get(format)
        if not format_regex:
            format_regex = time_re.compile(format)
            _regex_cache[format] = format_regex
    finally:
        _cache_lock.release()
    found = format_regex.match(data_string)
    if not found:
        raise ValueError("time data did not match format:  data=%s  fmt=%s" %
                         (data_string, format))
    if len(data_string) != found.end():
        raise ValueError("unconverted data remains: %s" %
                          data_string[found.end():])
    year = 1900
    month = day = 1
    hour = minute = second = 0
    tz = -1
    # weekday and julian defaulted to -1 so as to signal need to calculate values
    weekday = julian = -1
    found_dict = found.groupdict()
    for group_key in found_dict.keys():
        if group_key == 'y':
            year = int(found_dict['y'])
            # Open Group specification for strptime() states that a %y
            #value in the range of [00, 68] is in the century 2000, while
            #[69,99] is in the century 1900
            if year <= 68:
                year += 2000
            else:
                year += 1900
        elif group_key == 'Y':
            year = int(found_dict['Y'])
        elif group_key == 'm':
            month = int(found_dict['m'])
        elif group_key == 'B':
            month = locale_time.f_month.index(found_dict['B'].lower())
        elif group_key == 'b':
            month = locale_time.a_month.index(found_dict['b'].lower())
        elif group_key == 'd':
            day = int(found_dict['d'])
        elif group_key == 'H':
            hour = int(found_dict['H'])
        elif group_key == 'I':
            hour = int(found_dict['I'])
            ampm = found_dict.get('p', '').lower()
            # If there was no AM/PM indicator, we'll treat this like AM
            if ampm in ('', locale_time.am_pm[0]):
                # We're in AM so the hour is correct unless we're
                # looking at 12 midnight.
                # 12 midnight == 12 AM == hour 0
                if hour == 12:
                    hour = 0
            elif ampm == locale_time.am_pm[1]:
                # We're in PM so we need to add 12 to the hour unless
                # we're looking at 12 noon.
                # 12 noon == 12 PM == hour 12
                if hour != 12:
                    hour += 12
        elif group_key == 'M':
            minute = int(found_dict['M'])
        elif group_key == 'S':
            second = int(found_dict['S'])
        elif group_key == 'A':
            weekday = locale_time.f_weekday.index(found_dict['A'].lower())
        elif group_key == 'a':
            weekday = locale_time.a_weekday.index(found_dict['a'].lower())
        elif group_key == 'w':
            weekday = int(found_dict['w'])
            if weekday == 0:
                weekday = 6
            else:
                weekday -= 1
        elif group_key == 'j':
            julian = int(found_dict['j'])
        elif group_key == 'Z':
            # Since -1 is default value only need to worry about setting tz if
            # it can be something other than -1.
            found_zone = found_dict['Z'].lower()
            for value, tz_values in enumerate(locale_time.timezone):
                if found_zone in tz_values:
                    # Deal with bad locale setup where timezone names are the
                    # same and yet time.daylight is true; too ambiguous to
                    # be able to tell what timezone has daylight savings
                    if time.tzname[0] == time.tzname[1] and \
                       time.daylight:
                            break
                    else:
                        tz = value
                        break
    # Cannot pre-calculate datetime_date() since can change in Julian
    #calculation and thus could have different value for the day of the week
    #calculation
    if datetime_date:
        if julian == -1:
            # Need to add 1 to result since first day of the year is 1, not 0.
            julian = datetime_date(year, month, day).toordinal() - \
                      datetime_date(year, 1, 1).toordinal() + 1
        else:  # Assume that if they bothered to include Julian day it will
               #be accurate
            datetime_result = datetime_date.fromordinal((julian - 1) + datetime_date(year, 1, 1).toordinal())
            year = datetime_result.year
            month = datetime_result.month
            day = datetime_result.day
        if weekday == -1:
            weekday = datetime_date(year, month, day).weekday()
    return (year, month, day, hour, minute, second, weekday, julian, tz)
def strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
    """Return a time struct based on the input data and the format string."""
    global _locale_cache
    global _regex_cache
    locale_time = _locale_cache.locale_time
    # If the language changes, caches are invalidated, so clear them
    if locale_time.lang != _getlang():
        _locale_cache = TimeRE()
        _regex_cache.clear()
    format_regex = _regex_cache.get(format)
    if not format_regex:
        # Limit regex cache size to prevent major bloating of the module;
        # The value 5 is arbitrary
        if len(_regex_cache) > 5:
            _regex_cache.clear()
        format_regex = _locale_cache.compile(format)
        _regex_cache[format] = format_regex
    found = format_regex.match(data_string)
    if not found:
        raise ValueError("time data did not match format:  data=%s  fmt=%s" %
                         (data_string, format))
    if len(data_string) != found.end():
        raise ValueError("unconverted data remains: %s" %
                          data_string[found.end():])
    year = 1900
    month = day = 1
    hour = minute = second = 0
    tz = -1
    week_of_year = -1
    week_of_year_start = -1
    # weekday and julian defaulted to -1 so as to signal need to calculate values
    weekday = julian = -1
    found_dict = found.groupdict()
    for group_key in found_dict.iterkeys():
        if group_key == 'y':
            year = int(found_dict['y'])
            # Open Group specification for strptime() states that a %y
            #value in the range of [00, 68] is in the century 2000, while
            #[69,99] is in the century 1900
            if year <= 68:
                year += 2000
            else:
                year += 1900
        elif group_key == 'Y':
            year = int(found_dict['Y'])
        elif group_key == 'm':
            month = int(found_dict['m'])
        elif group_key == 'B':
            month = _insensitiveindex(locale_time.f_month, found_dict['B'])
        elif group_key == 'b':
            month = _insensitiveindex(locale_time.a_month, found_dict['b'])
        elif group_key == 'd':
            day = int(found_dict['d'])
        elif group_key == 'H':
            hour = int(found_dict['H'])
        elif group_key == 'I':
            hour = int(found_dict['I'])
            ampm = found_dict.get('p', '').lower()
            # If there was no AM/PM indicator, we'll treat this like AM
            if ampm in ('', locale_time.am_pm[0].lower()):
                # We're in AM so the hour is correct unless we're
                # looking at 12 midnight.
                # 12 midnight == 12 AM == hour 0
                if hour == 12:
                    hour = 0
            elif ampm == locale_time.am_pm[1].lower():
                # We're in PM so we need to add 12 to the hour unless
                # we're looking at 12 noon.
                # 12 noon == 12 PM == hour 12
                if hour != 12:
                    hour += 12
        elif group_key == 'M':
            minute = int(found_dict['M'])
        elif group_key == 'S':
            second = int(found_dict['S'])
        elif group_key == 'A':
            weekday = _insensitiveindex(locale_time.f_weekday,
                                        found_dict['A'])
        elif group_key == 'a':
            weekday = _insensitiveindex(locale_time.a_weekday,
                                        found_dict['a'])
        elif group_key == 'w':
            weekday = int(found_dict['w'])
            if weekday == 0:
                weekday = 6
            else:
                weekday -= 1
        elif group_key == 'j':
            julian = int(found_dict['j'])
        elif group_key in ('U', 'W'):
            week_of_year = int(found_dict[group_key])
            if group_key == 'U':
                # U starts week on Sunday
                week_of_year_start = 6
            else:
                # W starts week on Monday
                week_of_year_start = 0
        elif group_key == 'Z':
            # Since -1 is default value only need to worry about setting tz if
            # it can be something other than -1.
            found_zone = found_dict['Z'].lower()
            if found_zone in ("utc", "gmt"):
                tz = 0
            elif time.tzname[0] == time.tzname[1] and \
               time.daylight:
                continue #Deals with bad locale setup where timezone info is
                         # the same; first found on FreeBSD 4.4.
            elif locale_time.timezone[2].lower() == found_zone:
                tz = 0
            elif time.daylight and \
                locale_time.timezone[3].lower() == found_zone:
                tz = 1
    # If we know the week of the year and what day of that week, we can figure
    # out the Julian day of the year
    # Calculations below assume 0 is a Monday
    if julian == -1 and week_of_year != -1 and weekday != -1:
        # Calculate how many days in week 0
        first_weekday = datetime_date(year, 1, 1).weekday()
        preceeding_days = 7 - first_weekday
        if preceeding_days == 7:
            preceeding_days = 0
        # Adjust for U directive so that calculations are not dependent on
        # directive used to figure out week of year
        if weekday == 6 and week_of_year_start == 6:
            week_of_year -= 1
        # If a year starts and ends on a Monday but a week is specified to
        # start on a Sunday we need to up the week to counter-balance the fact
        # that with %W that first Monday starts week 1 while with %U that is
        # week 0 and thus shifts everything by a week
        if weekday == 0 and first_weekday == 0 and week_of_year_start == 6:
            week_of_year += 1
        # If in week 0, then just figure out how many days from Jan 1 to day of
        # week specified, else calculate by multiplying week of year by 7,
        # adding in days in week 0, and the number of days from Monday to the
        # day of the week
        if week_of_year == 0:
            julian = 1 + weekday - first_weekday
        else:
            days_to_week = preceeding_days + (7 * (week_of_year - 1))
            julian = 1 + days_to_week + weekday
    # Cannot pre-calculate datetime_date() since can change in Julian
    #calculation and thus could have different value for the day of the week
    #calculation
    if julian == -1:
        # Need to add 1 to result since first day of the year is 1, not 0.
        julian = datetime_date(year, month, day).toordinal() - \
                  datetime_date(year, 1, 1).toordinal() + 1
    else:  # Assume that if they bothered to include Julian day it will
           #be accurate
        datetime_result = datetime_date.fromordinal((julian - 1) + datetime_date(year, 1, 1).toordinal())
        year = datetime_result.year
        month = datetime_result.month
        day = datetime_result.day
    if weekday == -1:
        weekday = datetime_date(year, month, day).weekday()
    return time.struct_time((year, month, day,
                             hour, minute, second,
                             weekday, julian, tz))
Example #49
0
def strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
    """Return a time struct based on the input string and the format string."""
    global _TimeRE_cache, _regex_cache
    _cache_lock.acquire()
    try:
        time_re = _TimeRE_cache
        locale_time = time_re.locale_time
        if _getlang() != locale_time.lang:
            _TimeRE_cache = TimeRE()
            _regex_cache = {}
        if len(_regex_cache) > _CACHE_MAX_SIZE:
            _regex_cache.clear()
        format_regex = _regex_cache.get(format)
        if not format_regex:
            format_regex = time_re.compile(format)
            _regex_cache[format] = format_regex
    finally:
        _cache_lock.release()
    found = format_regex.match(data_string)
    if not found:
        raise ValueError("time data did not match format:  data=%s  fmt=%s" %
                         (data_string, format))
    if len(data_string) != found.end():
        raise ValueError("unconverted data remains: %s" %
                          data_string[found.end():])
    year = 1900
    month = day = 1
    hour = minute = second = 0
    tz = -1
    # Default to -1 to signify that values not known; not critical to have,
    # though
    week_of_year = -1
    week_of_year_start = -1
    # weekday and julian defaulted to -1 so as to signal need to calculate
    # values
    weekday = julian = -1
    found_dict = found.groupdict()
    for group_key in found_dict.iterkeys():
        # Directives not explicitly handled below:
        #   c, x, X
        #      handled by making out of other directives
        #   U, W
        #      worthless without day of the week
        if group_key == 'y':
            year = int(found_dict['y'])
            # Open Group specification for strptime() states that a %y
            #value in the range of [00, 68] is in the century 2000, while
            #[69,99] is in the century 1900
            if year <= 68:
                year += 2000
            else:
                year += 1900
        elif group_key == 'Y':
            year = int(found_dict['Y'])
        elif group_key == 'm':
            month = int(found_dict['m'])
        elif group_key == 'B':
            month = locale_time.f_month.index(found_dict['B'].lower())
        elif group_key == 'b':
            month = locale_time.a_month.index(found_dict['b'].lower())
        elif group_key == 'd':
            day = int(found_dict['d'])
        elif group_key == 'H':
            hour = int(found_dict['H'])
        elif group_key == 'I':
            hour = int(found_dict['I'])
            ampm = found_dict.get('p', '').lower()
            # If there was no AM/PM indicator, we'll treat this like AM
            if ampm in ('', locale_time.am_pm[0]):
                # We're in AM so the hour is correct unless we're
                # looking at 12 midnight.
                # 12 midnight == 12 AM == hour 0
                if hour == 12:
                    hour = 0
            elif ampm == locale_time.am_pm[1]:
                # We're in PM so we need to add 12 to the hour unless
                # we're looking at 12 noon.
                # 12 noon == 12 PM == hour 12
                if hour != 12:
                    hour += 12
        elif group_key == 'M':
            minute = int(found_dict['M'])
        elif group_key == 'S':
            second = int(found_dict['S'])
        elif group_key == 'A':
            weekday = locale_time.f_weekday.index(found_dict['A'].lower())
        elif group_key == 'a':
            weekday = locale_time.a_weekday.index(found_dict['a'].lower())
        elif group_key == 'w':
            weekday = int(found_dict['w'])
            if weekday == 0:
                weekday = 6
            else:
                weekday -= 1
        elif group_key == 'j':
            julian = int(found_dict['j'])
        elif group_key in ('U', 'W'):
            week_of_year = int(found_dict[group_key])
            if group_key == 'U':
                # U starts week on Sunday
                week_of_year_start = 6
            else:
                # W starts week on Monday
                week_of_year_start = 0
        elif group_key == 'Z':
            # Since -1 is default value only need to worry about setting tz if
            # it can be something other than -1.
            found_zone = found_dict['Z'].lower()
            for value, tz_values in enumerate(locale_time.timezone):
                if found_zone in tz_values:
                    # Deal with bad locale setup where timezone names are the
                    # same and yet time.daylight is true; too ambiguous to
                    # be able to tell what timezone has daylight savings
                    if (time.tzname[0] == time.tzname[1] and
                       time.daylight and found_zone not in ("utc", "gmt")):
                        break
                    else:
                        tz = value
                        break
    # If we know the week of the year and what day of that week, we can figure
    # out the Julian day of the year
    # Calculations below assume 0 is a Monday
    if julian == -1 and week_of_year != -1 and weekday != -1:
        # Calculate how many days in week 0
        first_weekday = datetime_date(year, 1, 1).weekday()
        preceeding_days = 7 - first_weekday
        if preceeding_days == 7:
            preceeding_days = 0
        # Adjust for U directive so that calculations are not dependent on
        # directive used to figure out week of year
        if weekday == 6 and week_of_year_start == 6:
            week_of_year -= 1
        # If a year starts and ends on a Monday but a week is specified to
        # start on a Sunday we need to up the week to counter-balance the fact
        # that with %W that first Monday starts week 1 while with %U that is
        # week 0 and thus shifts everything by a week
        if weekday == 0 and first_weekday == 0 and week_of_year_start == 6:
            week_of_year += 1
        # If in week 0, then just figure out how many days from Jan 1 to day of
        # week specified, else calculate by multiplying week of year by 7,
        # adding in days in week 0, and the number of days from Monday to the
        # day of the week
        if week_of_year == 0:
            julian = 1 + weekday - first_weekday
        else:
            days_to_week = preceeding_days + (7 * (week_of_year - 1))
            julian = 1 + days_to_week + weekday
    # Cannot pre-calculate datetime_date() since can change in Julian
    #calculation and thus could have different value for the day of the week
    #calculation
    if julian == -1:
        # Need to add 1 to result since first day of the year is 1, not 0.
        julian = datetime_date(year, month, day).toordinal() - \
                  datetime_date(year, 1, 1).toordinal() + 1
    else:  # Assume that if they bothered to include Julian day it will
           #be accurate
        datetime_result = datetime_date.fromordinal((julian - 1) + datetime_date(year, 1, 1).toordinal())
        year = datetime_result.year
        month = datetime_result.month
        day = datetime_result.day
    if weekday == -1:
        weekday = datetime_date(year, month, day).weekday()
    return time.struct_time((year, month, day,
                             hour, minute, second,
                             weekday, julian, tz))
def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
    """Return a time struct based on the input string and the format string."""
    global _TimeRE_cache, _regex_cache
    with _cache_lock:
        if _getlang() != _TimeRE_cache.locale_time.lang:
            _TimeRE_cache = TimeRE()
            _regex_cache.clear()
        if len(_regex_cache) > _CACHE_MAX_SIZE:
            _regex_cache.clear()
        locale_time = _TimeRE_cache.locale_time
        format_regex = _regex_cache.get(format)
        if not format_regex:
            try:
                format_regex = _TimeRE_cache.compile(format)
            # KeyError raised when a bad format is found; can be specified as
            # \\, in which case it was a stray % but with a space after it
            except KeyError as err:
                bad_directive = err.args[0]
                if bad_directive == "\\":
                    bad_directive = "%"
                del err
                raise ValueError("'%s' is a bad directive in format '%s'" %
                                    (bad_directive, format))
            # IndexError only occurs when the format string is "%"
            except IndexError:
                raise ValueError("stray %% in format '%s'" % format)
            _regex_cache[format] = format_regex
    found = format_regex.match(data_string)
    if not found:
        raise ValueError("time data %r does not match format %r" %
                         (data_string, format))
    if len(data_string) != found.end():
        raise ValueError("unconverted data remains: %s" %
                          data_string[found.end():])
    year = 1900
    month = day = 1
    hour = minute = second = fraction = 0
    tz = -1
    # Default to -1 to signify that values not known; not critical to have,
    # though
    week_of_year = -1
    week_of_year_start = -1
    # weekday and julian defaulted to -1 so as to signal need to calculate
    # values
    weekday = julian = -1
    found_dict = found.groupdict()
    for group_key in found_dict.keys():
        # Directives not explicitly handled below:
        #   c, x, X
        #      handled by making out of other directives
        #   U, W
        #      worthless without day of the week
        if group_key == 'y':
            year = int(found_dict['y'])
            # Open Group specification for strptime() states that a %y
            #value in the range of [00, 68] is in the century 2000, while
            #[69,99] is in the century 1900
            if year <= 68:
                year += 2000
            else:
                year += 1900
        elif group_key == 'Y':
            year = int(found_dict['Y'])
        elif group_key == 'm':
            month = int(found_dict['m'])
        elif group_key == 'B':
            month = locale_time.f_month.index(found_dict['B'].lower())
        elif group_key == 'b':
            month = locale_time.a_month.index(found_dict['b'].lower())
        elif group_key == 'd':
            day = int(found_dict['d'])
        elif group_key == 'H':
            hour = int(found_dict['H'])
        elif group_key == 'I':
            hour = int(found_dict['I'])
            ampm = found_dict.get('p', '').lower()
            # If there was no AM/PM indicator, we'll treat this like AM
            if ampm in ('', locale_time.am_pm[0]):
                # We're in AM so the hour is correct unless we're
                # looking at 12 midnight.
                # 12 midnight == 12 AM == hour 0
                if hour == 12:
                    hour = 0
            elif ampm == locale_time.am_pm[1]:
                # We're in PM so we need to add 12 to the hour unless
                # we're looking at 12 noon.
                # 12 noon == 12 PM == hour 12
                if hour != 12:
                    hour += 12
        elif group_key == 'M':
            minute = int(found_dict['M'])
        elif group_key == 'S':
            second = int(found_dict['S'])
        elif group_key == 'f':
            s = found_dict['f']
            # Pad to always return microseconds.
            s += "0" * (6 - len(s))
            fraction = int(s)
        elif group_key == 'A':
            weekday = locale_time.f_weekday.index(found_dict['A'].lower())
        elif group_key == 'a':
            weekday = locale_time.a_weekday.index(found_dict['a'].lower())
        elif group_key == 'w':
            weekday = int(found_dict['w'])
            if weekday == 0:
                weekday = 6
            else:
                weekday -= 1
        elif group_key == 'j':
            julian = int(found_dict['j'])
        elif group_key in ('U', 'W'):
            week_of_year = int(found_dict[group_key])
            if group_key == 'U':
                # U starts week on Sunday.
                week_of_year_start = 6
            else:
                # W starts week on Monday.
                week_of_year_start = 0
        elif group_key == 'Z':
            # Since -1 is default value only need to worry about setting tz if
            # it can be something other than -1.
            found_zone = found_dict['Z'].lower()
            for value, tz_values in enumerate(locale_time.timezone):
                if found_zone in tz_values:
                    # Deal with bad locale setup where timezone names are the
                    # same and yet time.daylight is true; too ambiguous to
                    # be able to tell what timezone has daylight savings
                    if (time.tzname[0] == time.tzname[1] and
                       time.daylight and found_zone not in ("utc", "gmt")):
                        break
                    else:
                        tz = value
                        break
    # If we know the week of the year and what day of that week, we can figure
    # out the Julian day of the year.
    if julian == -1 and week_of_year != -1 and weekday != -1:
        week_starts_Mon = True if week_of_year_start == 0 else False
        julian = _calc_julian_from_U_or_W(year, week_of_year, weekday,
                                            week_starts_Mon)
    # Cannot pre-calculate datetime_date() since can change in Julian
    # calculation and thus could have different value for the day of the week
    # calculation.
    if julian == -1:
        # Need to add 1 to result since first day of the year is 1, not 0.
        julian = datetime_date(year, month, day).toordinal() - \
                  datetime_date(year, 1, 1).toordinal() + 1
    else:  # Assume that if they bothered to include Julian day it will
           # be accurate.
        datetime_result = datetime_date.fromordinal((julian - 1) + datetime_date(year, 1, 1).toordinal())
        year = datetime_result.year
        month = datetime_result.month
        day = datetime_result.day
    if weekday == -1:
        weekday = datetime_date(year, month, day).weekday()
    return (time.struct_time((year, month, day,
                              hour, minute, second,
                              weekday, julian, tz)), fraction)
Example #51
0
def _strptime(data_string, format = '%a %b %d %H:%M:%S %Y'):
    """Return a time struct based on the input string and the format string."""
    global _TimeRE_cache
    global _regex_cache
    with _cache_lock:
        if _getlang() != _TimeRE_cache.locale_time.lang:
            _TimeRE_cache = TimeRE()
            _regex_cache.clear()
        if len(_regex_cache) > _CACHE_MAX_SIZE:
            _regex_cache.clear()
        locale_time = _TimeRE_cache.locale_time
        format_regex = _regex_cache.get(format)
        if not format_regex:
            try:
                format_regex = _TimeRE_cache.compile(format)
            except KeyError as err:
                bad_directive = err.args[0]
                if bad_directive == '\\':
                    bad_directive = '%'
                del err
                raise ValueError("'%s' is a bad directive in format '%s'" % (bad_directive, format))
            except IndexError:
                raise ValueError("stray %% in format '%s'" % format)

            _regex_cache[format] = format_regex
    found = format_regex.match(data_string)
    if not found:
        raise ValueError('time data %r does not match format %r' % (data_string, format))
    if len(data_string) != found.end():
        raise ValueError('unconverted data remains: %s' % data_string[found.end():])
    year = None
    month = day = 1
    hour = minute = second = fraction = 0
    tz = -1
    week_of_year = -1
    week_of_year_start = -1
    weekday = julian = -1
    found_dict = found.groupdict()
    for group_key in found_dict.iterkeys():
        if group_key == 'y':
            year = int(found_dict['y'])
            if year <= 68:
                year += 2000
            else:
                year += 1900
        elif group_key == 'Y':
            year = int(found_dict['Y'])
        elif group_key == 'm':
            month = int(found_dict['m'])
        elif group_key == 'B':
            month = locale_time.f_month.index(found_dict['B'].lower())
        elif group_key == 'b':
            month = locale_time.a_month.index(found_dict['b'].lower())
        elif group_key == 'd':
            day = int(found_dict['d'])
        elif group_key == 'H':
            hour = int(found_dict['H'])
        elif group_key == 'I':
            hour = int(found_dict['I'])
            ampm = found_dict.get('p', '').lower()
            if ampm in ('', locale_time.am_pm[0]):
                if hour == 12:
                    hour = 0
            elif ampm == locale_time.am_pm[1]:
                if hour != 12:
                    hour += 12
        elif group_key == 'M':
            minute = int(found_dict['M'])
        elif group_key == 'S':
            second = int(found_dict['S'])
        elif group_key == 'f':
            s = found_dict['f']
            s += '0' * (6 - len(s))
            fraction = int(s)
        elif group_key == 'A':
            weekday = locale_time.f_weekday.index(found_dict['A'].lower())
        elif group_key == 'a':
            weekday = locale_time.a_weekday.index(found_dict['a'].lower())
        elif group_key == 'w':
            weekday = int(found_dict['w'])
            if weekday == 0:
                weekday = 6
            else:
                weekday -= 1
        elif group_key == 'j':
            julian = int(found_dict['j'])
        elif group_key in ('U', 'W'):
            week_of_year = int(found_dict[group_key])
            if group_key == 'U':
                week_of_year_start = 6
            else:
                week_of_year_start = 0
        elif group_key == 'Z':
            found_zone = found_dict['Z'].lower()
            for value, tz_values in enumerate(locale_time.timezone):
                if found_zone in tz_values:
                    if time.tzname[0] == time.tzname[1] and time.daylight and found_zone not in ('utc', 'gmt'):
                        break
                    else:
                        tz = value
                        break

    leap_year_fix = False
    if year is None and month == 2 and day == 29:
        year = 1904
        leap_year_fix = True
    elif year is None:
        year = 1900
    if julian == -1 and week_of_year != -1 and weekday != -1:
        week_starts_Mon = True if week_of_year_start == 0 else False
        julian = _calc_julian_from_U_or_W(year, week_of_year, weekday, week_starts_Mon)
    if julian == -1:
        julian = datetime_date(year, month, day).toordinal() - datetime_date(year, 1, 1).toordinal() + 1
    else:
        datetime_result = datetime_date.fromordinal(julian - 1 + datetime_date(year, 1, 1).toordinal())
        year = datetime_result.year
        month = datetime_result.month
        day = datetime_result.day
    if weekday == -1:
        weekday = datetime_date(year, month, day).weekday()
    if leap_year_fix:
        year = 1900
    return (time.struct_time((year,
      month,
      day,
      hour,
      minute,
      second,
      weekday,
      julian,
      tz)), fraction)
Example #52
0
def strptime(data_string, format="%a %b %d %H:%M:%S %Y",
             reference=(1900, 1, 1, 0, 0, 0, -1, -1, -1),
             complete_with_zeroes=True):
    """Return time struct and microseconds based on an input and format string

    An optional ``reference`` is set by default to 1900-01-01 00:00:00.

    ``reference`` field is a tuple with:

    (year, month, day, hour, minute, second, weekday, julian, tz), microseconds

    Please note that weekday and julian will be ignored.

        >>> from sact.epoch.strptime import strptime
        >>> strptime('13:05', '%H:%M',
        ...          reference=((2000, 1, 1, 0, 0, 30, -1, -1, -1), 5))
        (time.struct_time(tm_year=2000, tm_mon=1, tm_mday=1, tm_hour=13, tm_min=5, tm_sec=0, tm_wday=5, tm_yday=1, tm_isdst=-1), 0)

    Notice how all the left most values (the biggest weight value)
    where used from the given reference, and how the left most value
    (least weight values) was zeroed. The middle part is what was
    parsed.

    You can switch of the zeroing of the left most part:

        >>> strptime('13:05', '%H:%M',
        ...          reference=((2000, 1, 1, 0, 0, 0, -1, -1, -1), 5),
        ...          complete_with_zeroes=False)
        (time.struct_time(tm_year=2000, tm_mon=1, tm_mday=1, tm_hour=13, tm_min=5, tm_sec=0, tm_wday=5, tm_yday=1, tm_isdst=-1), 5)


    """
    with _strptime._cache_lock:
        if _strptime._getlang() != _strptime._TimeRE_cache.locale_time.lang:
            _strptime._TimeRE_cache = TimeRE()
            _strptime._regex_cache.clear()
        if len(_strptime._regex_cache) > _strptime._CACHE_MAX_SIZE:
            _strptime._regex_cache.clear()
        locale_time = _strptime._TimeRE_cache.locale_time
        format_regex = _strptime._regex_cache.get(format)
        if not format_regex:
            try:
                format_regex = _strptime._TimeRE_cache.compile(format)
            # KeyError raised when a bad format is found; can be specified as
            # \\, in which case it was a stray % but with a space after it
            except KeyError as err:
                bad_directive = err.args[0]
                if bad_directive == "\\":
                    bad_directive = "%"
                del err
                raise ValueError("'%s' is a bad directive in format '%s'" %
                                    (bad_directive, format))
            # IndexError only occurs when the format string is "%"
            except IndexError:
                raise ValueError("stray %% in format '%s'" % format)
            _strptime._regex_cache[format] = format_regex
    found = format_regex.match(data_string)
    if not found:
        raise ValueError("time data %r does not match format %r" %
                         (data_string, format))
    if len(data_string) != found.end():
        raise ValueError("unconverted data remains: %s" %
                          data_string[found.end():])
    (year, month, day, hour, minute, second, weekday, julian, tz), \
           fraction = reference
    # Force calculation for julian and weekday
    julian = -1
    weekday = -1
    # Default to -1 to signify that values not known; not critical to have,
    # though
    week_of_year = -1
    week_of_year_start = -1
    found_dict = found.groupdict()
    for group_key in found_dict.keys():
        # Directives not explicitly handled below:
        #   c, x, X
        #      handled by making out of other directives
        #   U, W
        #      worthless without day of the week
        if group_key == 'y':
            year = int(found_dict['y'])
            # Open Group specification for strptime() states that a %y
            #value in the range of [00, 68] is in the century 2000, while
            #[69,99] is in the century 1900
            if year <= 68:
                year += 2000
            else:
                year += 1900
        elif group_key == 'Y':
            year = int(found_dict['Y'])
        elif group_key == 'm':
            month = int(found_dict['m'])
        elif group_key == 'B':
            month = locale_time.f_month.index(found_dict['B'].lower())
        elif group_key == 'b':
            month = locale_time.a_month.index(found_dict['b'].lower())
        elif group_key == 'd':
            day = int(found_dict['d'])
        elif group_key == 'H':
            hour = int(found_dict['H'])
        elif group_key == 'I':
            hour = int(found_dict['I'])
            ampm = found_dict.get('p', '').lower()
            # If there was no AM/PM indicator, we'll treat this like AM
            if ampm in ('', locale_time.am_pm[0]):
                # We're in AM so the hour is correct unless we're
                # looking at 12 midnight.
                # 12 midnight == 12 AM == hour 0
                if hour == 12:
                    hour = 0
            elif ampm == locale_time.am_pm[1]:
                # We're in PM so we need to add 12 to the hour unless
                # we're looking at 12 noon.
                # 12 noon == 12 PM == hour 12
                if hour != 12:
                    hour += 12
        elif group_key == 'M':
            minute = int(found_dict['M'])
        elif group_key == 'S':
            second = int(found_dict['S'])
        elif group_key == 'f':
            s = found_dict['f']
            # Pad to always return microseconds.
            s += "0" * (6 - len(s))
            fraction = int(s)
        elif group_key == 'A':
            weekday = locale_time.f_weekday.index(found_dict['A'].lower())
        elif group_key == 'a':
            weekday = locale_time.a_weekday.index(found_dict['a'].lower())
        elif group_key == 'w':
            weekday = int(found_dict['w'])
            if weekday == 0:
                weekday = 6
            else:
                weekday -= 1
        elif group_key == 'j':
            julian = int(found_dict['j'])
        elif group_key in ('U', 'W'):
            week_of_year = int(found_dict[group_key])
            if group_key == 'U':
                # U starts week on Sunday.
                week_of_year_start = 6
            else:
                # W starts week on Monday.
                week_of_year_start = 0
        elif group_key == 'Z':
            # Since -1 is default value only need to worry about setting tz if
            # it can be something other than -1.
            found_zone = found_dict['Z'].lower()
            for value, tz_values in enumerate(locale_time.timezone):
                if found_zone in tz_values:
                    # Deal with bad locale setup where timezone names are the
                    # same and yet time.daylight is true; too ambiguous to
                    # be able to tell what timezone has daylight savings
                    if (time.tzname[0] == time.tzname[1] and
                       time.daylight and found_zone not in ("utc", "gmt")):
                        break
                    else:
                        tz = value
                        break
    # If we know the week of the year and what day of that week, we can figure
    # out the Julian day of the year.
    if julian == -1 and week_of_year != -1 and weekday != -1:
        week_starts_Mon = True if week_of_year_start == 0 else False
        julian = _strptime._calc_julian_from_U_or_W(year, week_of_year, weekday,
                                            week_starts_Mon)
    if complete_with_zeroes:
        idxs = ("j", "Y", "mBb", "UW", "dAaw", "HI", "M", "S", "f")
        specified = [idxs.index(gk)
                     for gk in [first(idxs, lambda key_group: k in key_group)
                                for k in found_dict.keys()]]
        rightmost_specified = max(specified) if len(specified) else 0
        base = (1900, 1, 1, 1, 0, 0, 0, 0)
        values = year, month, week_of_year, day, hour, minute, second, fraction
        year, month, week_of_year, day, hour, minute, second, fraction = \
               values[:rightmost_specified] + base[rightmost_specified:]

    # Cannot pre-calculate datetime_date() since can change in Julian
    # calculation and thus could have different value for the day of the week
    # calculation.
    if julian == -1:
        # Need to add 1 to result since first day of the year is 1, not 0.
        julian = datetime_date(year, month, day).toordinal() - \
                  datetime_date(year, 1, 1).toordinal() + 1
    else:  # Assume that if they bothered to include Julian day it will
           # be accurate.
        datetime_result = datetime_date.fromordinal(
            (julian - 1) + datetime_date(year, 1, 1).toordinal())
        year = datetime_result.year
        month = datetime_result.month
        day = datetime_result.day
    if weekday == -1:
        weekday = datetime_date(year, month, day).weekday()
    return (time.struct_time((year, month, day,
                              hour, minute, second,
                              weekday, julian, tz)), fraction)