Converting value type inside class property - c#

Hi I am having a simple problem, I am trying to convert (value is DateTime and i want to convert it to string) value type inside class to another type, so far i have tried:
private string timestamp;
public string timestamp
{
get => timestamp;
set
{
if (value != timestamp)
{
timestamp = (DateTime)value.ToString("dd'/'MM'/'yyyy HH':'mm':'ss.fff");
}
}
}
But with no luck. Is there a solution for this??

You are converting a string to a string using ToString with a formatter that you would expect on a DateTime, and then you're casting it to a DateTime that has to be assigned to a string.
Obviously, this cannot work.
If you are trying to validate if the new property-value (Value) can be considered as a valid string, then use DateTime.TryParseExact to verify if the given string is a valid DateTime. If so, assign the string to the backing field of the property.
set
{
if (value != timestamp && DateTime.TryParseExact(value,
"dd'/'MM'/'yyyy HH':'mm':'ss.fff",
CultureInfo.CurrentCulture,
DateTimeStyles.None, DateTime out d )
{
timestamp = value;
}
}
You will have to fiddle around a bit with the arguments of the TryParseExact method to see what works in your scenario.

Related

Parse custom JSON string to DateTime Convert in API

My Question is similar to this one (The JSON value could not be converted to System.DateTime), where I am trying to convert an input string to an DateTime. The difference, however, is that the input string cannot be changed. I have no choice in it.
[HttpPut("PutBodyToFoodChain")]
public async Task<IActionResult> PutBodyToFoodChain([FromBody] TxMSAGrading body)
{ ... }
What I've tried:
[JsonConverter(typeof(DateFormatConverter), "MM/dd/yyyy hh:mm:ss")]
public DateTime KillDate { get; set; }
Error:
The JSON value could not be converted to System.DateTime
Input String:
{"GradeDate": "08/24/2020 01:36:00", "KillDate" : "08/24/2020 00:00:00", ... }
Additional Information:
I cannot change the model. So it will always be parsed in to be converted to a Date-Time.
There are 500+ fields in the model. I can't explicitly convert every Date-time field.
DateTime fields will always have the same format.
One idea is to defer parsing until after the raw string is captured in your model. This allows deserialization to succeed more reliably and for you to have control over the parsing. For example, an updated model:
public string KillDate { get; set; }
public DateTime KillDateValue => DateTime.TryParse(KillDate, out DateTime parsed) ? parsed : DateTime.MinValue;
public bool KillDateParsed => KillDateValue != DateTime.MinValue;
If parsing succeeds, KillDateParsed will be true and you'll have the parsed value in KillDateValue. DateTime.TryParse can also be provided a specific pattern to match.

DateTime To Be NULL when the value is DateTime.MinValue or it is Null

In my console app I am attempting to format to HHmmss -> I am sure it is due to my data types but how can I have this be NULL when NULL and not display 1/1/0001 12:00:00 AM?
This is my syntax
public static DateTime fmtLST;
public static string LST = null;
if (LST != null)
{
IFormatProvider format = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat;
fmtLST = DateTime.ParseExact(LST, "HHmmss", format);
}
Console.WriteLine(fmtLST.ToString("hh:mm:ss tt"));
If altered to public static DateTime? fmtLastScanTime; I get an error of
'No overload for method 'ToString' takes 1 arguments
How can I have this display NULL instead of 1/1/0001 12:00:00 AM?
Trying to account for 1/1/0001 12:00:00 AM being displayed
Nullable DateTime. A nullable DateTime can be null. The DateTime struct itself does not provide a null option. But the "DateTime?" nullable type allows you to assign the null literal to the DateTime type. It provides another level of indirection.
public static DateTime? fmtLST;
//or
public static Nullable<DateTime> fmtLST;
A nullable DateTime is most easily specified using the question mark syntax
Edit:
Console.WriteLine(fmtLST != null ? fmtLST.ToString("hh:mm:ss tt") : "");
Another one could be
if(fmtLST == DateTime.MinValue)
{
//your date is "01/01/0001 12:00:00 AM"
}
May be but try this
IFormatProvider format = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat;
fmtLST = DateTime.ParseExact((LST != null ? LST : null), "HHmmss", format);
I have Found This refrence here when seraching for same Issue
using System;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Nullable nullDateTime;
//DateTime? nullDateTime = null;
nullDateTime = DateTime.Now;
if (nullDateTime != null)
{
MessageBox.Show(nullDateTime.Value.ToString());
}
}
}
}
you can go in link find more details
Thanks
The value 1/1/0001 12:00:00 AM is the minimum/default value of the DateTime object, if you want to assign null to DateTime object means you have to make them as Nullable(as like others suggested). So the declaration of fmtLST should be :
public static DateTime? fmtLST = null; // initialization is not necessary
In this case you have to care about printing the output to the console. it should be something like:
Console.WriteLine(fmtLST.HasValue ? fmtLST.Value.ToString("hh:mm:ss tt") : "Value is null");

Get DateTime as UTC with Dapper

