Fluent assertion to verify DateTime field is not empty - fluent-assertions

var dte = "2021-12-18T15:06:33.2677927Z"
dte.Should().
I want to check that this dte is not empty.
Currently I am using :
dte.Should().BeAfter(new DateTime());

From the example, your definition of an "empty" DateTime seems to be one different from the default value.
If you're looking for a more readable/idiomatic way to express that, I would go with
DateTime subject = MethodUnderTest();
subject.Should().NotBe(default);
For the general case, one cannot speak of an "empty" DateTime since even the default value is just the minimum representable valid datetime, namely 0001-01-01T00:00:00.
So all of these are identical.
DateTime.Parse("0001-01-01T00:00:00")
default(DateTime)
new DateTime()
DateTime.MinValue
DateTime.FromBinary(0)

Related

Why does SSRS Independent parameter is cascading by default

I have a report which currently has four parameters
1) BatchID
2) ProductName (dropdown parameter populated based on the batch ID)
3) StartDateTime (Date/time parameter with default value set to =Now)
4) EndDateTime (Date/time parameter with default value set to =Now)
When I run the report the StartDateTime and EndDateTime are greyed out why? As they are independent parameters I would have thought it will be enabled by default.
The next question I have is the delay, after entering the first parameter the second parameter is populated as expected. However after selecting the second parameter from the dropdown. There is a delay of over 30 seconds to populate the StartDateTime and EndDateTime parameter(s) with the current datetime.
I don't know if I did a good job in explaining my question. Any help.
Note that the problem of having a disabled date type of parameter for SSRS only occurs when the date/time parameter’s default value is an expression like =Now. If the default value is not set or is set to a literal value like 1/1/2018, the control functions as expected — it is enabled.
"Parameter order is important when you want to show users the default value for one parameter before they choose values for other parameters"
Ref:http://msdn.microsoft.com/en-us/library/cc281392.aspx
The controls after non-default parameters are disabled the user picks something, so order is important. The date/time control won’t be enabled until it evaluates its default value expression. It is stated that the delay you mentioned here is intended to allow this parameter’s expression to use the proceeding parameter’s value in its computation.
Ref2: https://bengribaudo.com/blog/2011/03/02/595/ssrs-datetime-parameter-disabled-when-default-value-expression-is-used

If - Strange Behavior in Detecting Type

I discovered this strange behavior in VB.Net today in trying to work with nullable DateTime data. I am pulling a DateTime value out of an XML file for inserting into a database, and I want to allow for an empty value. So I thought I should use If to prevent casting errors:
Dim LastRun As DateTime? = _
If(rowData("LastRun") = "", Nothing, CType(rowData("LastRun"), DateTime))
It seems like this should return a value of Nothing in the case that the If is false, or a value of the date time from LastRun if the value is not blank. Instead, when the If condition returns false, I get a value of DateTime.MinValue, which causes an exception on insert to the database due to SQL DateTime underflow.
I was able to fix it by using DateTime? as the cast in the last parameter, but this behavior seems odd to me. The expected type is clearly DateTime? because that's the variable type. Also, the narrowest type that can allow for both possible result values is DateTime?, since it could be either a DateTime or Nothing. And yet somehow it decides that the result value should be DateTime and then I guess typecasts Nothing to DateTime.MinValue? What is going on here?
Part of the problem is I'm used to C#, and the equivalent expression rowData["LastRun"] == "" ? null : (DateTime)rowData["LastRun"]) doesn't even compile (as expected), because there's "no implicit conversion between DateTime and null."
Nothing is not the same as null in C#, it is a mixture between null and default(T). So when you use Nothing on a value type(like the structure DateTime) you get it's default value what is DateTime.MinValue.

.NET Date variable specification & locale

