119

I searched for a standard format for using a date/time as part of a file name and was unable to come up with anything.

My question is two parts:

Is using time stamps to enforce unique in file names a poor practice?

I could get the time from the creation date and serialize the file names (file0001.bak, file0002.bak, etc) but just including the time stamp lets perform file operations such as mv 2011-01* somewhere/. Is there a downside to using this type of naming system?

The format I am using is YYYY-mm-dd_HH-MM-SS.

Is there a better format I should be using?

With this format should i be concerned with file system compatibility, str_to_date_parsing concerns, etc?

Thanks!

edit:

I might have wanted to leave out the enforce uniqueness bit since it's a single user generating backup using a cronjob (there shouldn't be any concurrency problems).

10
  • 11
    Generally, I just use YYYYmmddHHMMSS, then it can be sorted/filtered numerically or lexically.
    – Orbling
    Mar 25, 2011 at 0:09
  • 1
    I use seconds since the Epoch so I don't have to deal with time zones, leap year and day light savings. Also makes date/time math easier.
    – dietbuddha
    Mar 25, 2011 at 5:36
  • 1
    See also: serverfault.com/questions/292014/…
    – Zero3
    Jun 13, 2015 at 14:30
  • 2
    I propose a format based on ISO 8601 in a post at blog.xam.de/2016/07/… -- would make our world easier, if we could agree on a format :-) Jul 19, 2016 at 8:07
  • 1
    Considering that there is an international standard for writing date/time stamps, this question should not have been closed for being opinion based. Apr 18, 2020 at 18:02

6 Answers 6

80

You should consider ISO 8601 format (2013-04-01T13:01:02). Yes there are standards for these things. The colons and hyphens may be omitted.

The format string I usually use is %Y%m%dT%H%M%S yielding 20130401T130102. Depending on requirements I omit values from the left. In a bash script I get the date with a line like:

LOGDATE=$(date +%Y%m%dT%H%M%S)
9
  • 43
    The colons will certainly have to be omitted if you're on a Windows system, since they are illegal in a filename! Mar 25, 2011 at 2:29
  • 3
    For those using strftime, the format string is "%Y-%m-%dT%H:%M:%S%z" Dec 12, 2013 at 14:52
  • 9
    I use YYYY-MM-DD-HHMMSS (I might omit the SS in some cases). The date portion is quite readable, and the time portion is readable enough for most purposes. Jan 30, 2014 at 1:23
  • 29
    Standard or not, "20130401T1301102" is not very user friendly. My eyes hurt when I see software logfiles with these kind of timestamps in their names.
    – Zero3
    Jun 13, 2015 at 14:28
  • 3
    2019.04.01-13.01.02.JPG or similar names (eg: 190401-130102.JPG) are mush better for eyes. @Zero3 Jun 22, 2019 at 9:22
9

I searched for a standard format for using a date/time as part of a file name and was unable to come up with anything.

My question is two parts:

Is using a time stamp to enforce unique file names a poor practice?

No, it's fine.

I could get the time from the creation date and serialize the file names (file0001.bak, file0002.bak, etc)

Numbering them sequentially is more work. Think of the timestamp as an increasing but non-sequential numbering.

but just including the time stamp lets perform file operations such as mv 2011-01* somewhere/. Is there a downside to using this type of naming system?

No, it's done all the time.

The format I am using is YYYY-mm-dd_HH-MM-SS.

That's good, because they will sort in chronological order. I would lose the underscore, just because it's easier to type a hyphen.

Is there a better format I should be using?

Not really.

2
  • 4
    I'd say "Use an ISO standard time format". So, YYYY-mm-ddTHH:MM:SS (or yyyymmddTHHMMSS).
    – Vatine
    Mar 25, 2011 at 13:05
  • 3
    @Vatine Agreed, although the first variant is not advisable because it isn't cross-platform (Windows doesn't allow colons in filenames). Yet another option which maximises readability and is cross-platform is YYYY-MM-DDTHHMMSS Apr 18, 2020 at 18:11
4

The format you are using is fine but if you want uniqueness and the time has no other meaning you may have concurrency problem in your application if the application is used by multiple users in the same time and they all cause files to be created in the same folder. If you just want uniqueness you may consider generation of GUID and removing any invalid characters like curly braces and dashes and use it as the file name.

3

It depends on your application. Sometimes a timestamp like the one your described can be used. Sometimes when name collision is a concern you can use a GUID generator.

2

Using ISO 8601 format also lets you sort the files by date (presuming they all have the same prefix).

http://www.iso.org/iso/support/faqs/faqs_widely_used_standards/widely_used_standards_other/date_and_time_format.htm
http://en.wikipedia.org/wiki/ISO_8601

1
  • 5
    I think what you are trying to say is that sorting lexically ends up sorting by date. Mar 25, 2011 at 13:14
-1

The FBI has a "single" user problem of backing up 100 million criminal arrest fingerprints that they receive from all police everywhere...

...they start with the date: yyyymmdd

I don't know how they continue. I continue with hhmm and for me that does it.

Using GMT / Zulu sounds like an excellent idea for a global solution. Personally I use ET, and the FBI "personally" uses ET as well since that's where they are headquartered.

1
  • 6
    This doesn't add anything to the far more general discussion in the other posts, which actually touch upon aspects such as legal characters and sorting and uniqueness. Dec 12, 2014 at 13:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.