I'm using Dapper to map my entities to SQL Server CE. If I save a DateTime with Kind=Utc, when I read it back I get a DateTime with Kind=Unspecified, which leads to all kind of problems.
Example:
var f = new Foo { Id = 42, ModificationDate = DateTime.UtcNow };
Console.WriteLine("{0} ({1})", f.ModificationDate, f.ModificationDate.Kind);
connection.Execute("insert into Foo(Id, ModificationDate) values(#Id, #ModificationDate)", f);
var f2 = connection.Query<Foo>("select * from Foo where Id = #Id", f).Single();
Console.WriteLine("{0} ({1})", f2.ModificationDate, f2.ModificationDate.Kind);
This code gives the following output:
20/09/2012 10:04:16 (Utc)
20/09/2012 10:04:16 (Unspecified)
I know I should be using a DateTimeOffset, but unfortunately SQL CE has no support for this type.
Is there a workaround? Can I tell Dapper to assume that all dates have DateTimeKind.Utc? And more generally, what are my options to customize the mapping?
EDIT: My current workaround is to patch the dates after Dapper has materialized the result, but it kind of smells...
var results = _connection.Query<Foo>(sql, param).Select(PatchDate);
...
static Foo PatchDate(Foo f)
{
if (f.ModificationDate.Kind == DateTimeKind.Unspecified)
f.ModificationDate = DateTime.SpecifyKind(f.ModificationDate, DateTimeKind.Utc);
return f;
}
Adding this answer for anyone else who comes looking for a simple fix. This is possible now with the addition of SqlMapper.TypeHandler in Dapper.
Add this class to convert the value from the db to a datetime with the kind specified as UTC.
public class DateTimeHandler : SqlMapper.TypeHandler<DateTime>
{
public override void SetValue(IDbDataParameter parameter, DateTime value)
{
parameter.Value = value;
}
public override DateTime Parse(object value)
{
return DateTime.SpecifyKind((DateTime)value, DateTimeKind.Utc);
}
}
Then in my Global.asax file of my Web API I add the type handler to dapper.
SqlMapper.AddTypeHandler(new DateTimeHandler());
If you need to ensure you are always inserting dates as UTC, then on the SetValue method you can use:
parameter.Value = DateTime.SpecifyKind(value, DateTimeKind.Utc);
Looked into the Dapper code. Unless mine was out of date, for value types like datetime (which is mapped to DbType.DateTime), dapper just does a simple cast from the IDataReader object.
Pseudo : yield return (DateTime)IDataReader.GetValue(0);
That's the specific case for Datetime out of a bunch of generic code and lambdas.
AFAIK, SQL datetime never stores the offset / timezone so the kind will always say "Unspecified" on any datetime you store and fetch.
So, to do it cleanly, you could touch dapper internals:
which is a pain as you'd have to touch a big IL generating method (the DataRow Deserializer) and put in an if case for DateTime.
OR
just put a setter on the DateTime props where UTC is an issue (which is kinda against POCO but is relatively sane):
class Foo
{
private DateTime _modificationDate;
public DateTime ModificationDate
{
get { return _modificationDate; }
set { _modificationDate = DateTime.SpecifyKind(value, DateTimeKind.Utc); }
}
//Ifs optional? since it's always going to be a UTC date, and any DB call will return unspecified anyways
}
Just wanted to put my full solution here for seamlessly integrating DateTimeOffset / DateTimeOffset? fields/properties with a MySQL 5.7 database (which doesn't support DbType.DateTimeOffset) - based on #matt-jenkins answer above:
public static class DapperExtensions
{
class DateTimeOffsetTypeHandler : SqlMapper.TypeHandler<DateTimeOffset>
{
public override void SetValue(IDbDataParameter parameter, DateTimeOffset value)
{
switch (parameter.DbType)
{
case DbType.DateTime:
case DbType.DateTime2:
case DbType.AnsiString: // Seems to be some MySQL type mapping here
parameter.Value = value.UtcDateTime;
break;
case DbType.DateTimeOffset:
parameter.Value = value;
break;
default:
throw new InvalidOperationException("DateTimeOffset must be assigned to a DbType.DateTime SQL field.");
}
}
public override DateTimeOffset Parse(object value)
{
switch (value)
{
case DateTime time:
return new DateTimeOffset(DateTime.SpecifyKind(time, DateTimeKind.Utc), TimeSpan.Zero);
case DateTimeOffset dto:
return dto;
default:
throw new InvalidOperationException("Must be DateTime or DateTimeOffset object to be mapped.");
}
}
}
private static int DateTimeOffsetMapperInstalled = 0;
public static void InstallDateTimeOffsetMapper()
{
// Assumes SqlMapper.ResetTypeHandlers() is never called.
if (Interlocked.CompareExchange(ref DateTimeOffsetMapperInstalled, 1, 0) == 0)
{
// First remove the default type map between typeof(DateTimeOffset) => DbType.DateTimeOffset (not valid for MySQL)
SqlMapper.RemoveTypeMap(typeof(DateTimeOffset));
SqlMapper.RemoveTypeMap(typeof(DateTimeOffset?));
// This handles nullable value types automatically e.g. DateTimeOffset?
SqlMapper.AddTypeHandler(typeof(DateTimeOffset), new DateTimeOffsetTypeHandler());
}
}
}
If you are using Dapper from source (not nuget), you could tweak the code to always force DateTimeKind of UTC. A more configurable option might be to create a new attribute for DateTime property values that allow you to specify date time kind as a hint to dapper. Dapper could look for DateTime properties with this attribute and when found could use it to specify the DateTime kind during ORM mapping. This might be a nice feature for core dapper as you are not the only one with this issue :)

Compare objects where some props have different formats

What is the best way to compare all properties of two objects where some of them have different formats (e.g. DateTime in one and DateTime.ToString() with custom format in other)?
I was able to do that by using 2 assertions:
o1.ShouldHave().AllPropertiesBut(dto1 => dto1.Date).EqualTo(o2);
o1.Date.Should().Be(DateTime.Parse(o2.Date));
I would think about the following, but that does not compile because EqualTo<T>() is void.
o1.ShouldHave().AllProperties().But(d => d.Date).EqualTo(o2)
.And.Date.Should().Be((DateTime.Parse(o2.Date));
types are:
public class Dto1
{
public int ID { get { return 1; } }
public DateTime Date { get { return DateTime.Now.Date; } }
}
public class Dto2
{
public int ID { get { return 1; } }
public string Date { get { return DateTime.Now.Date.ToShortDateString(); } }
}
var o1 = new Dto1();
var o2 = new Dto2();
The first example is typically the best way. However, if you would switch o1 and o2, it might work in a single call. Fluent Assertions will try to convert (using Convert.ChangeType) the actual value of a property to the expected value of the property with the same name. In your particular example, it would try to convert the DateTime in Dto1 to a string in Dto2 before comparing the values. But since the string representation of a DateTime is dependent on the culture of the thread, it would not give you predictable results. However, if you would switch o1 and o2, I wouldn't be surprised if Convert.ChangeType would succesfully convert your short datetime back to a DateTIme object.
As a side-note, my DTOs usually just pass the DateTime to the caller without any string conversion. I believe that the actual representation of the DateTime is purely a UI responsibility.
HTH
Dennis

Format a string to display the Date correctly

I need help converting this string --> 20090727 10:16:36:643 to --> 07/27/2009 10:16:36
The original date and time are being returned by the SynchronizationAgent.LastUpdated() function, which returns a String in the above format.
Original question:preserved for reference
I have this -->
HUD.LastSyncDate = mergeSubscription.SynchronizationAgent.LastUpdatedTime;
Which is setting a property that looks like this -->
public static string LastSyncDate
{
get { return _lastSyncDate; }
set
{
_lastSyncDate = String.Format(CultureInfo.InvariantCulture,"{0:G}", value);
}
}
Unfortunately, with or without the String.Format the date that is displayed looks like this --> 20090727 10:16:36:643
I have tried multiple variations to Format it the way I want. What am I missing?
Based on the below suggestions(Mostly Joel's), I implemented the suggested changes but I am still getting a "String is not a valid DateTime error"
I also tried implementing this -->
HUD.LastSyncDate = DateTime.ParseExact(mergeSubscription.SynchronizationAgent.LastUpdatedTime,"yyyyMMdd HH:mm:ss:fff",CultureInfo.InvariantCulture);
but still nothing.
HUD.LastSyncDate = DateTime.Parse(mergeSubscription.SynchronizationAgent.LastUpdatedTime).ToString("MM/dd/yyyy")
You can put any format string you want there. But it sounds like what you really want is something more like this:
private static DateTime _lastSyncDate;
public static DateTime LastSyncDate
{
get { return _lastSyncDate; }
set { _lastSyncDate = value;}
}
public static string LastSyncDateString
{
get { return LastSyncDate.ToString("MM/dd/yyyy"); }
}
Keep it as a datetime in the background and just use the string property for display.
It appears to me that LastUpdatedTime is actually a string (since you can do the assignment) not a DateTime. In that case, the format applied won't do anything. You'll want to parse the LastUpdatedTime into a DateTime then reformat into the format that you want before assigning it to your string.
DateTime lastUpdated = DateTime.Parse( mergeSubscription.SynchronizationAgent.LastUpdatedTime );
HUD.LastSyncDate = string.Format( "{0:G}", lastUpdated );
public static string LastSyncDate { get; set; }
Note that you may need to use ParseExact instead.
DateTime lastUpdated = DateTime.ParseExact( "yyyyMMdd HH:mm:ss:fff",
...,
CultureInfo.InvariantCulture );
What do you want to do? You get a string, pass it to String.Format() and store it in a string field. Do you want to reformat the string? In this case you have to parse the string back to DateTime and format this value again.
DateTime dateTime;
if (DateTime.TryParse(value, out dateTime))
{
lastSyncDate = String.Format(CultureInfo.InvariantCulture,"{0:G}", dateTime);
}
else
{
HandleInvalidInput(value);
}

Categories