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
5  
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
add comment

6 Answers

up vote 10 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
4  
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
add comment

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
1  
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
add comment

I've seen dates used for file names and other things that needed an identifier. Unless you have very high volume and if you use hours minutes and seconds in the date/time name then you would also have a unique identifier. Another approach I've used it to use the Unix Timestamp which if I remember correctly is the number of seconds that have passed since 1/1/1970... but don't quote me on the date! lol. This works well and if you don't want people to know what you're doing it doesn't seem as obvious. But to answer your original question, you're fine to use this approach!

share|improve this answer
add comment

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
add comment

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
add comment

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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