{
  "esm": "1.2.0",
  "metadata": {
    "name": "Calendar",
    "description": "Standard-library calendar templates (esm-spec §9.7.1 template-library file), generated by scripts/gen-lib-calendar.py. Ten public match-less expression templates of one parameter `t`, UTC seconds since 1970-01-01T00:00:00Z (binary64, proleptic Gregorian, no leap seconds): the pure-AST definitions of the datetime.* closed functions (esm-spec §9.2) `year(t)`, `month(t)`, `day(t)`, `hour(t)`, `minute(t)`, `second(t)`, `day_of_year(t)`, `julian_day(t)` and `is_leap_year(t)`, and `seconds_since_midnight(t)`. Each splits t exactly into the day number d = floor(floor(t) / 86400) and the whole second of the day s = floor(t) - 86400 d, then runs Howard Hinnant's civil_from_days, every integer division written floor(a / b); each step is a `civil_*` helper template the others call by reference (esm-spec §9.7.3). On the domain |t| < 2^52 s every intermediate is an integer below 2^53, so each floor is exact and each datetime field equals the closed function's value. Outside that domain, and for NaN or an infinite t, every public template returns NaN. The second-valued constants are unit-bearing `const` nodes (esm-spec §4.8.5), so t is read in seconds; the datetime fields are dimensionless and seconds_since_midnight is in seconds. Import with `expression_template_imports` and invoke with `apply_expression_template`; see docs/content/standard_library.md.",
    "authors": ["EarthSciML"],
    "created": "2026-04-25T00:00:00Z",
    "tags": ["standard-library", "calendar", "datetime", "expression-templates"],
    "references": [
      {
        "citation": "esm-spec.md §9.2 (datetime.* semantics, the pinned julian_day formula); §9.6 (expression templates); §9.7.1 (template-library files); §9.7.3 (template references in bodies); §4.8.5 (unit-bearing const)."
      },
      {
        "citation": "Hinnant, H., 2013. chrono-Compatible Low-Level Date Algorithms, `civil_from_days`. https://howardhinnant.github.io/date_algorithms.html"
      }
    ]
  },
  "expression_templates": {
    "year": {
      "params": ["t"],
      "description": "Proleptic-Gregorian year of the UTC instant t (seconds since 1970-01-01T00:00:00Z), astronomical numbering (0 = 1 BC), the value datetime.year gives (esm-spec §9.2): yoe + 400 era, plus one in January and February (Howard Hinnant's civil_from_days). Finite t with |t| < 2^52 s; outside it, and for NaN or an infinite t, the value is NaN (0/0).",
      "body": {
        "op": "ifelse",
        "args": [
          {
            "op": "<",
            "args": [{"op": "abs", "args": ["t"]}, {"op": "const", "args": [], "value": 4503599627370496, "units": "s"}]
          },
          {
            "op": "+",
            "args": [
              {
                "op": "+",
                "args": [
                  {"op": "apply_expression_template", "args": [], "name": "civil_year_of_era", "bindings": {"t": "t"}},
                  {
                    "op": "*",
                    "args": [400, {"op": "apply_expression_template", "args": [], "name": "civil_era", "bindings": {"t": "t"}}]
                  }
                ]
              },
              {
                "op": "ifelse",
                "args": [
                  {"op": "apply_expression_template", "args": [], "name": "civil_is_jan_feb", "bindings": {"t": "t"}},
                  1,
                  0
                ]
              }
            ]
          },
          {"op": "/", "args": [0, 0]}
        ]
      }
    },
    "month": {
      "params": ["t"],
      "description": "Calendar month 1..12 of the UTC instant t, the value datetime.month gives (esm-spec §9.2): mp + 3 for mp < 10 and mp - 9 otherwise, mp being the month counted from March. Finite t with |t| < 2^52 s; outside it, and for NaN or an infinite t, the value is NaN (0/0).",
      "body": {
        "op": "ifelse",
        "args": [
          {
            "op": "<",
            "args": [{"op": "abs", "args": ["t"]}, {"op": "const", "args": [], "value": 4503599627370496, "units": "s"}]
          },
          {
            "op": "+",
            "args": [
              {"op": "apply_expression_template", "args": [], "name": "civil_month_index", "bindings": {"t": "t"}},
              {
                "op": "ifelse",
                "args": [
                  {
                    "op": "<",
                    "args": [
                      {"op": "apply_expression_template", "args": [], "name": "civil_month_index", "bindings": {"t": "t"}},
                      10
                    ]
                  },
                  3,
                  -9
                ]
              }
            ]
          },
          {"op": "/", "args": [0, 0]}
        ]
      }
    },
    "day": {
      "params": ["t"],
      "description": "Day of month 1..31 of the UTC instant t, the value datetime.day gives (esm-spec §9.2): doy - floor((153 mp + 2) / 5) + 1. Finite t with |t| < 2^52 s; outside it, and for NaN or an infinite t, the value is NaN (0/0).",
      "body": {
        "op": "ifelse",
        "args": [
          {
            "op": "<",
            "args": [{"op": "abs", "args": ["t"]}, {"op": "const", "args": [], "value": 4503599627370496, "units": "s"}]
          },
          {
            "op": "+",
            "args": [
              {
                "op": "-",
                "args": [
                  {"op": "apply_expression_template", "args": [], "name": "civil_day_of_year", "bindings": {"t": "t"}},
                  {
                    "op": "floor",
                    "args": [
                      {
                        "op": "/",
                        "args": [
                          {
                            "op": "+",
                            "args": [
                              {
                                "op": "*",
                                "args": [
                                  153,
                                  {"op": "apply_expression_template", "args": [], "name": "civil_month_index", "bindings": {"t": "t"}}
                                ]
                              },
                              2
                            ]
                          },
                          5
                        ]
                      }
                    ]
                  }
                ]
              },
              1
            ]
          },
          {"op": "/", "args": [0, 0]}
        ]
      }
    },
    "hour": {
      "params": ["t"],
      "description": "UTC hour 0..23 of t, the value datetime.hour gives (esm-spec §9.2): floor(s / 3600), s being the whole second of the day. Finite t with |t| < 2^52 s; outside it, and for NaN or an infinite t, the value is NaN (0/0).",
      "body": {
        "op": "ifelse",
        "args": [
          {
            "op": "<",
            "args": [{"op": "abs", "args": ["t"]}, {"op": "const", "args": [], "value": 4503599627370496, "units": "s"}]
          },
          {
            "op": "floor",
            "args": [
              {
                "op": "/",
                "args": [
                  {
                    "op": "/",
                    "args": [
                      {
                        "op": "apply_expression_template",
                        "args": [],
                        "name": "civil_second_of_day",
                        "bindings": {"t": "t"}
                      },
                      {"op": "const", "args": [], "value": 1, "units": "s"}
                    ]
                  },
                  3600
                ]
              }
            ]
          },
          {"op": "/", "args": [0, 0]}
        ]
      }
    },
    "minute": {
      "params": ["t"],
      "description": "UTC minute 0..59 of t, the value datetime.minute gives (esm-spec §9.2): floor(s / 60) - 60 floor(s / 3600). Finite t with |t| < 2^52 s; outside it, and for NaN or an infinite t, the value is NaN (0/0).",
      "body": {
        "op": "ifelse",
        "args": [
          {
            "op": "<",
            "args": [{"op": "abs", "args": ["t"]}, {"op": "const", "args": [], "value": 4503599627370496, "units": "s"}]
          },
          {
            "op": "-",
            "args": [
              {
                "op": "floor",
                "args": [
                  {
                    "op": "/",
                    "args": [
                      {
                        "op": "/",
                        "args": [
                          {
                            "op": "apply_expression_template",
                            "args": [],
                            "name": "civil_second_of_day",
                            "bindings": {"t": "t"}
                          },
                          {"op": "const", "args": [], "value": 1, "units": "s"}
                        ]
                      },
                      60
                    ]
                  }
                ]
              },
              {
                "op": "*",
                "args": [
                  60,
                  {
                    "op": "floor",
                    "args": [
                      {
                        "op": "/",
                        "args": [
                          {
                            "op": "/",
                            "args": [
                              {
                                "op": "apply_expression_template",
                                "args": [],
                                "name": "civil_second_of_day",
                                "bindings": {"t": "t"}
                              },
                              {"op": "const", "args": [], "value": 1, "units": "s"}
                            ]
                          },
                          3600
                        ]
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {"op": "/", "args": [0, 0]}
        ]
      }
    },
    "second": {
      "params": ["t"],
      "description": "UTC second 0..59 of t (no leap-second slot), the value datetime.second gives (esm-spec §9.2): s - 60 floor(s / 60); a fractional t floors toward minus infinity. Finite t with |t| < 2^52 s; outside it, and for NaN or an infinite t, the value is NaN (0/0).",
      "body": {
        "op": "ifelse",
        "args": [
          {
            "op": "<",
            "args": [{"op": "abs", "args": ["t"]}, {"op": "const", "args": [], "value": 4503599627370496, "units": "s"}]
          },
          {
            "op": "-",
            "args": [
              {
                "op": "/",
                "args": [
                  {
                    "op": "apply_expression_template",
                    "args": [],
                    "name": "civil_second_of_day",
                    "bindings": {"t": "t"}
                  },
                  {"op": "const", "args": [], "value": 1, "units": "s"}
                ]
              },
              {
                "op": "*",
                "args": [
                  60,
                  {
                    "op": "floor",
                    "args": [
                      {
                        "op": "/",
                        "args": [
                          {
                            "op": "/",
                            "args": [
                              {
                                "op": "apply_expression_template",
                                "args": [],
                                "name": "civil_second_of_day",
                                "bindings": {"t": "t"}
                              },
                              {"op": "const", "args": [], "value": 1, "units": "s"}
                            ]
                          },
                          60
                        ]
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {"op": "/", "args": [0, 0]}
        ]
      }
    },
    "day_of_year": {
      "params": ["t"],
      "description": "Day of the year 1..366 of the UTC instant t (1 = January 1), the value datetime.day_of_year gives (esm-spec §9.2): doy - 305 in January and February, otherwise doy + 60 plus one in a leap year. Finite t with |t| < 2^52 s; outside it, and for NaN or an infinite t, the value is NaN (0/0).",
      "body": {
        "op": "ifelse",
        "args": [
          {
            "op": "<",
            "args": [{"op": "abs", "args": ["t"]}, {"op": "const", "args": [], "value": 4503599627370496, "units": "s"}]
          },
          {
            "op": "+",
            "args": [
              {
                "op": "+",
                "args": [
                  {"op": "apply_expression_template", "args": [], "name": "civil_day_of_year", "bindings": {"t": "t"}},
                  60
                ]
              },
              {
                "op": "ifelse",
                "args": [
                  {"op": "apply_expression_template", "args": [], "name": "civil_is_jan_feb", "bindings": {"t": "t"}},
                  -365,
                  {
                    "op": "apply_expression_template",
                    "args": [],
                    "name": "civil_leap",
                    "bindings": {
                      "x": {"op": "apply_expression_template", "args": [], "name": "civil_year_of_era", "bindings": {"t": "t"}}
                    }
                  }
                ]
              }
            ]
          },
          {"op": "/", "args": [0, 0]}
        ]
      }
    },
    "julian_day": {
      "params": ["t"],
      "description": "Julian Date of the UTC instant t, exactly esm-spec §9.2's pinned binary64 formula (d + 2440588) + ((s - 43200) + (t - floor(t))) / 86400, evaluated in that order. Finite t with |t| < 2^52 s; outside it, and for NaN or an infinite t, the value is NaN (0/0).",
      "body": {
        "op": "ifelse",
        "args": [
          {
            "op": "<",
            "args": [{"op": "abs", "args": ["t"]}, {"op": "const", "args": [], "value": 4503599627370496, "units": "s"}]
          },
          {
            "op": "+",
            "args": [
              {
                "op": "+",
                "args": [
                  {"op": "apply_expression_template", "args": [], "name": "civil_day_number", "bindings": {"t": "t"}},
                  2440588
                ]
              },
              {
                "op": "/",
                "args": [
                  {
                    "op": "+",
                    "args": [
                      {
                        "op": "-",
                        "args": [
                          {
                            "op": "apply_expression_template",
                            "args": [],
                            "name": "civil_second_of_day",
                            "bindings": {"t": "t"}
                          },
                          {"op": "const", "args": [], "value": 43200, "units": "s"}
                        ]
                      },
                      {"op": "-", "args": ["t", {"op": "floor", "args": ["t"]}]}
                    ]
                  },
                  {"op": "const", "args": [], "value": 86400, "units": "s"}
                ]
              }
            ]
          },
          {"op": "/", "args": [0, 0]}
        ]
      }
    },
    "is_leap_year": {
      "params": ["t"],
      "description": "1 if the proleptic-Gregorian year of the UTC instant t is a leap year (divisible by 4, and not by 100 unless by 400), else 0, the value datetime.is_leap_year gives (esm-spec §9.2). The year is congruent modulo 400 to yoe from March to December and to yoe + 1 in January and February. Finite t with |t| < 2^52 s; outside it, and for NaN or an infinite t, the value is NaN (0/0).",
      "body": {
        "op": "ifelse",
        "args": [
          {
            "op": "<",
            "args": [{"op": "abs", "args": ["t"]}, {"op": "const", "args": [], "value": 4503599627370496, "units": "s"}]
          },
          {
            "op": "ifelse",
            "args": [
              {"op": "apply_expression_template", "args": [], "name": "civil_is_jan_feb", "bindings": {"t": "t"}},
              {
                "op": "apply_expression_template",
                "args": [],
                "name": "civil_leap_next",
                "bindings": {
                  "x": {"op": "apply_expression_template", "args": [], "name": "civil_year_of_era", "bindings": {"t": "t"}}
                }
              },
              {
                "op": "apply_expression_template",
                "args": [],
                "name": "civil_leap",
                "bindings": {
                  "x": {"op": "apply_expression_template", "args": [], "name": "civil_year_of_era", "bindings": {"t": "t"}}
                }
              }
            ]
          },
          {"op": "/", "args": [0, 0]}
        ]
      }
    },
    "seconds_since_midnight": {
      "params": ["t"],
      "description": "Seconds elapsed since the most recent UTC midnight, 0..86399 s, in seconds: floor(t) - 86400 d, the whole second of the day (integer-valued; a fractional t floors toward minus infinity). Finite t with |t| < 2^52 s; outside it, and for NaN or an infinite t, the value is NaN (0/0).",
      "body": {
        "op": "ifelse",
        "args": [
          {
            "op": "<",
            "args": [{"op": "abs", "args": ["t"]}, {"op": "const", "args": [], "value": 4503599627370496, "units": "s"}]
          },
          {
            "op": "apply_expression_template",
            "args": [],
            "name": "civil_second_of_day",
            "bindings": {"t": "t"}
          },
          {"op": "/", "args": [0, 0]}
        ]
      }
    },
    "civil_day_number": {
      "params": ["t"],
      "description": "Day number d = floor(floor(t) / 86400) of the UTC instant t: days since 1970-01-01, exact. A step of the public templates, not guarded: meaningful for finite |t| < 2^52 s, where it is exact.",
      "body": {
        "op": "floor",
        "args": [
          {
            "op": "/",
            "args": [{"op": "floor", "args": ["t"]}, {"op": "const", "args": [], "value": 86400, "units": "s"}]
          }
        ]
      }
    },
    "civil_second_of_day": {
      "params": ["t"],
      "description": "Whole second of the day s = floor(t) - 86400 d, in seconds, in [0, 86399]. A step of the public templates, not guarded: meaningful for finite |t| < 2^52 s, where it is exact.",
      "body": {
        "op": "-",
        "args": [
          {"op": "floor", "args": ["t"]},
          {
            "op": "*",
            "args": [
              {"op": "const", "args": [], "value": 86400, "units": "s"},
              {"op": "apply_expression_template", "args": [], "name": "civil_day_number", "bindings": {"t": "t"}}
            ]
          }
        ]
      }
    },
    "civil_era": {
      "params": ["t"],
      "description": "The 400-year era of the date, counted from 0000-03-01: floor((d + 719468) / 146097). A step of the public templates, not guarded: meaningful for finite |t| < 2^52 s, where it is exact.",
      "body": {
        "op": "floor",
        "args": [
          {
            "op": "/",
            "args": [
              {
                "op": "+",
                "args": [
                  {"op": "apply_expression_template", "args": [], "name": "civil_day_number", "bindings": {"t": "t"}},
                  719468
                ]
              },
              146097
            ]
          }
        ]
      }
    },
    "civil_day_of_era": {
      "params": ["t"],
      "description": "Day of the era doe = (d + 719468) - 146097 era, in [0, 146096]. A step of the public templates, not guarded: meaningful for finite |t| < 2^52 s, where it is exact.",
      "body": {
        "op": "-",
        "args": [
          {
            "op": "+",
            "args": [
              {"op": "apply_expression_template", "args": [], "name": "civil_day_number", "bindings": {"t": "t"}},
              719468
            ]
          },
          {
            "op": "*",
            "args": [
              146097,
              {"op": "apply_expression_template", "args": [], "name": "civil_era", "bindings": {"t": "t"}}
            ]
          }
        ]
      }
    },
    "civil_year_of_era": {
      "params": ["t"],
      "description": "Year of the era yoe = floor((doe - floor(doe/1460) + floor(doe/36524) - floor(doe/146096)) / 365), in [0, 399]; the years begin on March 1. A step of the public templates, not guarded: meaningful for finite |t| < 2^52 s, where it is exact.",
      "body": {
        "op": "floor",
        "args": [
          {
            "op": "/",
            "args": [
              {
                "op": "-",
                "args": [
                  {
                    "op": "+",
                    "args": [
                      {
                        "op": "-",
                        "args": [
                          {"op": "apply_expression_template", "args": [], "name": "civil_day_of_era", "bindings": {"t": "t"}},
                          {
                            "op": "floor",
                            "args": [
                              {
                                "op": "/",
                                "args": [
                                  {"op": "apply_expression_template", "args": [], "name": "civil_day_of_era", "bindings": {"t": "t"}},
                                  1460
                                ]
                              }
                            ]
                          }
                        ]
                      },
                      {
                        "op": "floor",
                        "args": [
                          {
                            "op": "/",
                            "args": [
                              {"op": "apply_expression_template", "args": [], "name": "civil_day_of_era", "bindings": {"t": "t"}},
                              36524
                            ]
                          }
                        ]
                      }
                    ]
                  },
                  {
                    "op": "floor",
                    "args": [
                      {
                        "op": "/",
                        "args": [
                          {"op": "apply_expression_template", "args": [], "name": "civil_day_of_era", "bindings": {"t": "t"}},
                          146096
                        ]
                      }
                    ]
                  }
                ]
              },
              365
            ]
          }
        ]
      }
    },
    "civil_day_of_year": {
      "params": ["t"],
      "description": "Day of the year counted from March 1, doy = doe - (365 yoe + floor(yoe/4) - floor(yoe/100)), in [0, 365]. A step of the public templates, not guarded: meaningful for finite |t| < 2^52 s, where it is exact.",
      "body": {
        "op": "-",
        "args": [
          {"op": "apply_expression_template", "args": [], "name": "civil_day_of_era", "bindings": {"t": "t"}},
          {
            "op": "-",
            "args": [
              {
                "op": "+",
                "args": [
                  {
                    "op": "*",
                    "args": [
                      365,
                      {"op": "apply_expression_template", "args": [], "name": "civil_year_of_era", "bindings": {"t": "t"}}
                    ]
                  },
                  {
                    "op": "floor",
                    "args": [
                      {
                        "op": "/",
                        "args": [
                          {"op": "apply_expression_template", "args": [], "name": "civil_year_of_era", "bindings": {"t": "t"}},
                          4
                        ]
                      }
                    ]
                  }
                ]
              },
              {
                "op": "floor",
                "args": [
                  {
                    "op": "/",
                    "args": [
                      {"op": "apply_expression_template", "args": [], "name": "civil_year_of_era", "bindings": {"t": "t"}},
                      100
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    },
    "civil_month_index": {
      "params": ["t"],
      "description": "Month counted from March, mp = floor((5 doy + 2) / 153), in [0, 11]. A step of the public templates, not guarded: meaningful for finite |t| < 2^52 s, where it is exact.",
      "body": {
        "op": "floor",
        "args": [
          {
            "op": "/",
            "args": [
              {
                "op": "+",
                "args": [
                  {
                    "op": "*",
                    "args": [
                      5,
                      {"op": "apply_expression_template", "args": [], "name": "civil_day_of_year", "bindings": {"t": "t"}}
                    ]
                  },
                  2
                ]
              },
              153
            ]
          }
        ]
      }
    },
    "civil_is_jan_feb": {
      "params": ["t"],
      "description": "True in January and February (doy >= 306, March 1 + 306 days), which belong to the year after the year of era. A step of the public templates, not guarded: meaningful for finite |t| < 2^52 s, where it is exact.",
      "body": {
        "op": ">=",
        "args": [
          {"op": "apply_expression_template", "args": [], "name": "civil_day_of_year", "bindings": {"t": "t"}},
          306
        ]
      }
    },
    "civil_leap": {
      "params": ["x"],
      "description": "1 if a year congruent modulo 400 to x, for x in [0, 399], is a leap year, else 0 (divisible by 400 only at x = 0). A step of the public templates, not guarded: meaningful for finite |t| < 2^52 s, where it is exact.",
      "body": {
        "op": "ifelse",
        "args": [
          {
            "op": "and",
            "args": [
              {
                "op": "==",
                "args": [
                  {
                    "op": "-",
                    "args": ["x", {"op": "*", "args": [4, {"op": "floor", "args": [{"op": "/", "args": ["x", 4]}]}]}]
                  },
                  0
                ]
              },
              {
                "op": "or",
                "args": [
                  {
                    "op": "not",
                    "args": [
                      {
                        "op": "==",
                        "args": [
                          {
                            "op": "-",
                            "args": ["x", {"op": "*", "args": [100, {"op": "floor", "args": [{"op": "/", "args": ["x", 100]}]}]}]
                          },
                          0
                        ]
                      }
                    ]
                  },
                  {"op": "==", "args": ["x", 0]}
                ]
              }
            ]
          },
          1,
          0
        ]
      }
    },
    "civil_leap_next": {
      "params": ["x"],
      "description": "1 if the year after a year congruent modulo 400 to x, for x in [0, 399], is a leap year, else 0 (divisible by 400 only for x = 399). A step of the public templates, not guarded: meaningful for finite |t| < 2^52 s, where it is exact.",
      "body": {
        "op": "ifelse",
        "args": [
          {
            "op": "and",
            "args": [
              {
                "op": "==",
                "args": [
                  {
                    "op": "-",
                    "args": [
                      {"op": "+", "args": ["x", 1]},
                      {
                        "op": "*",
                        "args": [4, {"op": "floor", "args": [{"op": "/", "args": [{"op": "+", "args": ["x", 1]}, 4]}]}]
                      }
                    ]
                  },
                  0
                ]
              },
              {
                "op": "or",
                "args": [
                  {
                    "op": "not",
                    "args": [
                      {
                        "op": "==",
                        "args": [
                          {
                            "op": "-",
                            "args": [
                              {"op": "+", "args": ["x", 1]},
                              {
                                "op": "*",
                                "args": [100, {"op": "floor", "args": [{"op": "/", "args": [{"op": "+", "args": ["x", 1]}, 100]}]}]
                              }
                            ]
                          },
                          0
                        ]
                      }
                    ]
                  },
                  {"op": "==", "args": ["x", 399]}
                ]
              }
            ]
          },
          1,
          0
        ]
      }
    }
  }
}
