Engaged in software development for so many years, usually also accumulated some convenient to their own rapid development of the help class, has been thinking of what way to share out, so there is this series of articles, the back I will be "open source - Ideal library" series of articles to share some of what I think is more mature, more convenient, better code, if you feel that there is a reference to the place can be integrated into their own public codebase, and at the same time I will also be released as a Nuget package for direct download and use.
Mainly includes: public, documentation, ORM, SqlSugar, timed tasks, Redis, Mqtt, SignalR and other library packages, the back may also be appropriate to delete .
Today we are going to share the relevant wrappers in the public libraries for time conversions, mainly about the interconversion between local and UTC dates, times and timestamps and strings.
01Date time to timestamp (seconds)
This method is to convert the date time DateTime to a 10-bit timestamp, i.e. a second timestamp, with the following code:
/// <summary>
/// datetime to timestamp(unit of angle or arc equivalent one sixtieth of a degree)
/// </summary>
/// <param name="dateTime">Date Time</param>
/// <returns>timestamp(unit of angle or arc equivalent one sixtieth of a degree)</returns>
public static long ToUnixTimestampBySeconds(this DateTime dateTime)
{
var dto = new DateTimeOffset(dateTime);
return ();
}
02, datetime to timestamp (milliseconds)
This method is to convert the datetime DateTime to a 13-bit timestamp, i.e. a millisecond timestamp, with the following code:
/// <summary>
/// datetime to timestamp(millisecond)
/// </summary>
/// <param name="dateTime">Date Time</param>
/// <returns>timestamp(millisecond)</returns>
public static long ToUnixTimestampByMilliseconds(this DateTime dateTime)
{
var dto = new DateTimeOffset(dateTime);
return ();
}
03Time Stamp (sec) to Local Date Time
This method is to convert a 10-bit second timestamp to a local datetime DateTime with the following code:
/// <summary>
/// Timestamp (seconds) to local datetime
/// </summary>
/// <param name="timestamp">Timestamp (seconds)</param>
/// <returns>Local datetime</returns>
public static DateTime ToLocalTimeDateTimeBySeconds(this long timestamp)
{
var dto = (timestamp);; return ().
return ().DateTime;
}
04Time stamps (milliseconds) to local datetime.
This method is to convert a 13-bit millisecond timestamp to a local datetime DateTime with the following code:
/// <summary>
/// Timestamp (milliseconds) to local datetime
/// </summary>
/// <param name="timestamp">Timestamp (milliseconds)</param>
/// <returns> local datetime</returns>
public static DateTime ToLocalTimeDateTimeByMilliseconds(this long timestamp)
{
var dto = (timestamp);; return ().DateTimeByMilliseconds().
return ().DateTime;
}
05Time Stamp (sec) to UTC Date Time
This method is to convert a 10-bit second timestamp to a UTC date time DateTime with the following code:
/// <summary>
/// timestamp(unit of angle or arc equivalent one sixtieth of a degree)classifier for repeated actionsUTCDate Time
/// </summary>
/// <param name="timestamp">timestamp(unit of angle or arc equivalent one sixtieth of a degree)</param>
/// <returns>UTCDate Time</returns>
public static DateTime ToUniversalTimeDateTimeBySeconds(this long timestamp)
{
var dto = (timestamp);
return ().DateTime;
}
06Time stamp (milliseconds) to UTC datetime.
This method is to convert a 13-bit millisecond timestamp to a UTC datetime DateTime with the following code:
/// <summary>
/// timestamp(millisecond)classifier for repeated actionsUTCDate Time
/// </summary>
/// <param name="timestamp">timestamp(millisecond)</param>
/// <returns>UTCDate Time</returns>
public static DateTime ToUniversalTimeDateTimeByMilliseconds(this long timestamp)
{
var dto = (timestamp);
return ().DateTime;
}
07Time stamp (seconds) to local date
The method is to convert the 10-bit second timestamp to a local date DateOnly with the following code:
/// <summary>
/// timestamp(unit of angle or arc equivalent one sixtieth of a degree)Date of transfer to local
/// </summary>
/// <param name="timestamp">timestamp(unit of angle or arc equivalent one sixtieth of a degree)</param>
/// <returns>local date</returns>
public static DateOnly ToLocalTimeDateBySeconds(this long timestamp)
{
var dt = ();
return (dt);
}
08Time Stamp (milliseconds) to Local Date
The method is to convert the 13-bit millisecond timestamp to a local date DateOnly with the following code:
/// <summary>
/// timestamp(millisecond)Date of transfer to local
/// </summary>
/// <param name="timestamp">timestamp(millisecond)</param>
/// <returns>local date</returns>
public static DateOnly ToLocalTimeDateByMilliseconds(this long timestamp)
{
var dt = ();
return (dt);
}
09Time Stamp (seconds) to UTC Date
This method is to convert a 10-bit second timestamp to a UTC date DateOnly with the following code:
/// <summary>
/// timestamp(unit of angle or arc equivalent one sixtieth of a degree)classifier for repeated actionsUTCdates
/// </summary>
/// <param name="timestamp">timestamp(unit of angle or arc equivalent one sixtieth of a degree)</param>
/// <returns>UTCdates</returns>
public static DateOnly ToUniversalTimeDateBySeconds(this long timestamp)
{
var dt = ();
return (dt);
}
10Time Stamp (milliseconds) to UTC Date
This method is to convert 13-bit millisecond timestamp to UTC date DateOnly with the following code:
/// <summary>
/// timestamp(millisecond)classifier for repeated actionsUTCdates
/// </summary>
/// <param name="timestamp">timestamp(millisecond)</param>
/// <returns>UTCdates</returns>
public static DateOnly ToUniversalTimeDateByMilliseconds(this long timestamp)
{
var dt = ();
return (dt);
}
11Time stamp (seconds) to local time
This method is to convert 10-bit second timestamp to local time TimeOnly with the following code:
/// <summary>
/// timestamp(unit of angle or arc equivalent one sixtieth of a degree)relocate to local time
/// </summary>
/// <param name="timestamp">timestamp(unit of angle or arc equivalent one sixtieth of a degree)</param>
/// <returns>local time</returns>
public static TimeOnly ToLocalTimeTimeBySeconds(this long timestamp)
{
var dt = ();
return (dt);
}
12Time Stamp (milliseconds) to Local Time
This method is to convert the 13-bit millisecond timestamp to local time TimeOnly with the following code:
/// <summary>
/// timestamp(millisecond)relocate to local time
/// </summary>
/// <param name="timestamp">timestamp(millisecond)</param>
/// <returns>local time</returns>
public static TimeOnly ToLocalTimeTimeByMilliseconds(this long timestamp)
{
var dt = ();
return (dt);
}
13Timestamp (seconds) to UTC Time
This method is to convert the 10-bit second timestamp to UTC time TimeOnly with the following code:
/// <summary>
/// timestamp(unit of angle or arc equivalent one sixtieth of a degree)classifier for repeated actionsUTCtiming
/// </summary>
/// <param name="timestamp">timestamp(unit of angle or arc equivalent one sixtieth of a degree)</param>
/// <returns>UTCtiming</returns>
public static TimeOnly ToUniversalTimeTimeBySeconds(this long timestamp)
{
var dt = ();
return (dt);
}
14Time Stamp (milliseconds) to UTC Time
The method is to convert the 13-bit millisecond timestamp to UTC time TimeOnly with the following code:
/// <summary>
/// timestamp(millisecond)classifier for repeated actionsUTCtiming
/// </summary>
/// <param name="timestamp">timestamp(millisecond)</param>
/// <returns>UTCtiming</returns>
public static TimeOnly ToUniversalTimeTimeByMilliseconds(this long timestamp)
{
var dt = ();
return (dt);
}
15If the conversion fails, it returns null.
The method is to convert the string to DateTime DateTime, the conversion fails to return empty, the specific code is as follows:
/// <summary>
/// String to datetime,Returns null if conversion fails
/// </summary>
/// <param name="source">String to be converted</param>
/// <returns>Date Time</returns>
public static DateTime? ToDateTime(this string source)
{
if ((source, , , out var date))
{
return date;
}
return default;
}
16If the conversion fails, the default date and time will be returned.
The method is to convert the string to DateTime DateTime, the conversion fails to return to the default date and time, the specific code is as follows:
/// <summary>
/// String to datetime,If the conversion fails, the default value is returned
/// </summary>
/// <param name="source">String to be converted</param>
/// <param name="dateTime">Default date and time</param>
/// <returns>Date Time</returns>
public static DateTime ToDateTimeOrDefault(this string source, DateTime dateTime)
{
if ((source, , , out var date))
{
return date;
}
return dateTime;
}
17Returns null if the conversion fails.
The method is to convert the string to a date DateOnly, the conversion fails to return empty, the specific code is as follows:
/// <summary>
/// String to Date,Returns null if conversion fails
/// </summary>
/// <param name="source">String to be converted</param>
/// <returns>dates</returns>
public static DateOnly? ToDateOnly(this string source)
{
if ((source, , , out var date))
{
return date;
}
return default;
}
18If the conversion fails, the default date will be returned.
The method is to convert the string to a date DateOnly, the conversion fails to return to the default date, the specific code is as follows:
/// <summary>
/// String to Date,If the conversion fails, the default date is returned
/// </summary>
/// <param name="source">String to be converted</param>
/// <param name="dateOnly">Default date</param>
/// <returns>dates</returns>
public static DateOnly ToDateOnlyOrDefault(this string source, DateOnly dateOnly)
{
if ((source, , , out var date))
{
return date;
}
return dateOnly;
}
19If the conversion fails, it returns null.
This method is to convert the string to a date TimeOnly, the conversion fails to return null, the specific code is as follows:
/// <summary>
/// string to time,Returns null if conversion fails
/// </summary>
/// <param name="source">String to be converted</param>
/// <returns>timing</returns>
public static TimeOnly? ToTimeOnly(this string source)
{
if ((source, , , out var date))
{
return date;
}
return default;
}
20If the conversion fails, the default time is returned.
The method is to convert the string to a date TimeOnly, the conversion fails to return to the default time, the specific code is as follows:
/// <summary>
/// string to time,If the conversion fails, the default time is returned
/// </summary>
/// <param name="source">String to be converted</param>
/// <param name="timeOnly">default time</param>
/// <returns>timing</returns>
public static TimeOnly ToTimeOnlyOrDefault(this string source, TimeOnly timeOnly)
{
if ((source, , , out var date))
{
return date;
}
return timeOnly;
}
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