I'm reading Fowler Clean Code book and I think that my code is a little messy, I want some suggestions:
I have a simple business requirement that is return the date of new execution of my Thread.
I've two class fields: _hour and _day.
If actual day is higher than my _day field I must return true, so I'll add a month to "executionDate"
If the day is the same, but the actual hour is higher than _hour I should return true too.
So I did this simple method:
private bool ScheduledDateGreaterThanCurrentDate (DateTime dataAtual) {
if (dateActual.Day > _day) {
return true;
}
if (dateActual.Day == _day && dateActual.Hour > _hour) {
return true;
}
if (dateActual.Day == _day && dateActual.Hour == _hour)
if (dateActual.Minute>0 || dateActual.Second>0)
return true;
return false;
}
I'm programming with TDD, so I know that the return is correct, but this is bad maintain code right?
var compareDate = new DateTime(
dateActual.Year,
dateActual.Month,
_day,
_hour,
0,
0);
return dateActual> compareDate;
DateTime objects can be compared against one another.
For example, say my class has this field:
// Using DateTime.Today as an example
DateTime _date = DateTime.Today;
I can then do this instead of a method call
if (dataAtual > _date) {
// logic here
}
If you use the datetime object in .NET it will do all this logic for you.
Related
I have a 'simple' requirement - a quick check to see if an object is a valid DateTime and, if not, return a known default date.
There are a number of alternative solution:
Use the VB function directly by adding a reference to the VB library - simple but adding an additional library to the solution feels like bloat
Using DateTime.TryParse() - need to convert to object to string first and don't need the result
try catch block around Convert.ToDateTime() - feels clumsy but simple
They each have different pros and cons and they all have overheads - have a missed a simpler solution
Use the is operator:
Object foo = new DateTime();
if( foo is DateTime dt )
{
Console.WriteLine( "The date is: {0:yyyy-MM-dd}", dt );
return dt;
}
else
{
return someDefaultDate;
}
If you want the same behaviour as VB's IsDate function, add a call to DateTime.TryParse if the is DateTime check evaluates to false:
static DateTime GetDateOrDefault( Object o, DateTime defaultValue )
{
if( o is DateTime dt )
{
return dt;
}
else if( DateTime.TryParse( o?.ToString(), out dt ) )
{
return dt;
}
else
{
return defaultValue;
}
}
I am using the following code in order to substract a day of the DateTime until I am getting Monday:
DateTime currentWeek = new DateTime(beginDate.Year, beginDate.Month, beginDate.Day);
while (currentWeek.DayOfWeek.ToString() != "Monday")
{
currentWeek.AddDays(-1);
MessageBox.Show(currentWeek.Day.ToString());
MessageBox.Show(currentWeek.DayOfWeek.ToString());
}
beginDate is in the first run set to the current Date of DateTime.Now.
For me this loops forever, and the day of currentWeek always stays the same (29) even though I am substracting 1 everytime I am looping through.
I am already using another function that takes a DateTime and a bool Parameter, which does pretty much the same and works:
private void ErstenTagDerWocheAuswaehlen(DateTime date, bool anfangDerWoche = true)
{
string wochentagName;
int incrementor;
if(anfangDerWoche == true)
{
wochentagName = "Monday";
incrementor = -1;
}
else
{
wochentagName = "Friday";
incrementor = 1;
}
while(date.DayOfWeek.ToString() != wochentagName)
{
date = date.AddDays(incrementor);
}
}
Can someone explain to me why the upper code doesn't work whilst the lower one does?
You have to assign the resulting value, DateTime is immutable.
currentWeek = currentWeek.AddDays(-1);
About your 2nd question:
Use the enum for day of the week, do not try to convert a day of the week to a string for a comparison. The type is DayOfWeek.
Again, a DateTime is not mutable so you have to return a DateTime instance as you can't mutate the one that was passed in (without passing it as ref)
Code change
private DateTime ErstenTagDerWocheAuswaehlen(DateTime date, bool anfangDerWoche = true)
{
System.DayOfWeek wochentagName;
int incrementor;
if(anfangDerWoche == true)
{
wochentagName = System.DayOfWeek.Monday;
incrementor = -1;
}
else
{
wochentagName = System.DayOfWeek.Friday;
incrementor = 1;
}
while(date.DayOfWeek != wochentagName)
{
date = date.AddDays(incrementor);
}
return date;
}
DateTime is an immutable struct, so you need to store the value returned from AddDays():
var t2 = currentWeek.AddDays(-1);
Then use t2. The call to AddDays() doesn't actually change currentWeek.
As DateTime is immutable, when using the AddDays it returns a new DateTime structure with the new information and does not change the given one.
Method documentation states:
Returns a new System.DateTime that adds the specified number of days to the value of this instance.
You must assign it to a variable:
currentWeek = currentWeek.AddDays(-1);
I need to compare two dates including the seconds. I searched the web and got two methods. One is import Microsoft.VisualBasic dll. The result is not same when it runs the same data. I think C# should do the same thing without import VisualBasic. Can someone point the way to me to made it work in C#?
Thanks in advance.
There is one using Microsoft.VisualBasic
if (Math.Abs(DateAndTime.DateDiff(DateInterval.Second,
Conversions.ToDate(colFilesFound[RuntimeHelpers.GetObjectValue(rw["file_path"])]),
Conversions.ToDate(rw["last_modified_timestamp"]),
FirstDayOfWeek.Sunday, FirstWeekOfYear.Jan1)) == 0L)
{
unchangedFileNum++;
Console.WriteLine("unchange");
}
else
{
modifiedFileNum++;
Console.WriteLine("change");
}
There is another method not using Visual.dll:
DateTime fileLastModifiedDate = Conversions.ToDate(colFilesFound[rw["file_path"]]);
DateTime dataLastModifiedDate = Conversions.ToDate(rw["last_modified_timestamp"]);
if (Math.Abs((fileLastModifiedDate - dataLastModifiedDate).TotalSeconds) == 0L)
{
Console.WriteLine("File Date: " + colFilesFound[rw["file_path"]] +
" <> Database Date: " + Conversions.ToString(rw["last_modified_timestamp"]));
unchangedFileNum++;
Console.WriteLine("unchange");
}
else
{
modifiedFileNum++;
Console.WriteLine("change");
}
The correct way to compare DateTimes in .NET is using the == operator or calling the DateTime.Compare() method:
DateTime fileLastModifiedDate = ...;
DateTime dataLastModifiedDate = ...;
if (fileLastModifiedDate == dataLastModifiedDate)
{
...
}
You need to take into account the precission. Sometimes, you cannot have enough precission to consider milliseconds. In this case you need to compare the dates without having into account the milliseconds:
public static bool IsSameDateWithoutMilliseconds(DateTime d1, DateTime d2)
{
return d1.Subtract(d2).TotalSeconds == 0;
}
Your problem is probably the milliseconds since those are probably not equal the way you check the DateTimes.
DateTime fileLastModifiedDate = Conversions.ToDate(colFilesFound[rw["file_path"]]);
DateTime dataLastModifiedDate = Conversions.ToDate(rw["last_modified_timestamp"]);
fileLastModifiedDate = fileLastModifiedDate.AddMilliseconds(-fileLastModifiedDate.Millisecond);
dataLastModifiedDate = dataLastModifiedDate.AddMilliseconds(-dataLastModifiedDate.Millisecond);
if (DateTime.Compare(fileLastModifiedDate, dataLastModifiedDate) == 0)
{
// dates are equal
}
else
{
// dates are not equal
}
I was wondering if there is any neat way to check is data is in allowed range. I mean in c# we can represent data from 0001-01-01 to (I think) 9999-01-01. However if we try to do something like that
DateTime result = DateTime.Parse("0001-01-01").Subtract(TimeSpan.FromDays(1))
I get an exception. Is there any neat way to check is it is possible to do DateTime operations (addition subtraction etc)
Just use the comparison operators (>, <, >=, <=, == and !=), as they are implemented in DateTime.
Example:
DateTime lowerAllowedDate = new DateTime(1,1,1); // 01/01/0001
DateTime upperAllowedDate = new DateTime(3000, 12, 31) // 31/12/3000
DateTime now = DateTime.Now
if (lowerAllowedDate <= now && now < upperAllowedDate)
{
//Do something with the date at is in within range
}
Consider these extension methods.
public static class ValidatedDateTimeOperations
{
public static bool TrySubtract (this DateTime dateTime, TimeSpan span, out DateTime result)
{
if (span < TimeSpan.Zero)
return TryAdd (dateTime, -span, out result);
if (dateTime.Ticks >= span.Ticks)
{
result = dateTime - span;
return true;
}
result = DateTime.MinValue;
return false;
}
public static bool TryAdd (this DateTime dateTime, TimeSpan span, out DateTime result)
{
if (span < TimeSpan.Zero)
return TrySubtract (dateTime, -span, out result);
if (DateTime.MaxValue.Ticks - span.Ticks >= dateTime.Ticks)
{
result = dateTime + span;
return true;
}
result = DateTime.MaxValue;
return false;
}
}
The can be called like this:
DateTime result;
if (DateTime.MinValue.TrySubtract (TimeSpan.FromDays(1), out result)
{
// Subtraction succeeded.
}
Checking for an overflow in a given operation beforehand is cumbersome and I'm not really sure it's really worth it against simply handling the exception.
You could for example do the following when subtracting:
DateTime date;
TimeSpan subtractSpan;
if ((date - DateTime.MinValue) < subtractSpan)
{
//out of range exception: date - subtractSpan
}
Worth it? Your call.
Take a look at the DateTime structure documentation in MSDN.
In particular, you can take a look at:
TryParse and TryParseExact
The comparison operators
MinValue and MaxValue
You can also put try..catch (ArgumentOutOfRangeException) around the DateTime values you are trying to use.
However, if you are consistently (or ever?) running into this kind of exception, I'd take a closer look at your design. Unless you are doing some serious date-crunching, I don't know of any instance where I would be bumping into the min and max values.
I've seen these questions but both involve methods that aren't available in the CellStyle Format value. I only want to show the hours and minutes portion (16:05); not the seconds as well (16:05:13). I tried forcing the seconds value to zero but still got something like 16:05:00. Short of using a kludge like providing a string or a DateTime (and only showing the hour/minutes part) is there any way I can get the formatting to do what I want.
I just discovered this myself. Unfortunately, the solution is pretty involved. The good news is that it works.
Firstly, you need an ICustomFormatter implementation that deals with TimeSpan values. The .NET framework does not include such a type out-of-the-box; I am guessing this is because Microsoft didn't want to have to deal with the ambiguity involved in formatting a TimeSpan (e.g., does "hh" mean total hours or only the hour component?) and the ensuing onslaught of support issues that would arise when these ambiguities confused developers.
That's OK -- just implement your own. Below is a sample class I wrote that uses basically the same custom format strings as DateTime (those that were applicable, anyway)*:
class TimeSpanFormatter : IFormatProvider, ICustomFormatter
{
private Regex _formatParser;
public TimeSpanFormatter()
{
_formatParser = new Regex("d{1,2}|h{1,2}|m{1,2}|s{1,2}|f{1,7}", RegexOptions.Compiled);
}
#region IFormatProvider Members
public object GetFormat(Type formatType)
{
if (typeof(ICustomFormatter).Equals(formatType))
{
return this;
}
return null;
}
#endregion
#region ICustomFormatter Members
public string Format(string format, object arg, IFormatProvider formatProvider)
{
if (arg is TimeSpan)
{
var timeSpan = (TimeSpan)arg;
return _formatParser.Replace(format, GetMatchEvaluator(timeSpan));
}
else
{
var formattable = arg as IFormattable;
if (formattable != null)
{
return formattable.ToString(format, formatProvider);
}
return arg != null ? arg.ToString() : string.Empty;
}
}
#endregion
private MatchEvaluator GetMatchEvaluator(TimeSpan timeSpan)
{
return m => EvaluateMatch(m, timeSpan);
}
private string EvaluateMatch(Match match, TimeSpan timeSpan)
{
switch (match.Value)
{
case "dd":
return timeSpan.Days.ToString("00");
case "d":
return timeSpan.Days.ToString("0");
case "hh":
return timeSpan.Hours.ToString("00");
case "h":
return timeSpan.Hours.ToString("0");
case "mm":
return timeSpan.Minutes.ToString("00");
case "m":
return timeSpan.Minutes.ToString("0");
case "ss":
return timeSpan.Seconds.ToString("00");
case "s":
return timeSpan.Seconds.ToString("0");
case "fffffff":
return (timeSpan.Milliseconds * 10000).ToString("0000000");
case "ffffff":
return (timeSpan.Milliseconds * 1000).ToString("000000");
case "fffff":
return (timeSpan.Milliseconds * 100).ToString("00000");
case "ffff":
return (timeSpan.Milliseconds * 10).ToString("0000");
case "fff":
return (timeSpan.Milliseconds).ToString("000");
case "ff":
return (timeSpan.Milliseconds / 10).ToString("00");
case "f":
return (timeSpan.Milliseconds / 100).ToString("0");
default:
return match.Value;
}
}
}
We're not finished yet. With this type in place, you are equipped to assign a custom formatter to the column in your DataGridView that you want to use for displaying your TimeSpan values.
Let's say that column is called "Time"; then you would do this:
DataGridViewColumn timeColumn = dataGridView.Columns["Time"];
timeColumn.DefaultCellStyle.FormatProvider = new TimeSpanFormatter();
timeColumn.DefaultCellStyle.Format = "hh:mm";
So now you're set up, right?
Well, for some odd reason, you're still not 100% of the way there. Why custom formatting can't kick in at this point, I honestly couldn't tell you. But we're almost done. The one final step is to handle the CellFormatting event to get this new functionality we've written to actually take effect:
private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
var formatter = e.CellStyle.FormatProvider as ICustomFormatter;
if (formatter != null)
{
e.Value = formatter.Format(e.CellStyle.Format, e.Value, e.CellStyle.FormatProvider);
e.FormattingApplied = true;
}
}
At last, we're finished. Setting the DefaultCellStyle.Format property of the DataGridViewColumn you want formatted according to your custom rules should now work as expected.
*So, "h"/"hh" for hours, "m"/"mm" for minutes. etc.
It is possible to achieve the effect same by just using the CellFormatting event.
private void dataGridView_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
if (e.Value != null && e.Value != DBNull.Value)
e.Value = ((TimeSpan)e.Value).Hours.ToString("00") + ":" +
((TimeSpan)e.Value).Minutes.ToString("00");
}
This obviously is not as comprehensive a solution, but quite quick.
Try the following code
dataGridView1.Columns["columnName"].DefaultCellStyle.Format = "hh\\:mm";
I don't know how to set the format of the cell to show only hours and minutes. I'd suggest you set the format of the cell to string and format the value like this:
String.Format("{0:D2}:{1:D2}",
DateTime.Now.TimeOfDay.Hours, DateTime.Now.TimeOfDay.Minutes);
Use format string "hh\\:mm".
e.g
YourGrid.Column[index].DefaultCellStyle.Format = "hh\\:mm"
Try another approach. Just add to your class binding to the datagridview properties like for instance LastPacketAtTimeDelayAsStr.
Let's say you have some class that has it...
public DateTime? LastPacketAtTime { get; set; }
public TimeSpan? LastPacketAtTimeDelay
{
get
{
if (LastPacketAtTime.HasValue)
{
var ts = DateTime.Now - LastPacketAtTime.Value;
return ts;
}
return null;
}
}
public string LastPacketAtTimeDelayAsStr
{
get
{
if (LastPacketAtTimeDelay.HasValue)
{
var hours = LastPacketAtTimeDelay.Value.Hours.ToString("00");
var minutes = LastPacketAtTimeDelay.Value.Minutes.ToString("00");
var seconds = LastPacketAtTimeDelay.Value.Seconds.ToString("00");
return $"{LastPacketAtTimeDelay.Value.Days} days {hours}:{minutes}:{seconds}";
}
return null;
}
}
And after that just bind the LastPacketAtTimeDelayAsStr to the DataGridView column you need which has String datatype.
And that's it!