# Time

The seven methods that read and move the clock of a test node: the timestamp its next blocks
get, and the real time at which it mined the latest one.

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`getGenesisTime`](#getgenesistime) | `anvil_getGenesisTime` | `bigint` | The timestamp of the genesis block, in seconds. |
| [`getLastBlockWallTime`](#getlastblockwalltime) | `anvil_getLastBlockWallTime` | `number` | The real time at which the latest block became the head, in milliseconds. |
| [`increaseTime`](#increasetime) | `anvil_increaseTime` | `number` | Moves the clock forward by a number of seconds. |
| [`setTime`](#settime) | `anvil_setTime` | `number` | Sets the clock to a timestamp. |
| [`setNextBlockTimestamp`](#setnextblocktimestamp) | `anvil_setNextBlockTimestamp` | `null` | Sets the timestamp of the next block. |
| [`setBlockTimestampInterval`](#setblocktimestampinterval) | `anvil_setBlockTimestampInterval` | `null` | Makes every block's timestamp a fixed number of seconds after the one before. |
| [`removeBlockTimestampInterval`](#removeblocktimestampinterval) | `anvil_removeBlockTimestampInterval` | `boolean` | Removes that interval. |

Each returns an [`Effect<…, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method).
Moving the clock mines nothing: the new time shows in the `timestamp` of the next block, so
mine one with [`Anvil.mine`](https://ethect.dev/rpc/anvil/mining/#mine).

## getGenesisTime

Returns the timestamp of the genesis block, in seconds.

```ts
import { Effect } from "effect";
import { Anvil, Eth } from "@ethect/core/rpc";

const age = Effect.gen(function* () {
  const genesis = yield* Anvil.getGenesisTime();
  const block = yield* Eth.getBlockByNumber("latest", false);
  return block === null ? 0n : block.timestamp - genesis;
});
```

- **JSON-RPC:** `anvil_getGenesisTime`
- **Parameters:** none.
- **Returns:** `bigint`, in seconds since the Unix epoch, like the `timestamp` of a block.

## getLastBlockWallTime

Returns the real time at which the latest block became the head, in milliseconds.

```ts
import { Effect } from "effect";
import { Anvil } from "@ethect/core/rpc";

const sinceLastBlock = Anvil.getLastBlockWallTime().pipe(
  Effect.map((milliseconds) => Date.now() - milliseconds),
);
```

- **JSON-RPC:** `anvil_getLastBlockWallTime`
- **Parameters:** none.
- **Returns:** `number`, in milliseconds since the Unix epoch. It is the time of the machine,
  not the `timestamp` of the block: moving the chain's clock does not change it.

## increaseTime

Moves the clock forward by a number of seconds.

```ts
import { Effect } from "effect";
import { Anvil, Eth } from "@ethect/core/rpc";

const timestamp = Eth.getBlockByNumber("latest", false).pipe(
  Effect.map((block) => block?.timestamp),
);

const later = Effect.gen(function* () {
  yield* Anvil.increaseTime(3600n);
  yield* Anvil.mine();
  return yield* timestamp; // at least an hour after the block before
});
```

- **JSON-RPC:** `anvil_increaseTime`
- **Parameters:** `seconds` (`bigint`).
- **Returns:** `number`. How many seconds the chain's clock is now ahead of the real one,
  negative when it is behind. It is `3600` after the first call on a fresh node and `7200`
  after a second one, or [one second less](https://ethect.dev/rpc/evm/#increasetime) on a node that has just
  started.

## setTime

Sets the clock to a timestamp.

```ts
import { Effect } from "effect";
import { Anvil, Eth } from "@ethect/core/rpc";

const timestamp = Eth.getBlockByNumber("latest", false).pipe(
  Effect.map((block) => block?.timestamp),
);

const moved = Effect.gen(function* () {
  yield* Anvil.setTime(2_000_000_000n);
  yield* Anvil.mine();
  return yield* timestamp; // 2000000000n
});
```

- **JSON-RPC:** `anvil_setTime`
- **Parameters:** `timestamp` (`bigint`), in seconds. A value above `10n ** 12n` is read as
  milliseconds: `3_000_000_000_000n` gives a block at `3000000000n`.
- **Returns:** `number`. How many seconds forward the clock moved, `0` when it moved back.
- The clock can be set before the latest block. The next block then has a `timestamp` lower
  than its parent's.

## setNextBlockTimestamp

Sets the timestamp of the next block, in seconds.

```ts
import { Effect } from "effect";
import { Anvil, Eth } from "@ethect/core/rpc";

const timestamp = Eth.getBlockByNumber("latest", false).pipe(
  Effect.map((block) => block?.timestamp),
);

const exact = Effect.gen(function* () {
  yield* Anvil.setNextBlockTimestamp(2_000_000_000n);
  yield* Anvil.mine();
  return yield* timestamp; // 2000000000n
});
```

- **JSON-RPC:** `anvil_setNextBlockTimestamp`
- **Parameters:** `timestamp` (`bigint`, in seconds).
- **Returns:** `null`.
- A timestamp lower than the latest block's fails with an `RpcError` of reason
  `InvalidParams`. One equal to it is accepted.
- The clock goes on from there: the block after it is one second later when mined right away.

## setBlockTimestampInterval

Makes every block's timestamp a fixed number of seconds after the one before, whatever the real
time between them.

```ts
import { Effect } from "effect";
import { Anvil, Eth } from "@ethect/core/rpc";

const timestamp = Eth.getBlockByNumber("latest", false).pipe(
  Effect.map((block) => block?.timestamp),
);

const spaced = Effect.gen(function* () {
  yield* Anvil.setNextBlockTimestamp(2_000_000_000n);
  yield* Anvil.setBlockTimestampInterval(10n);
  yield* Anvil.mine(3);
  return yield* timestamp; // 2000000020n
});
```

- **JSON-RPC:** `anvil_setBlockTimestampInterval`
- **Parameters:** `seconds` (`bigint`).
- **Returns:** `null`.

## removeBlockTimestampInterval

Removes the interval set by [`setBlockTimestampInterval`](#setblocktimestampinterval).

```ts
import { Effect } from "effect";
import { Anvil } from "@ethect/core/rpc";

const removed = Effect.gen(function* () {
  yield* Anvil.setBlockTimestampInterval(10n);
  const first = yield* Anvil.removeBlockTimestampInterval(); // true
  const second = yield* Anvil.removeBlockTimestampInterval(); // false
  return { first, second };
});
```

- **JSON-RPC:** `anvil_removeBlockTimestampInterval`
- **Parameters:** none.
- **Returns:** `boolean`. `true` when an interval was set, `false` when there was none.

## Behavior

- **Two clocks.** The chain's clock gives blocks their `timestamp`. It starts at the real time
  and these methods move it. `getLastBlockWallTime` reads the real one.
- **Seconds and timestamps are `bigint`s.** Every method here takes one. The two results that
  count seconds, of `increaseTime` and `setTime`, are `number`s, as is the wall time.
- **Undone by a revert.** [`Anvil.revert`](https://ethect.dev/rpc/anvil/history/#revert) also brings the
  clock back to where it was when the snapshot was taken.
