128

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

dting
  • 1,549
  • 2
  • 9
  • 8
  • 11
    Generally, I just use YYYYmmddHHMMSS, then it can be sorted/filtered numerically or lexically. – Orbling Mar 25 '11 at 00: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 02:48
  • 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 '11 at 05:36
  • @MikeRosenblum: (Yeah, I'm resonding to a comment from almost 3 years ago.) Spaces in file names can cause problems on some systems. – Keith Thompson Jan 30 '14 at 01:24
  • @Keith: Fair enough, I used "." in that case, which I think looks better than underscores: "yyyy.mmdd.hhmm" – Mike Rosenblum Apr 02 '14 at 21:49
  • 1
    See also: https://serverfault.com/questions/292014/preferred-format-of-file-names-which-include-a-timestamp – Zero3 Jun 13 '15 at 14:30
  • Anyone copy/pasting should note that on Windows month is uppercase "M", minute is lowercase "m" and second is lowercase "s". https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx – Dan Oct 07 '15 at 14:32
  • 2
    I propose a format based on ISO 8601 in a post at http://blog.xam.de/2016/07/standard-format-for-time-stamps-in-file.html -- would make our world easier, if we could agree on a format :-) – Dr. Max Völkel Jul 19 '16 at 08: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. – Jon Bentley Apr 18 '20 at 18:02
  • For me, its important to name certain files with a complete, time-aware, filenames. I don't care for Unix Time because its only special-human readable. I prefer including either the Timezone Identifier (PST, PDT, MST, MDT, etc.) or the Timezone Offset From UTC (-0800, +0700, etc.). Without timezone information, the actual date-time stamp is +/- 24 hrs. **Example**: `2022-05-15T084500+0800.txt` – SeaDude May 15 '22 at 15:47

6 Answers6

83

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)
BillThor
  • 6,232
  • 17
  • 17
  • 49
    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 02:29
  • That's what I use, the colons are easy to remove because they are in fixed positions. – Martin Beckett Mar 25 '11 at 02:38
  • 4
    For those using strftime, the format string is `"%Y-%m-%dT%H:%M:%S%z"` – Alec Jacobson Dec 12 '13 at 14:52
  • 10
    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 01:23
  • 33
    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
  • 4
    `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 09:22
  • 1
    @S.serpooshan: I loooove this dot and dash solution! Looks good! Make it an answer, so other people can comment if this is good or not. – lode Mar 24 '20 at 18:57
  • 1
    @Zero3 You can maximise for both readability and cross-platform availability with YYYY-MM-DDTHHMMSS (e.g. 2020-04-18T191300). That leaves you with a user friendly date, and for most use cases you're not too interested in the time anyway. – Jon Bentley Apr 18 '20 at 18:14
  • 3
    @S.Serpooshan I personally like `2019-04-01_13-01-02` since non-extension dots might break poorly written scripts. EDIT: Apparently that is exactly the same format as OP. – Mateen Ulhaq Jan 02 '21 at 23:57
10

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.

kevin cline
  • 33,608
  • 3
  • 71
  • 142
  • 5
    I'd say "Use an ISO standard time format". So, YYYY-mm-ddTHH:MM:SS (or yyyymmddTHHMMSS). – Vatine Mar 25 '11 at 13:05
  • 4
    @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
5

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.

M.Sameer
  • 1,374
  • 4
  • 11
  • 20
4

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.

grokus
  • 7,536
  • 4
  • 31
  • 46
3

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

Tangurena
  • 13,294
  • 4
  • 37
  • 65
-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.

dave
  • 29
  • 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. – Martijn Pieters Dec 12 '14 at 13:03