I set the date variable
Dim myDate as Date
myDate = #5/15/2013#
Does this work always at runtime, no matter what is the system locale settings?
According to the MSDN documentation:
You must enclose a Date literal within number signs (# #). You must specify the date value in the format M/d/yyyy, for example #5/31/1993#. This requirement is independent of your locale and your computer's date and time format settings.
The reason for this restriction is that the meaning of your code should never change depending on the locale in which your application is running. Suppose you hard-code a Date literal of #3/4/1998# and intend it to mean March 4, 1998. In a locale that uses mm/dd/yyyy, 3/4/1998 compiles as you intend. But suppose you deploy your application in many countries. In a locale that uses dd/mm/yyyy, your hard-coded literal would compile to April 3, 1998. In a locale that uses yyyy/mm/dd, the literal would be invalid (April 1998, 0003) and cause a compiler error.
So, the answer to your question is YES, it will always work at runtime, and NO, you do not need to change this for the locale settings of the computer.
Do keep in mind that date literals are somewhat frowned upon. They are supported in VB.Net as a backwards-familiarity thing from VB6. They don't even exist in other .Net languages like C#. If you have to hard-code a specific date, you are much better off using DateTime with separate parameters, such as:
Dim myDate as DateTime
myDate = new DateTime(2013,5,15)
Also note that Date is just a VB.Net alias to System.DateTime, again there for backwards familiarity from VB6. It doesn't matter which you use, they mean the same thing.
No, that does not work in a country such as Canada where the standard is dd/MM/yyyy. The best way to globalize your application would be to use DateTime.ParseExact and/or DateTime.TryParseExact
Dim tempdate As DateTime
tempdate = DateTime.ParseExact("05/20/2013", "MM/dd/yyyy", Globalization.CultureInfo.InvariantCulture)

Why does an uninitialised DateTime struct tell me it's monday

I have a DateTime struct I use, and sombody commented out the part where it was created (but not declared) When I was using it, myDate.DayOfWeek == DayOfWeek.Monday returned true.
If the part where it is created is commented out, how can it tell me it's monday, instead of throwing some exception?
DateTime is a value type (a struct), and therefore it always has a value. It cannot be null like a reference type. When a variable of a value type is not assigned yet, it's initialized to some default value depending on the type. The default value for DateTime just happens to be a Monday in the calendar .NET uses.
Of course, calendars have changed many times in the past and applying our current calendar more than a few hundred years into the past just doesn't work, but to .NET, it's a Monday.
DateTime is a struct which means that it has to have some sort of default value. The default value for DateTime is DateTime.MinValue, which is 1 January 0001, which was a monday.

.NET Date Const (with Globalization)

Does anyone know of a way to declare a date constant that is compatible with international dates?
I've tried:
' not international compatible
public const ADate as Date = #12/31/04#
' breaking change if you have an optional parameter that defaults to this value
' because it isnt constant.
public shared readonly ADate As New Date(12, 31, 04)
If you look at the IL generated by the statement
public const ADate as Date = #12/31/04#
You'll see this:
.field public static initonly valuetype [mscorlib]System.DateTime ADate
.custom instance void [mscorlib]System.Runtime.CompilerServices.DateTimeConstantAttribute::.ctor(int64) = ( 01 00 00 C0 2F CE E2 BC C6 08 00 00 )
Notice that the DateTimeConstantAttribute is being initialized with a constructor that takes an int64 tick count. Since this tick count is being determined at complile time, it seems unlikely that any localization is coming into play when this value is initialized at runtime. My guess is that the error is with some other date handling in your code, not the const initialization.
According to the Microsoft documentation,
"You must enclose a Date literal within number signs (# #). You must specify the date value in the format M/d/yyyy, for example #5/31/1993#. This requirement is independent of your locale and your computer's date and time format settings."
Are you saying that this is not correct and the parsing is affected by the current locale?
Edit: Did you try with a 4-digit year?
Once you have data into Date objects in VB, you don't have to worry about globalization until you compare something to it or try to export it.
This is fine:
Dim FirstDate as Date = Date.UtcNow() 'or this: = NewDate (2008,09,10)'
Dim SecondDate as Date
SecondDate = FirstDate.AddDays(1)
This pulls in the globalization rules and prints in the current thread's culture format:
HeaderLabel.Text = SecondDate.ToString()
This is bad:
Dim BadDate as Date = CDate("2/20/2000")
Actually--even that is OK if you force CDate in that case to use the right culture (InvariantCulture):
Dim OkButBadPracticeDate as Date = CDate("2/20/2000", CultureInfo.InvariantCulture)
If you want to force everything to a particular culture, you need to set the executing thread culture and UI culture to the desired culture (en-US, invariant, etc.).
Make sure you aren't doing any work with dates as strings--make sure they are actual Date objects!
Ok right, I understand more where you are coming from..
How about:
Create a static method that returns the date constant. This overcomes the international issue since it is returned as the specific DateTime value.
Now I remember optional params from my VB6 days, but can you not just overload the method? If you are using the overloaded method without the date, just pull it from the static?
EDIT: If you are unsure what I mean and would like a code sample, just comment this post and I will chuck one on.
OK, I am unsure what you are trying to do here:
The code you are posting is NOT .NET, are you trying to port?
DateTime's cannot be declared as constants.
DateTime's are a data type, so once init'ed, the format that they were init'ed from is irrelevant.
If you need a constant value, then just create a method to always return the same DateTime.
For example:
public static DateTime SadDayForAll()
{
return new DateTime(2001, 09, 11);
}
Update
Where the hell are you getting all that from?!
There are differences between C# and VB.NET, and this highlights one of them.
Date is not a .NET data type - DateTime is.
It looks like you can create DateTime constants in VB.NET but there are limitations
The method was there to try and help you, since you cannot create a const from a variable (i.e. optional param). That doesn't even make sense.