Timestamp file name – Daily WTF

It’s been a minute since some bad date processing code. Wesley inherited this C# blob that exists to generate filenames with timestamps.



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) the pattern is a perfectly cromulent way to plant a wire – which is to say, completely wrong, even though it somehow communicates its intent. Even if you refuse to use the built-in pad method, writing the pad method would be good. Of course, there’s no need to do any of this because C# has date formatting tools that will generate a single-line string for you.

I have to admit though – I had to stop and ask myself “what the hell is this even doing?” It was only for a second, but I had to read the code twice.

[Advertisement]

BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Find out how!

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *