CodeSOD: Timestamped File Name

This post was originally published on this site

The Daily WTF

It’s been a minute since some bad date handling code. Wesley inherited this C# blob which exists to generate timestamped filenames.

// filename format = yyyyMMddhhmmss_<bipad>.dat char c0 = ‘0’; this.m_FileName = DateTime.Now.Year.ToString() + new String(c0, 2-DateTime.Now.Month.ToString().Length) + DateTime.Now.Month.ToString() + new String(c0, 2-DateTime.Now.Day.ToString().Length) + DateTime.Now.Day.ToString() + new String(c0, 2-DateTime.Now.Hour.ToString().Length) + DateTime.Now.Hour.ToString() + new String(c0, 2-DateTime.Now.Minute.ToString().Length) + DateTime.Now.Minute.ToString() + new String(c0, 2-DateTime.Now.Second.ToString().Length) + DateTime.Now.Second.ToString() + “_” + new String(c0, 5-publication.Bipad.ToString().Length) + publication.Bipad.ToString() + “.dat”;

The new String(c0, n-myString.Length) pattern is a perfectly cromulent way to pad a string- that is to say, completely wrong, even though it somehow communicates its intent. Even if you refuse to use a built-in pad method, writing a pad method would be good. Of course, there’s no need to do any of this, as C# has date formatters that will handle generating the string for you in a single line.

Still, I have to give

To read the full article click on the 'post' link at the top.