2
\$\begingroup\$

Looking at a code review at work for a Win32 C# code I am seeing some lines like this:

int MinutesPerDay = 1440;
int MinutesPerWeek = 10080;
int MinutesPerMonth = 43200;

and so on ...

I am not sure what is wrong with it but still I feel this is not a good code and design? What do you guys think? Could he do it in a better way?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ It should be noted that these values are only valid in timezones which do not participate in Daylight Savings Time. MinutesPerMonth is of course approxiamate, which may not be quite what you want. \$\endgroup\$ Commented Apr 26, 2012 at 17:08

4 Answers 4

6
\$\begingroup\$

There is a class called "TimeSpan", you can use that

TimeSpan interval = TimeSpan.FromDays(1);
double seconds = interval.TotalSeconds;

I don't consider that better from a coding stand point. You should probably look at your code and figure out why you are storing those values, and find a better way.

\$\endgroup\$
10
\$\begingroup\$

Since these are constants, I would declare them as such. Also, making the calculation explicit would be more informative.

private const int MinutesPerDay = 24 * 60;
private const int MinutesPerWeek = 7 * MinutesPerDay;
private const int MinutesPerMonth = 30 * MinutesPerDay;
\$\endgroup\$
4
  • \$\begingroup\$ Because these values are constant by definition, I accept the use of constants here. However, it should be noted that .NET constants are tricky, because the value is copied to the manifest of any other assembly that uses it, requiring a recompile of those assemblies should the value ever change (such as to increase precision of a decimal "constant" like pi, e, Planck's constant, etc). These values, however, are so extremely unlikely to ever change that this isn't a problem. \$\endgroup\$
    – KeithS
    Commented Apr 26, 2012 at 17:00
  • \$\begingroup\$ @KeithS: Yes. In scenarios where this could be a problem, you could declare them as static readonly fields. By the way, they are not declared as public here. \$\endgroup\$ Commented Apr 26, 2012 at 17:08
  • 2
    \$\begingroup\$ I would spell out the private in front of const int. StyleCop would ask you to. \$\endgroup\$
    – Leonid
    Commented Apr 26, 2012 at 20:29
  • 1
    \$\begingroup\$ @Leonid: Yes, you are probably right; however, the context is not clear here, this code could be local to a method. \$\endgroup\$ Commented Apr 28, 2012 at 15:21
3
\$\begingroup\$

The closest thing .NET has is TimeSpan.TicksPerDay

Honestly, using ticks for your time constants is generally a good way to go, because ticks are the root value of DateTime and TimeSpan, and it makes it a little more efficient to compare Time values without having to create new instances of anything by simply comparing the Ticks of either your DateTime or TimeSpan to whatever Ticks constant you need to (be it .NET's or yours).

\$\endgroup\$
3
\$\begingroup\$

I have ran into this just last week. I wish TimeSpan had a few more From methods for bigger time periods like weeks months and years, but I made due with these:

    /// <summary>
    /// Number of minutes in an hour.
    /// </summary>
    private static readonly double minutesInHour = TimeSpan.FromHours(1).TotalMinutes;

    /// <summary>
    /// Number of hours in a day.
    /// </summary>
    private static readonly double hoursInDay = TimeSpan.FromDays(1).TotalHours;

    /// <summary>
    /// Number of hours in a week (7 days).
    /// </summary>
    private static readonly double hoursInWeek = 7.0D * hoursInDay;

    /// <summary>
    /// Number of hours in a month (30 days).
    /// </summary>
    private static readonly double hoursInMonth = 30.0D * hoursInDay;

The reason they're doubles are to do multiplication and division calculations later on with user input that could be in a different "base" unit.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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