본문으로 건너뛰기
토파즈6.0 · 출시 전
다운로드 →

문서 검색

명세 6.0

방향키로 결과를 고르고 Enter를 눌러 여세요.

API 이름, 진단 번호, 규칙 또는 프로젝트 주제를 입력하세요.

    라이브러리 · std.date

    Date.addDays

    Add signed whole days within the civil date range.

    addDays(days: int) -> Date
    수신자
    Date
    최소 인자 수
    1
    최대 인자 수
    1

    Declaration and parameters

    링크

    사용 조건

    기본 포함 · 미정

    Import std.date in language mode 5.21.

    The current compiler selects std/date.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.

    수신자

    Date

    매개변수 제약

    Date.addDays accepts a signed int count of whole calendar days. Positive values move forward, negative values move backward, and zero returns the same date value.

    The result follows the same proleptic Gregorian calendar as Date.fromYmd. Month boundaries, leap days, and year boundaries are crossed by counting calendar days. Historical calendar cutovers and daylight saving changes do not alter the result.

    The result must remain from 0001-01-01 through 9999-12-31. A count whose mathematical result is outside that range raises TPZ4004 during execution. An intermediate integer overflow also raises TPZ4004.

    An invalid result does not wrap, saturate, return a partial date, or change the receiver. Evaluation stops at the fault before later statements run.

    The receiver is evaluated before the day count and each is evaluated once. An earlier fault prevents later evaluation. The operation has no external effects.

    Adding a count and then its negative recovers the starting date when both results lie in range. Date.toIso and the component accessors observe the resulting date without changing it.

    결과

    The declared result type is Date.

    효과

    실패

    Result and effects

    링크

    Calendar arithmetic preserves the receiver. A result outside years 1 through 9999 raises TPZ4004.

    Resolved contracts

    링크

    Related entries

    링크

    std.date →

    Add whole calendar days within the civil date range

    링크
    규칙topaz.stdlib.contract.date-arithmetic.rule

    Add whole calendar days within the civil date range

    Date.addDays accepts a signed int count of whole calendar days. Positive values move forward, negative values move backward, and zero returns the same date value.

    The result follows the same proleptic Gregorian calendar as Date.fromYmd. Month boundaries, leap days, and year boundaries are crossed by counting calendar days. Historical calendar cutovers and daylight saving changes do not alter the result.

    The result must remain from 0001-01-01 through 9999-12-31. A count whose mathematical result is outside that range raises TPZ4004 during execution. An intermediate integer overflow also raises TPZ4004.

    An invalid result does not wrap, saturate, return a partial date, or change the receiver. Evaluation stops at the fault before later statements run.

    The receiver is evaluated before the day count and each is evaluated once. An earlier fault prevents later evaluation. The operation has no external effects.

    Adding a count and then its negative recovers the starting date when both results lie in range. Date.toIso and the component accessors observe the resulting date without changing it.

    이유

    Calendar arithmetic should depend on a date and a day count. A bounded result with an explicit overflow fault prevents wraparound from silently producing a different civil date.

    Cross leap days, years, and the full supported interval

    링크
    main.tpzCross leap days, years, and the full supported interval플레이그라운드 열기 →
    1. import std.date { Date }
    2. import std.test
    3. import std.io
    4. let leap = test.assertOk(Date.fromYmd(2000, 2, 28))
    5. io.writeLine(leap.addDays(1).toIso())
    6. io.writeLine(leap.addDays(2).toIso())
    7. io.writeLine(leap.toIso())
    8. io.writeLine(test.assertOk(Date.fromYmd(1900, 2, 28)).addDays(1).toIso())
    9. io.writeLine(test.assertOk(Date.fromYmd(1582, 10, 4)).addDays(1).toIso())
    10. io.writeLine(test.assertOk(Date.fromYmd(2000, 1, 1)).addDays(-1).toIso())
    11. io.writeLine(leap.addDays(0).toIso())
    12. io.writeLine(test.assertOk(Date.fromYmd(1, 1, 1)).addDays(3652058).toIso())
    13. io.writeLine(test.assertOk(Date.fromYmd(9999, 12, 31)).addDays(-3652058).toIso())
    기대 출력종료 코드 0
    2000-02-29
    2000-03-01
    2000-02-28
    1900-03-01
    1582-10-05
    1999-12-31
    2000-02-28
    9999-12-31
    0001-01-01
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. 2000-02-29
    2. 2000-03-01
    3. 2000-02-28
    4. 1900-03-01
    5. 1582-10-05
    6. 1999-12-31
    7. 2000-02-28
    8. 9999-12-31
    9. 0001-01-01

    Reject the day after the final supported date

    링크
    main.tpzReject the day after the final supported date플레이그라운드 열기 →
    1. import std.date { Date }
    2. import std.test
    3. import std.io
    4. let date = test.assertOk(Date.fromYmd(9999, 12, 31))
    5. date.addDays(1)
    6. io.writeLine("after")
    기대 출력0이 아닌 종료 코드
    표준 출력 없음.
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 run. 종료 코드가 0이 아닙니다.

    Reject the day before the first supported date

    링크
    main.tpzReject the day before the first supported date플레이그라운드 열기 →
    1. import std.date { Date }
    2. import std.test
    3. import std.io
    4. let date = test.assertOk(Date.fromYmd(1, 1, 1))
    5. date.addDays(-1)
    6. io.writeLine("after")
    기대 출력0이 아닌 종료 코드
    표준 출력 없음.
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 run. 종료 코드가 0이 아닙니다.

    Reject a day count that overflows signed arithmetic

    링크
    main.tpzReject a day count that overflows signed arithmetic플레이그라운드 열기 →
    1. import std.date { Date }
    2. import std.test
    3. import std.io
    4. let date = test.assertOk(Date.fromYmd(1, 1, 1))
    5. date.addDays(-9223372036854775808)
    6. io.writeLine("after")
    기대 출력0이 아닌 종료 코드
    표준 출력 없음.
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 run. 종료 코드가 0이 아닙니다.

    Evaluate a date receiver before its day count

    링크
    main.tpzEvaluate a date receiver before its day count플레이그라운드 열기 →
    1. import std.date { Date }
    2. import std.test
    3. import std.io
    4. function receiver() -> Date {
    5. io.writeLine("date")
    6. return test.assertOk(Date.fromYmd(2024, 2, 28))
    7. }
    8. function days() -> int {
    9. io.writeLine("days")
    10. return 1
    11. }
    12. let next = receiver().addDays(days())
    13. io.writeLine(next.toIso())
    기대 출력종료 코드 0
    date
    days
    2024-02-29
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. date
    2. days
    3. 2024-02-29

    Diagnostics

    링크

    Integer overflow →

    메뉴

    다운로드 →