views:

477

answers:

6

This is a somewhat subjective question, and not very important in the big scheme of things, but something that yet annoys me regularly. There seems to be no self-evident way to put a timestamp in a file name.

Objective issue is that timestamps in file names should be sortable. But .NET sortable date formats like "s" ("yyyy-MM-ddTHH:mm:ss") and "u" ("yyyy-MM-dd HH:mm:ssZ") are not valid in file names because of ':' characters.

Other thing is that you should easily see if universal or local time is used. Practically, users seem to prefer local time to universal time.

I have mostly ended up using ISO 8601 with basic time format:

  • Local time format string "yyyy-MM-ddTHHmmsszz"
  • UTC format string "yyyy-MM-ddTHHmmssZ"

In these formats my current local time would be "2009-08-08T151800+03" and UTC "2009-08-08T121800Z"

You can also autodetect the DateTime.Kind with "K" and use "yyyy-MM-ddTHHmmssK", but then you'll have to replace the ':' characters.

Any other suggestions?

Edit: A few notes so far:

local time + time zone format "yyyy-MM-ddTHHmmsszz" is no longer sortable if multiple time zones are involved. In most cases it would make sense to drop the time zone info if it is redundant, and use UTC otherwise.

Another thing is that UTC should always be marked with 'Z', 'GMT' or 'UTC' to prevent guesswork and mistakes.

Julian dates and other stardates are cool because date arithmetic with the gregorian calendar is braindead.

+1  A: 

yyMMddHHmmss

This is because it is later easy to sort the contents of a folder ascending or decending by date alfabetically.

Inge Henriksen
Ouch... year 2100 bug? ;)
You
This is the one I normally use but with 4-digit year, and hundredths of a second where needed.
paxdiablo
Hi, You.I'll be dead then, so no worries >:o)
Inge Henriksen
I assume you have no need for dates 10 years ago either then though.
Matthew Scharley
Hi, Matthew. In my case you assume correct, sir :)
Inge Henriksen
+6  A: 

I'd use YYYY-MM-DD HHmmss for filenames, unless there is a particular need for timezones or a possible need to parse them into ISO dates; in those cases an ISO date would probably be preferrable.

Edit: Timezones shouldn't really ever be required; saving everything in UTC and letting people know that it's all UTC is more efficient than specifying the time zone of everything.

You
ouch, spaces in your filenames?
Saggi Malachi
What's wrong with spaces?
Joey
Yes, spaces are heinous. I will personally track down people who use spaces in filenames and beat them to death with a wet stick of celery :-)
paxdiablo
@Saggi; well I probably deserved that one, but seriously -- show me one system that doesn't handle filenames with spaces? From what I could gather, there were no URIs involved and thus I don't worry too much about spaces.You could of course exchange that space for something else, such as an underscore or `T`, but that's less natural when you read it.
You
I'll vote for this one (but I'd never use spaces), the reason being the 4-digit year which will put off the y2k1 problem (but not the y10k one, but who really gives a XXXX about that?).
paxdiablo
I tend to use yyyy-mm-dd-hh-mm-ss.
paxdiablo
That one makes it kinda hard to see where the date ends and the time starts, though. But I don't have a nice format which solves that, too :/
Joey
@Pax: back in 1970 they said the same for 2000! ;)
Stefano Borini
+3  A: 

I use this:

My-File--2009-12-31--23-59-59.txt
  • No spaces
  • Double dashes to separate the pieces, making each piece easy to see
  • Only one punctuation character (dash) making it easy to type
  • No timezone because for me I'm always working in my local timezone; if I needed one I'd go with UTC and append "--UTC" after the time.
RichieHindle
I do approximately the same although I *start* with the timestamp and then the 'my-file' stuff since that allows one to view the files in chronological order by just ordering the filenames alphabetically.
ChristopheD
@ChristopheD: Sure - I'd do the same in that case. For my answer, I was thinking of the case where you have several versions of several different files, and you want them grouped by filename.
RichieHindle
+1  A: 

Normally I use yyyymmdd. If further precision is needed it changes to yyyymmddhhmmss

Carlton Jenke
+1  A: 

I use unix timestamps, eg. how many seconds passed from epoch. All times in UTC. But guess you could prefix w/ timezone data if you wish.

jinzo
+3  A: 

Is there a requirement for the timestamp to be human readable? If not, you could just use DateTime.Ticks.ToString(). Very accurate, sortable and no special characters.

Dan Diplo
I tend to try and make everything human readable, if at all possible. This is one of the great values of schemes like JSON notation. Even if you don't think it needs to be human readable, you never know if you'll want it to be human readable later. Also, it tends to make for easier debugging.
Edan Maor