Changing the date format in the filename (%b to %m)

Hi,
by default, expInfo[‘date’] returns the date in the format %Y_%b_%d_%H%M, e.g., “2018_Sep_04_1652”, which is included in the data file names. Since this format mixes up their chronological when sorted alphabetically (for example because “Feb” comes in front of “Jan”), I would like to change it to %Y-%m-%d_%H%M, e.g., “2018-09-04_1652”, in some experiments (or even in general). What would be the best way to achieve this?

Thanks a lot for any advice.

Best
Torge

I think this is customisable in the experiment settings dialog.

Hi Michael,
thanks for your reply! Yes, expInfo[‘date’] is defined in “Experiment settings / Data / Data filename”, but I don’t know how to change the format just there. Maybe the answer is very simple, but being new to Python I don’t know it.

I’m sure it could be done somehow, but perhaps just easiest to generate your own desired date format, e.g. replace the reference to expInfo with something like this:

datetime.datetime.today().strftime('%Y-%m-%d')

You might need to put import datetime in a code component to run at the start of the experiment.

Thanks again for your reply. The code works fine in a console, but unfortunately even if I import datetime
in a code component at the top of the first routine and run the experiment, it says

NameError: name ‘datetime’ is not defined

…because in the code generated by Builder, the filename is already created in line 38, while the first code component is included in line 72. So I would probably have to change the order there manually after compiling the script.

Just out of curiosity - does it have any advantage to use %b (Sep) instead of %m (09) as a default in PsychoPy? Is it to avoid misunderstandings concerning the order of month and day when they’re returned in numbers?

Thanks again!

That’s a pain, but I guess that “begin experiment” code needs to be about there so it can refer to variables that have been entered in the info dialog box.

Rather than using datetime from the Python standard library, you could actually use the same PsychoPy function that Builder uses to automatically populate the date, but specify your own format string. This function is already imported for you, so no issues with code ordering. Here is the doc string for that data.getDateStr() function:

    Uses ``time.strftime()``_ to generate a string of the form
    2012_Apr_19_1531 for 19th April 3.31pm, 2012.
    This is often useful appended to data filenames to provide unique names.
    To include the year: getDateStr(format="%Y_%b_%d_%H%M")
    returns '2011_Mar_16_1307' depending on locale, can have unicode chars
    in month names, so utf_8_decode them
    For date in the format of the current localization, do:
        data.getDateStr(format=locale.nl_langinfo(locale.D_T_FMT))

Agreed, the numerical yyyy-mm-dd format is the recommended standard for most data science applications. Maybe it confuses some Americans due to their non-standard date traditions, but as you say, has the immense value of being sensibly sortable. The data.getDateStr() function allows you to specify whatever format you want if you call it in code. I guess it is just sort of an accident that the default format is baked in to Builder, and it might be tricky to change now?

But maybe it would be worth suggesting a pull request to change it for future releases, as it would be better practice, and compatibility-wise, people shouldn’t be upgrading PsychoPy mid-experiment any way. There would need to be changes in a couple of places in the code base:

https://github.com/psychopy/psychopy/search?q=getdatestr&unscoped_q=getdatestr

i.e. where the function should be called with a specified format string, rather than relying on the default.

Thanks again!

Rather than using datetime from the Python standard library, you could actually use the same PsychoPy function that Builder uses to automatically populate the date, but specify your own format string. This function is already imported for you, so no issues with code ordering.

Do you mean by entering the getDateStr() command in the filename field in the experiment settings? Using
u'data/%s_%s_%s' % (expName,expInfo['participant'],getDateStr(format="%Y_%b_%d_%H%M")) still returns

NameError: name 'getDateStr' is not defined

Maybe functions aren’t even evaluated there? Also, if I create a date variable in a code component, I have the same ordering problem as before because the filename is generated so early in the script.
Anyway - if I really want the new format in the filename, I can still add the respective lines at the top of the compiled script.

Agreed, the numerical yyyy-mm-dd format is the recommended standard for most data science applications. […] I guess it is just sort of an accident that the default format is baked in to Builder, and it might be tricky to change now? But maybe it would be worth suggesting a pull request to change it for future releases, as it would be better practice […]

Alright, if you think so, too, I’ll take a look at it!

Best
Torge

It should be prefixed with the data module name:

I did that in one part of my post, but not consistently, so apologies.

Right, I should have seen that.

Indeed, entering
u'data/%s_%s_%s' % (expName,expInfo['participant'], data.getDateStr(format="%Y-%m-%d-%H%M"))
in Experiment settings/Data/Data filename
gets me exactly the filenames that I wanted.

Thank you very much for your help, Michael!

1 Like