:::info サンプルコード
実際に動作するサンプルコードを GitHub で公開しています。[nodejs/plugin/timestamp](https://github.com/obniz/obniz/tree/master/examples/nodejs/plugin/timestamp) をご覧ください。
:::

経過時間を取得したり、obniz.jsに対して時間情報を付与してデータ送信することができます。

## cloud.enqueueTimestampByTick(tick)

`os.getTick()`を呼び出すことでobnizOSが起動から何ミリ秒経過したかを知ることができます。

通常`cloud.pluginSend("data");`のように送信してもこの`"data"`がいつのデータなのかはデータの中に時間を含めない限りわかりません。

しかし`cloud.enqueueTimestampByTick(tick)`関数をデータを送る前に付与することでtickの時刻をunix millisecondsにて付与することができます。

以下の例では3秒ごとにクラウドにデータを2つ送り、1つは1秒前のデータとしてのタイムスタンプ、もう１つは現在時刻のタイムスタンプを付与しています。



```lua
tick = 0

function on_online_loop() 
  local current = os.getTick()
  if tick == 0 then
    tick = current
  end
  if current - tick >= 3 * 1000 then
    tick = current
    os.log(" - 3 seconds passed");

    -- add old data with data retrieval timestamp
    cloud.enqueueTimestampByTick(current-1000);
    cloud.pluginSend("this is 1 sec old data");

    -- just add simple timestamp
    cloud.enqueueTimestampByTick(current);
    cloud.pluginSend("now");
  end
end
```

javascript側ではonreceiveにてデータを受信したときに`obniz.deviceTimestamp`からデータを受け取ることができます。

この時間は1970年1月1日からの経過ミリ秒となっています。

**Note**: 端末の時計を合わせない限り時間がずれるため、開始時と定期的なタイミングで`obniz.setClock()`を呼び出す必要があります。

```javascript
  obniz.setClock();
  obniz.plugin.onreceive = (data) => {
    console.log(`data "${Buffer.from(data).toString()}" is ${(Date.now() - (obniz.deviceTimestamp)) / 1000} seconds old`);
  };
```