# How to get current timestamp()? Bug, timestamp not accurate

**URL:** <https://forum.fuel.network/t/how-to-get-current-timestamp-bug-timestamp-not-accurate/6751>\
**Category:** Sway\
**Tags:** beta-1, faqs, tutorials\
**Created:** [September 29, 2024, 8:10am UTC](https://forum.fuel.network/t/how-to-get-current-timestamp-bug-timestamp-not-accurate/6751 "2024-09-29T08:10:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eesheng](https://avatars.discourse-cdn.com/v4/letter/e/2bfe46/32.png) [@eesheng](https://forum.fuel.network/u/eesheng)\
**Post date:** [September 29, 2024, 8:10am UTC](https://forum.fuel.network/t/how-to-get-current-timestamp-bug-timestamp-not-accurate/6751/1 "2024-09-29T08:10:22Z")

</div>

Currently from my test cases, the timestamp() is always the same value:

```auto
#[test]
 fn foo() {
     let current_timestamp = timestamp();
     log(current_timestamp);
 }

```

The output is always the same after running it at different timings.

```auto
Decoded log value: 4611686018427387924, log rb: 1515152261580153489

```

To get the current timestamp, I also tried and deployed the contract to a testnet, but the output value remains the same `4611686020155001000`.  
Update: It does changes but only after several minutes, it returns the value but only after every 17 minutes. The second call returned `4611686020155002000`.

To get the same contract, here is the link to [sway playground](https://www.sway-playground.org/?toolchain=testnet&transpile=false&gist=f94ac65d7c1726269fbc26d9f4ba6251):

```auto
contract;
use std::block::timestamp;
use standards::src5::{AccessError, SRC5, State};
use standards::src20::SRC20;
use standards::src3::SRC3;

 
abi MyContract {
    fn test_function() -> u64;

}   
 
impl MyContract for Contract {
    fn test_function() -> u64 {
        let time = timestamp();
        time
    }
}

```

---

<div class="post-metadata">

**Author:** ![naz3eh](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/naz3eh/32/2025_2.png) [@naz3eh](https://forum.fuel.network/u/naz3eh)\
**Post date:** [September 30, 2024, 6:39am UTC](https://forum.fuel.network/t/how-to-get-current-timestamp-bug-timestamp-not-accurate/6751/2 "2024-09-30T06:39:02Z")

</div>

hey @eesheng, the behaviour you’re experiencing with the `timestamp()` function is expected. The `timestamp()` function returns the block timestamp, which only updates when a new block is mined. This means the timestamp will remain the same until a new block is produced, which can take several minutes depending on the network’s block time. However, if you need more precise timing within the constraints of the blockchain, you can try using an oracle or an off-chain service to fetch the precise time and then send it to your contract.

Or, another approach would be combine the block timestamp with the block height to get a more granular sense of time progression. This won’t give you a more precise timestamp but can help you understand the relative time between blocks i.e. you can do something like this

```auto
impl MyContract for Contract {
    fn get_precise_time() -> (u64, u64) {
        let time = timestamp();
        let block_height = height();
        (time, block_height)
    }
}

```

You can refer to our [example](https://docs.fuel.network/docs/sway-by-example-lib/solidity/#solidity) for more details

---

<div class="post-metadata">

**Author:** ![eesheng](https://avatars.discourse-cdn.com/v4/letter/e/2bfe46/32.png) [@eesheng](https://forum.fuel.network/u/eesheng)\
**Post date:** [October 8, 2024, 1:46pm UTC](https://forum.fuel.network/t/how-to-get-current-timestamp-bug-timestamp-not-accurate/6751/3 "2024-10-08T13:46:16Z")

</div>

How long does it take to mine a block? Every second on the mainnet? If so, why deploying the contract and calling timestamp(), on swayplayground seems to not update but only after 17 minutes?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex022/uploads/fuel/original/2X/5/57d5a345cc15a64b636e0d56e042857f8a0e80b1.png) [@system](https://forum.fuel.network/u/system)\
**Post date:** [October 29, 2024, 9:47am UTC](https://forum.fuel.network/t/how-to-get-current-timestamp-bug-timestamp-not-accurate/6751/4 "2024-10-29T09:47:11Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
