示例#1
0
def test_normalize_year():
    assert (1900, 1, 1) == normalize_year(1900, 1, 1)
    assert (1900, 2, 1) == normalize_year(1900, 1, 32)
    assert (1900, 3, 1) == normalize_year(1900, 1, 61)
    assert (1900, 4, 1) == normalize_year(1900, 1, 92)
    assert (1900, 5, 1) == normalize_year(1900, 1, 122)
    assert (1900, 4, 1) == normalize_year(1900, 0, 123)
    assert (1900, 3, 1) == normalize_year(1900, -1, 122)

    assert (1899, 12, 1) == normalize_year(1900, 1, -31)
    assert (1899, 12, 1) == normalize_year(1900, 0, 1)
    assert (1899, 11, 1) == normalize_year(1900, -1, 1)
示例#2
0
def date(year, month, day): # Excel reference: https://support.office.com/en-us/article/DATE-function-e36c0c8c-4104-49da-ab83-82328b832349

    if type(year) != int:
        raise TypeError("%s is not an integer" % str(year))

    if type(month) != int:
        raise TypeError("%s is not an integer" % str(month))

    if type(day) != int:
        raise TypeError("%s is not an integer" % str(day))

    if year < 0 or year > 9999:
        raise ValueError("Year must be between 1 and 9999, instead %s" % str(year))

    if year < 1900:
        year = 1900 + year

    year, month, day = normalize_year(year, month, day) # taking into account negative month and day values

    date_0 = datetime(1900, 1, 1)
    date = datetime(year, month, day)

    result = (datetime(year, month, day) - date_0).days + 2

    if result <= 0:
        raise ArithmeticError("Date result is negative")
    else:
        return result
示例#3
0
def date(year, month, day):
    # Excel reference: https://support.office.com/en-us/article/
    #   DATE-function-e36c0c8c-4104-49da-ab83-82328b832349

    # ::TODO:: error handling

    if not isinstance(year, int):
        raise TypeError("%s is not an integer" % year)

    if not isinstance(month, int):
        raise TypeError("%s is not an integer" % month)

    if not isinstance(day, int):
        raise TypeError("%s is not an integer" % day)

    if not (0 <= year <= 9999):
        raise ValueError("Year '%s' must be between 1 and 9999" % year)

    if year < 1900:
        year += 1900

    # taking into account negative month and day values
    year, month, day = normalize_year(year, month, day)

    date_0 = datetime(1900, 1, 1)
    result = (datetime(year, month, day) - date_0).days + 2

    if result <= 0:
        raise ArithmeticError("Date result is negative")
    return result
示例#4
0
def date(
    year, month, day
):  # Excel reference: https://support.office.com/en-us/article/DATE-function-e36c0c8c-4104-49da-ab83-82328b832349

    if type(year) != int:
        raise TypeError("%s is not an integer" % str(year))

    if type(month) != int:
        raise TypeError("%s is not an integer" % str(month))

    if type(day) != int:
        raise TypeError("%s is not an integer" % str(day))

    if year < 0 or year > 9999:
        raise ValueError("Year must be between 1 and 9999, instead %s" %
                         str(year))

    if year < 1900:
        year = 1900 + year

    year, month, day = normalize_year(
        year, month, day)  # taking into account negative month and day values

    date_0 = datetime(1900, 1, 1)
    date = datetime(year, month, day)

    result = (datetime(year, month, day) - date_0).days + 2

    if result <= 0:
        raise ArithmeticError("Date result is negative")
    else:
        return result
示例#5
0
def date(year, month, day):
    """
    The DATE function returns the sequential serial number that represents a particular date.

    :param year: Required. The value of the year argument can include one to four digits.
        Excel interprets the year argument according to the date system your computer is
        using. By default, Microsoft Excel for Windows uses the 1900 date system, which
        means the first date is January 1, 1900.
    :param month: Required. A positive or negative integer representing the month of the
        year from 1 to 12 (January to December).
    :param day: Required. A positive or negative integer representing the day of the month from 1 to 31.
    :return: serial number that represents the particular date.

    .. reference::
        https://support.office.com/en-us/article/DATE-function-e36c0c8c-4104-49da-ab83-82328b832349

    """

    if type(year) != int:
        raise TypeError("%s is not an integer" % str(year))

    if type(month) != int:
        raise TypeError("%s is not an integer" % str(month))

    if type(day) != int:
        raise TypeError("%s is not an integer" % str(day))

    if year < 0 or year > 9999:
        raise ValueError("Year must be between 1 and 9999, instead %s" % str(year))

    if year < 1900:
        year += 1900

    # taking into account negative month and day values
    year, month, day = normalize_year(year, month, day)

    date_0 = datetime(1900, 1, 1)
    date = datetime(year, month, day)

    result = (date - date_0).days + 2

    if result <= 0:
        raise ArithmeticError("Date result is negative")
    else:
        return result
示例#6
0
文件: excellib.py 项目: asitang/pycel
def date(year, month, day):
    # Excel reference: https://support.office.com/en-us/article/
    #   DATE-function-e36c0c8c-4104-49da-ab83-82328b832349

    if not (0 <= year <= 9999):
        return NUM_ERROR

    if year < 1900:
        year += 1900

    # taking into account negative month and day values
    year, month, day = normalize_year(year, month, day)

    date_0 = datetime(1900, 1, 1)
    result = (datetime(year, month, day) - date_0).days + 2

    if result <= 0:
        return NUM_ERROR
    return result
示例#7
0
def test_normalize_year(result, value):
    assert normalize_year(*value) == result
示例#8
0
def test_normalize_year(result, value):
    assert normalize_year(*value) == result