<p>As individual clients may have an arbitrary timeshift, the Satoshi client will use the <strong>median</strong> of its neighbors times alongs with its own time to find an offset to the local clock. This offset will then be used throughout the client wherever an accurate time is needed.</p>

CC BY-SA 3.0
2013-02-01T08:55:09.153Z
2013-02-01T08:55:09.153Z
cdecker
<p>It takes the median time of the other clients connected, but only<br>
 1. if there are at least 5, and<br>
 2. if the median time does not differ from system time by more than 70 minutes.</p>

<p>For specifics, we look at <code>AddTimeData</code>, in <a href="https://github.com/bitcoin/bitcoin/blob/master/src/timedata.cpp" rel="noreferrer"><code>timedata.cpp</code></a>.</p>

<p>Note: I have edited it down for length</p>

<pre><code>void AddTimeData(const CNetAddr&amp; ip, int64 nTime)
{
    int64 nOffsetSample = nTime - GetTime();
    // Add data
    vTimeOffsets.input(nOffsetSample);
    if (vTimeOffsets.size() &gt;= 5 &amp;&amp; vTimeOffsets.size() % 2 == 1)
    {
        int64 nMedian = vTimeOffsets.median();
        // Only let other nodes change our time by so much
        if (abs64(nMedian) &lt; 70 * 60)
        {
            nTimeOffset = nMedian;
        }
        else
        {
            nTimeOffset = 0;
        }
    }
}
</code></pre>

CC BY-SA 3.0
2015-03-08T06:20:01.390Z
Is there a possible attack on manipulating this time on a client?
abeikverdi
CC BY-SA 3.0
2015-03-08T06:28:23.013Z
@abeikverdi A full answer to that question would be too large to be contained within a single comment. It's an interesting question, though, and I encourage you to [ask it](http://bitcoin.stackexchange.com/questions/ask) as a separate question. Related: [Was a timejacking attack ever performed?](http://bitcoin.stackexchange.com/questions/3516/was-a-timejacking-attack-ever-performed/35441#35441)
Nick ODell
CC BY-SA 3.0
2015-03-08T06:37:07.693Z
Yea, that's a big question! Thanks for the link! I guess I need to read more about timejacking in Bitcoin's network
abeikverdi
CC BY-SA 3.0
2013-02-01T09:05:25.767Z
2015-03-08T18:26:22.527Z
2015-03-08T18:26:22.527Z
Nick ODell
<p>Bitcoin needs to know the current time in order to validate blocks. How does it decide that?</p>

<p>Note that I'm talking about the Satoshi client specifically.</p>

<timestamp><time>
How does Bitcoin figure out what time it is?
Nick ODell
# How does Bitcoin figure out what time it is?

(Asked by: username_0 on 2013-02-01)

Bitcoin needs to know the current time in order to validate blocks. How does it decide that?

Note that I'm talking about the Satoshi client specifically.

**Tags:** timestamp, time


---

## 2 answers

### Answer by username_0 on 2013-02-01. Score: 31 | Accepted answer

It takes the median time of the other clients connected, but only  
1. if there are at least 5, and  
2. if the median time does not differ from system time by more than 70 minutes.

For specifics, we look at `AddTimeData`, in [`timedata.cpp`](https://github.com/bitcoin/bitcoin/blob/master/src/timedata.cpp).

Note: I have edited it down for length

```
void AddTimeData(const CNetAddr& ip, int64 nTime)
{
    int64 nOffsetSample = nTime - GetTime();
    // Add data
    vTimeOffsets.input(nOffsetSample);
    if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1)
    {
        int64 nMedian = vTimeOffsets.median();
        // Only let other nodes change our time by so much
        if (abs64(nMedian) < 70 * 60)
        {
            nTimeOffset = nMedian;
        }
        else
        {
            nTimeOffset = 0;
        }
    }
}

```

> **Comments:**
> **username_2**: Is there a possible attack on manipulating this time on a client?  
> **username_0**: @username_2 A full answer to that question would be too large to be contained within a single comment. It's an interesting question, though, and I encourage you to [ask it](http://bitcoin.stackexchange.com/questions/ask) as a separate question. Related: [Was a timejacking attack ever performed?](http://bitcoin.stackexchange.com/questions/3516/was-a-timejacking-attack-ever-performed/35441#35441)  
> **username_2**: Yea, that's a big question! Thanks for the link! I guess I need to read more about timejacking in Bitcoin's network  

---

### Answer by username_1 on 2013-02-01. Score: 11

As individual clients may have an arbitrary timeshift, the Satoshi client will use the **median** of its neighbors times alongs with its own time to find an offset to the local clock. This offset will then be used throughout the client wherever an accurate time is needed.


---

