{
  "openapi": "3.1.0",
  "info": {
    "title": "Elsewhen Time API",
    "version": "1.0.0",
    "description": "Convert an instant into uncommon timekeeping systems. Canonical Hours and Time Cube use explicit coordinates when supplied and otherwise fall back to Cloudflare IP geolocation."
  },
  "servers": [
    {
      "url": "https://time.pgavlin.sh",
      "description": "Production"
    }
  ],
  "paths": {
    "/api/v1/formats": {
      "get": {
        "operationId": "listFormats",
        "summary": "List supported time formats",
        "responses": {
          "200": {
            "description": "Supported formats",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["formats"],
                  "properties": {
                    "formats": {
                      "type": "array",
                      "items": { "$ref": "#/components/schemas/Format" }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/location": {
      "get": {
        "operationId": "resolveLocation",
        "summary": "Resolve coordinates for location-dependent clocks",
        "description": "Returns explicit query coordinates when supplied; otherwise returns approximate Cloudflare IP geolocation. The visitor's IP address is neither returned nor stored.",
        "parameters": [
          { "$ref": "#/components/parameters/Latitude" },
          { "$ref": "#/components/parameters/Longitude" },
          { "$ref": "#/components/parameters/Timezone" }
        ],
        "responses": {
          "200": {
            "description": "Resolved location",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Location" }
              }
            }
          },
          "400": { "$ref": "#/components/responses/BadRequest" }
        }
      }
    },
    "/api/v1/time/{format}": {
      "get": {
        "operationId": "convertTime",
        "summary": "Convert an instant to an uncommon time format",
        "parameters": [
          {
            "name": "format",
            "in": "path",
            "required": true,
            "description": "Requested timekeeping system",
            "schema": {
              "type": "string",
              "enum": ["unix", "swatch", "republican", "canonical", "time-cube"]
            }
          },
          { "$ref": "#/components/parameters/Instant" },
          { "$ref": "#/components/parameters/Latitude" },
          { "$ref": "#/components/parameters/Longitude" },
          { "$ref": "#/components/parameters/Timezone" }
        ],
        "responses": {
          "200": {
            "description": "Converted time",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/TimeResponse" }
              }
            }
          },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "422": {
            "description": "The date or solar events cannot be represented",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorResponse" }
              }
            }
          },
          "500": {
            "description": "Conversion failed",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorResponse" }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "parameters": {
      "Instant": {
        "name": "at",
        "in": "query",
        "required": false,
        "description": "RFC 3339 instant. Defaults to the current instant.",
        "schema": { "type": "string", "format": "date-time" },
        "example": "2026-09-21T19:30:00Z"
      },
      "Latitude": {
        "name": "lat",
        "in": "query",
        "required": false,
        "description": "Latitude in decimal degrees. Must be supplied together with lon. Required by Canonical Hours when IP geolocation is unavailable.",
        "schema": { "type": "number", "minimum": -90, "maximum": 90 },
        "example": 37.7749
      },
      "Longitude": {
        "name": "lon",
        "in": "query",
        "required": false,
        "description": "Longitude in decimal degrees. Must be supplied together with lat. Required by Time Cube and Canonical Hours when IP geolocation is unavailable.",
        "schema": { "type": "number", "minimum": -180, "maximum": 180 },
        "example": -122.4194
      },
      "Timezone": {
        "name": "timezone",
        "in": "query",
        "required": false,
        "description": "IANA timezone used to select and display the local canonical day. Defaults to the Cloudflare timezone for an inferred location or UTC.",
        "schema": { "type": "string" },
        "example": "America/Los_Angeles"
      }
    },
    "responses": {
      "BadRequest": {
        "description": "Invalid timestamp, coordinates, timezone, or missing location",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/ErrorResponse" }
          }
        }
      },
      "NotFound": {
        "description": "Unknown format or API endpoint",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/ErrorResponse" }
          }
        }
      }
    },
    "schemas": {
      "Format": {
        "type": "object",
        "required": ["name", "locationRequired", "description"],
        "properties": {
          "name": {
            "type": "string",
            "enum": ["unix", "swatch", "republican", "canonical", "time-cube"]
          },
          "locationRequired": { "type": "boolean" },
          "description": { "type": "string" }
        }
      },
      "Location": {
        "type": "object",
        "required": ["latitude", "longitude", "timezone", "source"],
        "properties": {
          "latitude": { "type": "number", "minimum": -90, "maximum": 90 },
          "longitude": { "type": "number", "minimum": -180, "maximum": 180 },
          "timezone": { "type": "string" },
          "source": { "type": "string", "enum": ["query", "cloudflare"] }
        }
      },
      "TimeResponse": {
        "type": "object",
        "required": ["format", "at", "location", "result"],
        "properties": {
          "format": {
            "type": "string",
            "enum": ["unix", "swatch", "republican", "canonical", "time-cube"]
          },
          "at": { "type": "string", "format": "date-time" },
          "location": {
            "oneOf": [
              { "$ref": "#/components/schemas/Location" },
              { "type": "null" }
            ]
          },
          "result": {
            "oneOf": [
              { "$ref": "#/components/schemas/UnixResult" },
              { "$ref": "#/components/schemas/SwatchResult" },
              { "$ref": "#/components/schemas/RepublicanResult" },
              { "$ref": "#/components/schemas/CanonicalResult" },
              { "$ref": "#/components/schemas/TimeCubeResult" }
            ]
          }
        }
      },
      "UnixResult": {
        "type": "object",
        "required": ["seconds", "milliseconds", "hexadecimal", "signed32Value", "secondsUntilRollover", "rolloverAt"],
        "properties": {
          "seconds": { "type": "integer" },
          "milliseconds": { "type": "integer" },
          "hexadecimal": { "type": "string", "pattern": "^0x[0-9a-f-]+$" },
          "signed32Value": {
            "oneOf": [{ "type": "integer" }, { "type": "null" }]
          },
          "secondsUntilRollover": { "type": "integer", "minimum": 0 },
          "rolloverAt": { "type": "string", "format": "date-time" }
        }
      },
      "SwatchResult": {
        "type": "object",
        "required": ["beats", "display", "bmtDate"],
        "properties": {
          "beats": { "type": "number", "minimum": 0, "exclusiveMaximum": 1000 },
          "display": { "type": "string", "pattern": "^@[0-9]{3}\\.[0-9]{2}$" },
          "bmtDate": { "type": "string", "format": "date" }
        }
      },
      "RepublicanResult": {
        "type": "object",
        "required": ["calendar", "decimalTime", "basis"],
        "properties": {
          "calendar": { "$ref": "#/components/schemas/RepublicanDate" },
          "decimalTime": { "$ref": "#/components/schemas/DecimalTime" },
          "basis": { "const": "Europe/Paris" }
        }
      },
      "RepublicanDate": {
        "type": "object",
        "required": ["year", "romanYear", "month", "monthName", "day", "dayName", "complementary"],
        "properties": {
          "year": { "type": "integer", "minimum": 1 },
          "romanYear": { "type": "string" },
          "month": {
            "oneOf": [
              { "type": "integer", "minimum": 1, "maximum": 12 },
              { "type": "null" }
            ]
          },
          "monthName": { "type": "string" },
          "day": { "type": "integer", "minimum": 1, "maximum": 30 },
          "dayName": { "type": "string" },
          "complementary": { "type": "boolean" }
        }
      },
      "DecimalTime": {
        "type": "object",
        "required": ["hours", "minutes", "seconds", "fraction", "display"],
        "properties": {
          "hours": { "type": "integer", "minimum": 0, "maximum": 9 },
          "minutes": { "type": "integer", "minimum": 0, "maximum": 99 },
          "seconds": { "type": "integer", "minimum": 0, "maximum": 99 },
          "fraction": { "type": "number", "minimum": 0, "exclusiveMaximum": 1 },
          "display": { "type": "string", "pattern": "^[0-9]{2}:[0-9]{2}:[0-9]{2}$" }
        }
      },
      "CanonicalResult": {
        "type": "object",
        "required": ["office", "latin", "description", "nextOffice", "nextAt", "sunrise", "sunset", "daylightHourMinutes", "nightHourMinutes"],
        "properties": {
          "office": { "type": "string", "enum": ["Matins", "Lauds", "Prime", "Terce", "Sext", "None", "Vespers", "Compline"] },
          "latin": { "type": "string" },
          "description": { "type": "string" },
          "nextOffice": { "type": "string", "enum": ["Matins", "Lauds", "Prime", "Terce", "Sext", "None", "Vespers", "Compline"] },
          "nextAt": { "type": "string", "format": "date-time" },
          "sunrise": { "type": "string", "format": "date-time" },
          "sunset": { "type": "string", "format": "date-time" },
          "daylightHourMinutes": { "type": "number", "exclusiveMinimum": 0 },
          "nightHourMinutes": { "type": "number", "exclusiveMinimum": 0 }
        }
      },
      "TimeCubeResult": {
        "type": "object",
        "required": ["apparentSolarTime", "currentCorner", "days"],
        "properties": {
          "apparentSolarTime": { "type": "string", "pattern": "^[0-9]{2}:[0-9]{2}:[0-9]{2}$" },
          "currentCorner": { "$ref": "#/components/schemas/CubeCorner" },
          "days": {
            "type": "array",
            "minItems": 4,
            "maxItems": 4,
            "items": { "$ref": "#/components/schemas/CubeDay" }
          }
        }
      },
      "CubeDay": {
        "type": "object",
        "required": ["name", "number", "clock", "ageSeconds", "date"],
        "properties": {
          "name": { "$ref": "#/components/schemas/CubeCorner" },
          "number": { "type": "integer", "minimum": 1, "maximum": 4 },
          "clock": { "type": "string", "pattern": "^[0-9]{2}:[0-9]{2}:[0-9]{2}$" },
          "ageSeconds": { "type": "number", "minimum": 0, "exclusiveMaximum": 86400 },
          "date": { "type": "string", "format": "date" }
        }
      },
      "CubeCorner": {
        "type": "string",
        "enum": ["SUN-UP", "MID-DAY", "SUN-DOWN", "MID-NIGHT"]
      },
      "ErrorResponse": {
        "type": "object",
        "required": ["error"],
        "properties": {
          "error": {
            "type": "object",
            "required": ["code", "message"],
            "properties": {
              "code": {
                "type": "string",
                "enum": ["invalid_coordinate", "incomplete_coordinate", "invalid_timezone", "invalid_instant", "location_required", "unknown_format", "not_found", "unsupported_date", "solar_events_unavailable", "method_not_allowed", "internal_error"]
              },
              "message": { "type": "string" }
            }
          }
        }
      }
    }
  }
}
