🕒

現在時刻のタイムスタンプを返すAPIを作った

に公開

ベースURLは https://public-api.siyukatu.com/ です。

エンドポイント一覧

/time.txt

現在時刻のUNIXタイムスタンプをテキスト形式で返します。

レスポンス

Content-Type: text/vnd.sykt-api のヘッダーを持ち、テキストデータが返却されます。

応答例

1234567890123

使用例

fetch("https://public-api.siyukatu.com/time.txt")
  .then(response => response.text())
  .then(time => {
    console.log(time);
  });

/time.json

現在時刻のUNIXタイムスタンプをJSON形式で返します。

レスポンス

Content-Type: application/json のヘッダーを持ち、以下の構造のJSONデータが返却されます。

構造
  • time: 現在時刻のUNIXタイムスタンプ

応答例

{"time":1234567890123}

使用例

fetch("https://public-api.siyukatu.com/time.json")
  .then(response => response.json())
  .then(data => {
    console.log(data.time);
  });

/time.css

現在時刻をCSSカスタムプロパティとして返します。
このエンドポイントは :root--timestamp を設定します。
CSS として読み込むと、ページ内で var(--timestamp) を参照できる形になります。

レスポンス

:root{--timestamp:1234567890123}

使用例

https://css-clock.siyukatu.me
※外部リンク

/time/events

Server-Sent Events (SSE) で現在時刻のUNIXタイムスタンプを定期的に送信します。

クエリパラメータ

  • interval (任意)
    送信頻度をミリ秒単位で指定します。 (送信頻度一覧)
    省略時は 500 が使用されます。

送信イベント形式

event: time
data: 1234567890123

応答例

event: time
data: 1234567890123

event: time
data: 1234567890500

event: time
data: 1234567891000

使用例

const sse = new EventSource("https://public-api.siyukatu.com/time/events?interval=1000");

sse.addEventListener("time", (event) => {
  console.log(Number(event.data));
});

/time/socket (WebSocket)

WebSocketで現在時刻のUNIXタイムスタンプを定期送信します。

クエリパラメータ

  • interval (任意)
    送信頻度をミリ秒単位で指定します。 (送信頻度一覧)
    省略時は 500 が使用されます。

初期メッセージ

接続成功後、最初に以下が送信されます。

{"type":"connected"}
{"type":"time","time":1234567890123}

定期送信メッセージ

以後、指定間隔ごとに現在時刻が送信されます。

{"type":"time","time":1234567890500}
{"type":"time","time":1234567891000}

Ping / Pong

クライアントから ping を送信するとサーバーから pong が返信されます。

送信例
{"type":"ping","id":123}
返信例
{"type":"pong","reply_to":123,"time":1234567890123}

使用例

https://siyukatu.me/time
※外部リンク

const ws = new WebSocket("wss://public-api.siyukatu.com/time/socket?interval=1000");

ws.onmessage = (event) => {
  const data = JSON.parse(event.data);
  if (data.type === "time") {
    console.log(data.time);
  }
  if (data.type === "pong") {
    console.log("Pong!", data.reply_to);
  }
};

ws.onopen = () => {
  ws.send(JSON.stringify({ type: "ping", id: 1 }));
};

指定可能な送信頻度の一覧

  • 50
  • 100
  • 200
  • 250
  • 500
  • 1000
  • 2000
  • 5000

Discussion