105

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).

9
  • 10
    Generally, I just use YYYYmmddHHMMSS, then it can be sorted/filtered numerically or lexically. – Orbling Mar 25 '11 at 0:09
  • I use "yyyy mmdd hhmm". My firm has global reach, so I time stamp using GMT. So as I write this, it would be "2011 0325 0245", because that's the time in London right now (and they're still on standard time at the moment). If I want to specify in local time, which is Eastern Time for me, then I'd use "yyyy mmdd hhmm ET". – Mike Rosenblum Mar 25 '11 at 2:48
  • 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 '11 at 5:36
  • 1
    See also: serverfault.com/questions/292014/… – Zero3 Jun 13 '15 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 :-) – Dr. Max Völkel Jul 19 '16 at 8:07
73

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
  • 37
    The colons will certainly have to be omitted if you're on a Windows system, since they are illegal in a filename! – Carson63000 Mar 25 '11 at 2:29
  • 2
    For those using strftime, the format string is "%Y-%m-%dT%H:%M:%S%z" – Alec Jacobson Dec 12 '13 at 14:52
  • 8
    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. – Keith Thompson Jan 30 '14 at 1:23
  • 19
    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 '15 at 14:28
  • 2
    2019.04.01-13.01.02.JPG or similar names (eg: 190401-130102.JPG) are mush better for eyes. @Zero3 – S.Serpooshan Jun 22 '19 at 9:22
6

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 '11 at 13:05
  • @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 – Jon Bentley Apr 18 '20 at 18:11
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.

3

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.

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. – Michael Kohne Mar 25 '11 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
  • 3
    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. – Martijn Pieters Dec 12 '14 at 13:03

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