Click here to Skip to main content
15,898,222 members
Articles / Programming Languages / XML

Fun with Enums and a Generic File Date/Time Stamper

Rate me:
Please Sign up or sign in to vote.
2.78/5 (6 votes)
4 Aug 2009CPOL 15.2K   5   4
A generic file date/time stamper.

Time for a break from SQL stuff for a little .NET. Have you ever wanted to use the integer value of an enum instead of the actual enum value? For example, here is an enum I created to map datetime formats:

C#
public enum FileDatePrecision
{
    Day = 0, Hour = 2, Minute = 4, Second = 6,  
    Secondsf1 = 7, Secondsf2 = 8, Secondsf3 = 9, 
    Secondsf4 = 10, Secondsf5 = 11, Secondsf6 = 12, 
    Secondsf7 = 13
}

Never mind the weird names for the fractional second parts, was going to use millisecond, microsecond, etc., but couldn't figure out the names for all of the fractional precisions. Each "N" value for f is the fractional precision - i.e., f1 means 1/10 of a second, f2 means 1/100, and so on.

So, why did I create the enum with non-contiguous integer values? The reason is to accommodate a simple way to generate the format mask. Here's the code that returns the date time stamp with a couple of overload methods to provide defaults. It's pretty simple, just use the enum value as the substring length in order to include the characters in the format mask needed to achieve the desired output precision.

C#
public static string BuildFileDateTimeStamp(DateTime dt, FileDatePrecision precision)
{
    // Convert the enum to it's integer representation
    int precisonSequence = (int)Enum.Parse(typeof(FileDatePrecision), 
                                           precision.ToString());
    string formattedTime = "";
    if (precisonSequence > 0)
    {
        // Format the time based on the level of precision specified in the enum
        formattedTime = "_" + dt.ToString("HHmmssfffffff").Substring(0,precisonSequence);
    }
    //to get the date
    string formattedDate = dt.ToString("yyyyMMdd");
    return formattedDate + formattedTime;
}

public static string BuildFileDateTimeStamp(DateTime dt)
{
    return BuildFileDateTimeStamp(dt, FileDatePrecision.Minute);
}

public static string BuildFileDateTimeStamp()
{
    return BuildFileDateTimeStamp(DateTime.Now);
}

So, to create a filename, for example, for "StockDownloader_20090803.LOG" for use in a trace writer, I can just do:

C#
TextWriterTraceListener myWriter = new
      TextWriterTraceListener(
      System.IO.File.CreateText("StockDownloader_" 
      + Common.Utility.BuildFileDateTimeStamp() + ".log"));
      Trace.Listeners.Add(myWriter);
This article was originally posted at http://blogs.msdn.com/microsoftbob/rss.xml

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
kenan3411-Aug-09 0:40
kenan3411-Aug-09 0:40 
GeneralMy vote of 1 Pin
aciura10-Aug-09 10:01
aciura10-Aug-09 10:01 
GeneralMy vote of 1 Pin
John Kendrick5-Aug-09 7:16
John Kendrick5-Aug-09 7:16 
GeneralEnum.Parse Pin
Steve Hansen4-Aug-09 20:33
Steve Hansen4-Aug-09 20:33 
FYI, you can just cast an enum to an int.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.