Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

share|improve this question

closed as primarily opinion-based by Martijn Pieters, Kilian Foth, MichaelT, GlenH7, gnat Dec 12 at 19:36

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

6  
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
    
@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 at 1:24
    
@Keith: Fair enough, I used "." in that case, which I think looks better than underscores: "yyyy.mmdd.hhmm" –  Mike Rosenblum Apr 2 at 21:49

6 Answers 6

up vote 15 down vote accepted

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 giving 20130401T1301102. 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)
share|improve this answer
9  
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
    
That's what I use, the colons are easy to remove because they are in fixed positions. –  Martin Beckett Mar 25 '11 at 2:38
1  
For those using strftime, the format string is "%Y-%m-%dT%H:%M:%S%z" –  mangledorf Dec 12 '13 at 14:52
    
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 at 1:23

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.

share|improve this answer

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.

share|improve this answer

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

share|improve this answer
2  
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

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.

share|improve this answer
    
I'd say "Use an ISO standard time format". So, YYYY-mm-ddTHH:MM:SS (or yyyymmddTHHMMSS). –  Vatine Mar 25 '11 at 13:05

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.

share|improve this answer
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 at 13:03

protected by gnat Dec 12 at 14:16

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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