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

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

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

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

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

    Библиотека · std.date

    Date.month

    Read the calendar month from 1 through 12.

    month() -> int
    Модуль
    std.date →
    Получатель
    Date
    Минимальное число аргументов
    0
    Максимальное число аргументов
    0

    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 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.

    Результат

    The declared result type is int.

    Эффекты

    Ошибки

    Result and effects

    Ссылка

    This call returns the receiver month and leaves the date unchanged.

    Resolved contracts

    Ссылка

    Related entries

    Ссылка

    std.date →

    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

    Меню

    Загрузка →