Перейти к содержимому
Топаз6.0 · До релиза
Загрузка →

Поиск в документации

Спецификация 6.0

Выберите результат стрелками и нажмите Enter, чтобы открыть его.

Введите имя API, номер диагностики, правило или тему проекта.

    Библиотека

    std.date

    Work with Gregorian civil dates from year 1 through 9999.

    API этого модуля

    7 API

    ИмяМодуль
    Date.addDays →std.date →
    Date.day →std.date →
    Date.fromYmd →std.date →
    Date.month →std.date →
    Date.parseIso →std.date →
    Date.toIso →std.date →
    Date.year →std.date →

    Module

    Ссылка

    Date validates calendar components, exchanges exact YYYY-MM-DD text, and adds signed whole days without time zones or a host clock.

    A shared calendar and fixed text form give date construction, observation, and arithmetic the same meaning on every host.

    Availability

    Ссылка

    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.

    Exported types

    Ссылка

    export type Date = Date

    Type members keep the compiler-owned type name shown in their declarations. Importing a module does not create a new type identity.

    Public API

    Ссылка

    Date.addDays →

    Date.day →

    Date.fromYmd →

    Date.month →

    Date.parseIso →

    Date.toIso →

    Date.year →

    Pending contracts

    Ссылка

    Construct valid Gregorian civil dates

    Ссылка
    Правилоtopaz.stdlib.contract.date-calendar.rule

    Construct valid Gregorian civil dates

    Date is an immutable proleptic Gregorian calendar date from 0001-01-01 through 9999-12-31. It contains no time of day, time zone, or locale. The same calendar rules apply before and after 1582.

    Date.fromYmd accepts years from 1 through 9999 and months from 1 through 12. The day must be a real day of the selected month and year. All range endpoints are included.

    A year divisible by 4 is a leap year except a century year that is not divisible by 400. February has 29 days in a leap year and 28 otherwise. April, June, September, and November have 30 days. The other months have 31.

    Valid components return Ok containing that date. Year zero, a year outside the supported range, an invalid month, and an invalid day return Err containing a descriptive nonempty string. Components are not normalized into another date.

    The error string describes the failed construction and has no stable machine readable format. Callers branch on Ok or Err.

    Date.year, Date.month, and Date.day are calls with no arguments. They return the validated components of the receiver. A valid Date makes these observations total and leaves the receiver unchanged.

    Arguments are evaluated once in source order before validation. An earlier fault prevents later evaluation. Constructors and observations do not read a host clock or perform external effects.

    Причина

    Validating civil dates at construction gives every date observation a definite value. A shared Gregorian calendar and year range make dates independent of locale, time zones, and historical calendar cutovers.

    Read leap day and both supported endpoints

    Ссылка
    main.tpzRead leap day and both supported endpointsОткрыть песочницу →
    1. import std.date { Date }
    2. import std.test
    3. import std.io
    4. let leap = test.assertOk(Date.fromYmd(2000, 2, 29))
    5. io.writeLine("{leap.year()} {leap.month()} {leap.day()}")
    6. io.writeLine(leap.toIso())
    7. io.writeLine(test.assertOk(Date.fromYmd(1, 1, 1)).toIso())
    8. io.writeLine(test.assertOk(Date.fromYmd(9999, 12, 31)).toIso())
    Ожидаемый выводКод завершения 0
    2000 2 29
    2000-02-29
    0001-01-01
    9999-12-31
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Выполнение завершается. Код завершения 0.

    Ожидаемый стандартный вывод
    1. 2000 2 29
    2. 2000-02-29
    3. 0001-01-01
    4. 9999-12-31

    Reject invalid years, months, and days without normalization

    Ссылка
    main.tpzReject invalid years, months, and days without normalizationОткрыть песочницу →
    1. import std.date { Date }
    2. import std.io
    3. function reject(year: int, month: int, day: int) -> () {
    4. match (Date.fromYmd(year, month, day)) {
    5. case Err(_) => io.writeLine("rejected")
    6. case Ok(_) => io.writeLine("accepted")
    7. }
    8. }
    9. reject(1900, 2, 29)
    10. reject(2023, 2, 29)
    11. reject(2024, 0, 1)
    12. reject(2024, 13, 1)
    13. reject(2024, 4, 31)
    14. reject(2024, 1, 0)
    15. reject(2024, 1, 32)
    16. reject(-1, 1, 1)
    17. reject(10000, 1, 1)
    18. reject(9223372036854775807, 1, 1)
    Ожидаемый выводКод завершения 0
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Выполнение завершается. Код завершения 0.

    Ожидаемый стандартный вывод
    1. rejected
    2. rejected
    3. rejected
    4. rejected
    5. rejected
    6. rejected
    7. rejected
    8. rejected
    9. rejected
    10. rejected

    Reject year zero during component construction

    Ссылка
    main.tpzReject year zero during component constructionОткрыть песочницу →
    1. import std.date { Date }
    2. import std.io
    3. match (Date.fromYmd(0, 1, 1)) {
    4. case Err(_) => io.writeLine("year zero rejected")
    5. case Ok(_) => io.writeLine("accepted")
    6. }
    Ожидаемый выводКод завершения 0
    year zero rejected
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Выполнение завершается. Код завершения 0.

    Ожидаемый стандартный вывод
    1. year zero rejected

    Evaluate all components before date validation

    Ссылка
    main.tpzEvaluate all components before date validationОткрыть песочницу →
    1. import std.date { Date }
    2. import std.io
    3. function observed(label: string, value: int) -> int {
    4. io.writeLine(label)
    5. return value
    6. }
    7. match (Date.fromYmd(observed("year", 2024), observed("month", 13), observed("day", 1))) {
    8. case Err(_) => io.writeLine("rejected")
    9. case Ok(_) => io.writeLine("accepted")
    10. }
    Ожидаемый выводКод завершения 0
    year
    month
    day
    rejected
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Выполнение завершается. Код завершения 0.

    Ожидаемый стандартный вывод
    1. year
    2. month
    3. day
    4. rejected

    Exchange dates in one fixed ISO form

    Ссылка
    Правилоtopaz.stdlib.contract.date-iso.rule

    Exchange dates in one fixed ISO form

    Date.parseIso accepts exactly ten ASCII characters in the form YYYY-MM-DD. The year has four digits and the month and day each have two digits. Hyphens occur after the year and month.

    The components must form a valid Gregorian date in the same range as Date.fromYmd. Valid text returns Ok containing that date. Invalid grammar or an invalid calendar date returns Err containing a descriptive nonempty string.

    Parsing does not trim whitespace or normalize another spelling. Signs, omitted zero padding, alternate separators, non ASCII digits, timestamps, time zones, and extra characters are rejected.

    Date.toIso returns exactly the same ten character form using ASCII digits and zero padding. It has no locale or time zone parameter and leaves the receiver unchanged.

    Parsing the formatted text recovers the original date. Formatting accepted input returns that same text. Date.fromYmd and Date.parseIso establish the same component and range invariants.

    The receiver or argument is evaluated once before the operation. These operations do not change the input or perform external effects. Error strings have no stable machine readable format.

    Причина

    One fixed calendar format makes date text predictable to store, compare lexically, and exchange. Sharing validation with component construction prevents two constructors from assigning different meanings to the same date.

    Parse canonical date text and preserve its components

    Ссылка
    main.tpzParse canonical date text and preserve its componentsОткрыть песочницу →
    1. import std.date { Date }
    2. import std.test
    3. import std.io
    4. for text in (["0001-01-01", "1582-10-10", "2000-02-29", "9999-12-31"]) {
    5. let date = test.assertOk(Date.parseIso(text))
    6. io.writeLine(date.toIso())
    7. }
    8. let leap = test.assertOk(Date.parseIso("2000-02-29"))
    9. io.writeLine("{leap.year()} {leap.month()} {leap.day()}")
    Ожидаемый выводКод завершения 0
    0001-01-01
    1582-10-10
    2000-02-29
    9999-12-31
    2000 2 29
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Выполнение завершается. Код завершения 0.

    Ожидаемый стандартный вывод
    1. 0001-01-01
    2. 1582-10-10
    3. 2000-02-29
    4. 9999-12-31
    5. 2000 2 29

    Reject alternate spellings and invalid calendar dates

    Ссылка
    main.tpzReject alternate spellings and invalid calendar datesОткрыть песочницу →
    1. import std.date { Date }
    2. import std.io
    3. for text in (["", "2024-2-09", "2024-02-9", "2024/02/09", " 2024-02-09", "2024-02-09 ", "2024-02-09T00:00:00Z", "+2024-02-09", "2024-02-09", "2024-02-30", "1900-02-29", "10000-01-01"]) {
    4. match (Date.parseIso(text)) {
    5. case Err(_) => io.writeLine("rejected")
    6. case Ok(_) => io.writeLine("accepted")
    7. }
    8. }
    Ожидаемый выводКод завершения 0
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Выполнение завершается. Код завершения 0.

    Ожидаемый стандартный вывод
    1. rejected
    2. rejected
    3. rejected
    4. rejected
    5. rejected
    6. rejected
    7. rejected
    8. rejected
    9. rejected
    10. rejected
    11. rejected
    12. rejected

    Reject year zero in ISO text

    Ссылка
    main.tpzReject year zero in ISO textОткрыть песочницу →
    1. import std.date { Date }
    2. import std.io
    3. match (Date.parseIso("0000-01-01")) {
    4. case Err(_) => io.writeLine("year zero rejected")
    5. case Ok(_) => io.writeLine("accepted")
    6. }
    Ожидаемый выводКод завершения 0
    year zero rejected
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Выполнение завершается. Код завершения 0.

    Ожидаемый стандартный вывод
    1. year zero rejected

    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")
    Ожидаемый выводНенулевой код завершения
    Нет стандартного вывода.
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Этап отклонения run. Ненулевой код завершения.

    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")
    Ожидаемый выводНенулевой код завершения
    Нет стандартного вывода.
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Этап отклонения run. Ненулевой код завершения.

    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")
    Ожидаемый выводНенулевой код завершения
    Нет стандартного вывода.
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Этап отклонения run. Ненулевой код завершения.

    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 →

    Меню

    Загрузка →