# How can I get the start time of the stimulus?

**URL:** <https://psychtoolbox.discourse.group/t/how-can-i-get-the-start-time-of-the-stimulus/5309>\
**Category:** Programming Help\
**Created:** [March 22, 2024, 5:09am UTC](https://psychtoolbox.discourse.group/t/how-can-i-get-the-start-time-of-the-stimulus/5309 "2024-03-22T05:09:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ado](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@Ado](https://psychtoolbox.discourse.group/u/Ado)\
**Post date:** [March 22, 2024, 5:09am UTC](https://psychtoolbox.discourse.group/t/how-can-i-get-the-start-time-of-the-stimulus/5309/1 "2024-03-22T05:09:33Z")

</div>

Dear all,

I am writing a stimulus program and need to obtain the start time of the stimulus so that I can align it with the system time later.

I tested the following code in matlab:

> startTime = PsychPortAudio( 'Start’, pa, 1,0,1); % play audio and return the onset time  
> timeonset = posixtime(datetime( now))\*1000; % system time

And I found that “startTime” and “timeonset” are not the same and their intervals are not fixed. “timeonset” is a POSIX time (number of seconds elapsed since 00:00:00 1-Jan-1970 UTC) while the specific encoding format of “startTime” is not clear.

I need “startTime” to ensure the stimulation precision in time. So, do I have any methods to get the POXIS format for “startTime”?

Thanks,  
Ado

---

<div class="post-metadata">

**Author:** ![xiangrui](https://avatars.discourse-cdn.com/v4/letter/x/ec9cab/32.png) [@xiangrui](https://psychtoolbox.discourse.group/u/xiangrui)\
**Post date:** [March 22, 2024, 9:10pm UTC](https://psychtoolbox.discourse.group/t/how-can-i-get-the-start-time-of-the-stimulus/5309/2 "2024-03-22T21:10:31Z")

</div>

PTB timestamps consistently uses that returned by `GetSecs`. For MAC and Windows, GetSecs=0 typically means the time of last computer restart. You can have more information from

```
doc GetSecs

```

If you want to get the relationship between `posixtime` and `GetSecs`, following should be close to a constant:

```
posixtime(datetime('now')) - GetSecs

```

---

<div class="post-metadata">

**Author:** ![mariokleiner](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@mariokleiner](https://psychtoolbox.discourse.group/u/mariokleiner)\
**Post date:** [March 23, 2024, 6:33am UTC](https://psychtoolbox.discourse.group/t/how-can-i-get-the-start-time-of-the-stimulus/5309/3 "2024-03-23T06:33:43Z")

</div>

See `GetSecs AllClocks?`
