# Getting the time in each loop iteration

**URL:** https://discourse.psychopy.org/t/getting-the-time-in-each-loop-iteration/3210
**Category:** Builder
**Created:** [October 9, 2017, 9:17pm UTC](https://discourse.psychopy.org/t/getting-the-time-in-each-loop-iteration/3210 "2017-10-09T21:17:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Ashish](https://avatars.discourse-cdn.com/v4/letter/a/ea666f/32.png) [@Ashish](https://discourse.psychopy.org/u/Ashish)
#### Post date: [October 9, 2017, 9:17pm UTC](https://discourse.psychopy.org/t/getting-the-time-in-each-loop-iteration/3210/1 "2017-10-09T21:17:43Z")

</div>

Hello all,

I’m sure this is a super easy question to someone that speaks Python, but sadly I am not a member of that group. I want to include timestamps in the output CSV file for each iteration of a loop to confirm that everything is running on the right time. (My colleague is having issues with this).

I have tried the following code element:  
from psychopy import core  
clock = core.getAbsTime()

This does not put anything in my output file. How can I get the results of this call into my output file?

Thank you!

---

<div class="post-metadata">

### Author: ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)
#### Post date: [October 10, 2017, 1:49am UTC](https://discourse.psychopy.org/t/getting-the-time-in-each-loop-iteration/3210/2 "2017-10-10T01:49:14Z")

</div>

> [@Ashish](#):
>
> confirm that everything is running on the right time.

You need to define what time that is. Time in current trial? Time in overall experiment? If the latter, what is the zero point and does it matter? Currently you are using `core.getAbsTime()`, the number of seconds since Jan 1970, which might be overkill.

> [@](#):
>
> I have tried the following code element:

You need to tell us what tab in the code component any code is in (i.e. does it run at the start of the experiment, start of the trial, etc)

```
 # not necessary. Builder already imports this:
from psychopy import core

# this returns a time value in seconds, not a clock object:
clock = core.getAbsTime()

```

Note that Builder already creates a few clocks for you, which you can query the time from:

```
globalClock = core.Clock() # to track the time since experiment started

trialClock = core.Clock() # resets to zero at the start of each trial

```

You can add a time stamp to the data file like this:

```
thisExp.addData('time_at_some_point_of_interest', globalClock.getTime())`

```

but you really need to tell us more about exactly what times you want to record and when.

---

<div class="post-metadata">

### Author: ![Ashish](https://avatars.discourse-cdn.com/v4/letter/a/ea666f/32.png) [@Ashish](https://discourse.psychopy.org/u/Ashish)
#### Post date: [October 10, 2017, 2:24am UTC](https://discourse.psychopy.org/t/getting-the-time-in-each-loop-iteration/3210/3 "2017-10-10T02:24:26Z")

</div>

This is fantastic thank you very much!!!

I’m sorry I should have been more clear. The “time” in question is really the duration of each trial. My colleague has said that it is not coming out to the duration that it is supposed to be. I suspect that it is a problem in how he is measuring the timing of each trial length. The timestamps should determine whether this is correct or not.

---

<div class="post-metadata">

### Author: ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)
#### Post date: [October 10, 2017, 2:52am UTC](https://discourse.psychopy.org/t/getting-the-time-in-each-loop-iteration/3210/4 "2017-10-10T02:52:05Z")

</div>

OK, so in the `End routine` tab, put something like this:

```
thisExp.addData('trial_duration', t)

```

`t` is a time value updated by Builder on each screen refresh, starting at 0 from the first screen refresh in the trial.
