The book picks up where we left off, and we continue to share some common extensions on time conversion.
01TimeOnly
This way is to convert the TimeOnly type to DateTime type, where the date part uses the current date of the system, and the time part uses TimeOnly, the specific code is as follows:
// time to date time, the default use of the current date + time to date time format
public static DateTime ToDateTime(this TimeOnly timeOnly)
{
return ().ToDateTime(timeOnly);
}
02, Date+Time to DateTime TimeOnly
This method is a direct extension of TimeOnly time, take its time, then complement the specified DateOnly date, and finally to DateTime, the code is as follows:
//dates+时间转为dates时间
public static DateTime ToDateTime(this TimeOnly timeOnly, DateOnly dateOnly)
{
return (timeOnly);
}
03, date part in datetime + time to datetime TimeOnly
This method extends TimeOnly time, takes its time, then complements the date part of the specified DateTime, and finally converts it to DateTime, with the following code:
//DateTime in the date part of the date + time to date time format
public static DateTime ToDateTime(this TimeOnly timeOnly, DateTime dateTime)
ToDateTime(this TimeOnly timeOnly, DateTime dateTime)
return (dateTime).ToDateTime(timeOnly);
}
04DateOnly
This method is to convert the DateOnly type to DateTime type, where the date part uses DateOnly and the time part uses the current time of the system, the specific code is as follows:
//Date to date time, date + default use of the current time to date time format
public static DateTime ToDateTime(this DateOnly dateOnly)
{
return (());
}
05DateOnly, Date+DateTime in Time Part to DateTime
This method extends DateOnly date by taking its date, then completing the time part of the specified DateTime, and finally converting it to DateTime, with the following code:
//Date+DateTime in the time part to DateTime
public static DateTime ToDateTime(this DateOnly dateOnly, DateTime dateTime)
{
return ((dateTime));
}
06DateTime, date part in date time + time to date time DateTime
This method is to extend DateTime datetime, take its date part, and then complement the specified TimeOnly time, and then finally to DateTime, the code is as follows:
//DateTime in the date part of the time + time to DateTime
public static DateTime ToDateTime(this DateTime dateTime, TimeOnly timeOnly)
ToDateTime(this DateTime dateTime, TimeOnly timeOnly)
return (dateTime).ToDateTime(timeOnly);
}
07, date + date time in the time part of the conversion to date time DateTime
This method is an extension of DateTime datetime, take its time part, and then complement the specified DateOnly date, and then finally converted to DateTime, the code is as follows:
//Date + date time in the time part to date time
public static DateTime ToDateTime(this DateTime dateTime, DateOnly dateOnly)
{
return ((dateTime));
}
08, DateTime to DateTime
This method is an extension of DateTime datetime, take its date part to DateOnly, the code is as follows:
//DateTime to date, retain the date part of the date time
public static DateOnly ToDateOnly(this DateTime dateTime)
{
return (dateTime);
}
09DateTime
This method is to extend DateTime datetime, take its time part to TimeOnly, the code is as follows:
//DateTime to time, retain the time part of the date time
public static TimeOnly ToTimeOnly(this DateTime dateTime)
{
return (dateTime);
}
Although most of the extension methods above are simple calls to native methods, providing them through extension methods does improve the efficiency of coding, and the code will also be based on the simplicity of the code.
10Code structure design and organization
The design and organization of code structure is an important part of the software development process, which will directly affect the readability, maintainability, extensibility, testability, and efficiency of teamwork.
So far we have close to 30 extension methods on time, with many more to come, all of which are currently placed under the DateTimeExtension class, as shown below:
Here you can already feel the code structure seems very confusing, the intuitive feeling is poor readability, no structure will inevitably lead to expansion difficulties.
There may be people who put forward a different point of view, these are not a lot of static methods, only layout neat on the line, in fact, not, we can with the help of a number of small skills to the collection of methods structured to manage up.
Structured management one implementation will do - classification. Below we categorize the existing code.
Classification is a technical work, how to choose the classification criteria is very critical, for example, we can be divided according to the type of input parameter for long type conversion, for DateTime type conversion, for string type conversion; can also be categorized according to the function of the time and timestamps are converted to each other is a class of time and strings are converted to each other is a class of DateTime and DateOnly and TimeOnly is a class of conversion and so on.
1. Sub-categorization
My idea is like this, first of all, through the partial class partial DateTimeExtension according to the type of input parameter into several categories.
The code is organized as follows:
2. Sub-categories
After divided into large categories found long type in the method is still very large, so we continue to categorize it, we are divided into two categories according to the different functions: to local date and time and to UTC date and time. We can use the code-folding preprocessing directives #region and #endregion to deal with small categories.
After the code is organized, the effect is as follows:
From the beginning of a bunch of methods to the current structure of the hierarchy has been very clear, it is also very easy to read, later maintenance and expansion can be very easy to quickly find the appropriate place to deal with.
11Unit Tests
As an open source code, our first priority should be to ensure the correctness of the code, so unit testing is essential. Unit testing not only ensures the correctness of our code, but also promotes our code to write with the robust.
For the unit tests we organize the code in the same categorical way and the code is roughly as follows:
Because the specific test cases are relatively simple, I won't take them out and explain them here.
The better the test cases are written, the more reliable our code will be, so when we write unit tests, we should try to cover all the logic as much as possible, to cover as much as possible to some special cases, the more we think about the higher the quality of the code can be.
12Documentation
As an open source code, but also need a good documentation, so that others can easily use, the current documentation is still missing, I will continue to add on.
When the code structure design and organization, unit testing, documentation of these three parts are done, I feel that this open source code will meet the minimum requirements of open source to others. I will focus on these three parts of the back of the open source code , I hope to be able to share some high-quality open source code.
I'll upload the library to Nuget later, so you can search for it and use it directly.
classifier for sums of money: The test method code as well as the sample source code have been uploaded to the code repository for those who are interested./hugogoos/Ideal