chat. System time and file path of netcore mining pit
Hi, dear friends, I've been busy recently. I haven't shared anything with you for a long time. I've worked two extra days this weekend. The company's new projects are all based on netcore to develop, in the development process, we also stepped into some pits. Here we summarize two pits, which are about the compatibility between Linux (CentOS) and windows. Our initial development environment interface call has always been deployed in the windows environment, and everything runs normally. However, when deployed in the Linux (CentOS) environment, these two problems arise. In fact, the problem is also simple: the actual time is 8 hours less than the system time; The file path {is recognized as a file name. Here's a simple solution to share. In fact, as long as you look at it, you will find that it's very simple. The reason why you share it is when you start users netcore can have a prompt function, hey hey!
1, Datetime Now 8 hours less time to acquire the system
. net core project. When deploying to Linux (CentOS), datetime The time obtained by now is inconsistent with that obtained by Windows. The obtained system time is 8 hours less than the actual system time. When we find this problem, we first think of the time zone difference. After searching the Internet, I found that many small partners encountered the same problems, and some gave corresponding solutions, as follows:
The specific reason is that Linux and windows adopt different time zones, which are Linux: IANA and Windows:Windows time zone IDs. This is the ultimate culprit!
After finding the reason, how to solve it? The way is very simple, that is, if they use the same time zone, it's over. Finally, IANA is adopted uniformly. In terms of implementation, we can use the third-party library: NodaTime. The specific implementation code is as follows:
/// <summary> ///Get the current time of the system /// </summary> ///< returns > current system time < / returns > public static DateTime GetSysDateTimeNow() { Instant now = SystemClock.Instance.GetCurrentInstant(); var shanghaiZone = DateTimeZoneProviders.Tzdb["Asia/Shanghai"]; return now.InZone(shanghaiZone).ToDateTimeUnspecified(); }
Is it so easy?
In fact, when we use time, there are many ways, and we also do a lot of format conversion for time, such as yyyy MM DD HH: mm: SS formatting time, mutual conversion of time and timestamp, and so on. In order to unify and standardize the operation, in the actual project, we have made a unified package for the operation of time according to the actual needs. Of course, in the view of many people, it does not have much technical content. Oh, its purpose is to realize unified control, facilitate management and improve the reusability of code. Now I also post the code. If necessary, you can refer to it. At the same time, I also generated a package and put it on Nuget. The package name is (XYH.Tools.DateTimeTools). If necessary, it can be blocked for use.
I have uploaded the source code to GitHub, and those who are interested can file it down
Source address: https://github.com/xuyuanhong0902/XYH.Tools.git
Source code:
/* ============================================================================== * Function Description: a collection of related operations at all times * Creator: program cultivation journey communication micro signal: 15908150902 * Creation date: March 8, 2020 * CLR Version :1.0 * ==============================================================================*/ using NodaTime; using System; /// <summary> ///Public help class /// </summary> namespace XYH.Tools.DateTimeTools { /// <summary> ///Time dependent operation class /// </summary> public static class DateTimeTools { #region several methods to obtain the current time of the system (return time + formatted time string) /// <summary> ///Get the current time of the system /// </summary> ///< returns > current system time < / returns > public static DateTime GetSysDateTimeNow() { Instant now = SystemClock.Instance.GetCurrentInstant(); var shanghaiZone = DateTimeZoneProviders.Tzdb["Asia/Shanghai"]; return now.InZone(shanghaiZone).ToDateTimeUnspecified(); } /// <summary> ///Get the current time format string of the system. The 24-hour format is formatted as (yyyy MM DD HH: mm: SS. FFF) /// </summary> ///< returns > time string currently formatted by the system (yyyy MM DD HH: mm: SS. FFF) < / returns > public static string GetSysDateTimeNowStringYMD24HMSF() { return GetSysDateTimeNow().ToStringYMD24HMSF(); } /// <summary> ///Get the current time format string of the system. The 12 hour system is formatted as (yyyy MM DD HH: mm: SS. FFF) /// </summary> ///< returns > time string currently formatted by the system (yyyy MM DD HH: mm: SS. FFF) < / returns > public static string GetSysDateTimeNowStringYMD12HMSF(this DateTime time) { return GetSysDateTimeNow().ToStringYMD12HMSF(); } /// <summary> ///Get the current time format string of the system. The 24-hour format is formatted as (yyyy MM DD HH: mm: SS) /// </summary> ///< returns > time string currently formatted by the system (yyyy MM DD HH: mm: SS) < / returns > public static string GetSysDateTimeNowStringYMD24HMS(this DateTime time) { return GetSysDateTimeNow().ToStringYMD24HMS(); } /// <summary> ///Get the current time format string of the system. The 12 hour system is formatted as (yyyy MM DD HH: mm: SS) /// </summary> ///< returns > time string currently formatted by the system (yyyy MM DD HH: mm: SS) < / returns > public static string GetSysDateTimeNowStringYMD12HMS(this DateTime time) { return GetSysDateTimeNow().ToStringYMD12HMS(); } /// <summary> ///Get the current time format string of the system, which is formatted as (yyyy MM DD) /// </summary> ///< returns > time string currently formatted by the system (yyyy MM DD) < / returns > public static string GetSysDateTimeNowStringYMD(this DateTime time) { return GetSysDateTimeNow().ToStringYMD(); } #endregion #Several format methods of region DateTime extension /// <summary> ///The 24-hour time format is formatted as (yyyy MM DD HH: mm: SS. FFF) /// </summary> ///< param name = "time" > formatted time < / param > ///< returns > formatted time string (yyyy MM DD HH: mm: SS. FFF) < / returns > public static string ToStringYMD24HMSF(this DateTime time) { return time.ToString("yyyy-MM-dd HH:mm:ss.fff"); } /// <summary> ///Time format 12 hour format is formatted as (yyyy MM DD HH: mm: SS. FFF) /// </summary> ///< param name = "time" > formatted time < / param > ///< returns > formatted time string (yyyy MM DD HH: mm: SS. FFF) < / returns > public static string ToStringYMD12HMSF(this DateTime time) { return time.ToString("yyyy-MM-dd hh:mm:ss.fff"); } /// <summary> ///The 24-hour time format is formatted as (yyyy MM DD HH: mm: SS) /// </summary> ///< param name = "time" > formatted time < / param > ///< returns > formatted time string (yyyy MM DD HH: mm: SS) < / returns > public static string ToStringYMD24HMS(this DateTime time) { return time.ToString("yyyy-MM-dd HH:mm:ss"); } /// <summary> ///Time format 12 hour format is formatted as (yyyy MM DD HH: mm: SS) /// </summary> ///< param name = "time" > formatted time < / param > ///< returns > formatted time string (yyyy MM DD HH: mm: SS) < / returns > public static string ToStringYMD12HMS(this DateTime time) { return time.ToString("yyyy-MM-dd hh:mm:ss"); } /// <summary> ///The time format is formatted as (yyyy MM DD) /// </summary> ///< param name = "param" > formatted time ///< returns > formatted time string (yyyy MM DD) < / returns > public static string ToStringYMD(this DateTime time) { return time.ToString("yyyy-MM-dd"); } #endregion #region get timestamp /// <summary> ///Get timestamp (seconds) /// </summary> ///< returns > second timestamp < / returns > public static long GetSecondTimestamp() { // Starting from 1970-1-1, the second difference from the current time of the system is the second timestamp TimeSpan ts = GetSysDateTimeNow() - new DateTime(1970, 1, 1, 0, 0, 0, 0); return Convert.ToInt64(ts.TotalSeconds); } /// <summary> ///Get timestamp (MS) /// </summary> ///< returns > millisecond timestamp < / returns > public static long GetMilliSecondTimestamp() { // Starting from 1970-1-1, the millisecond difference from the current time of the system is the millisecond timestamp TimeSpan ts = GetSysDateTimeNow() - new DateTime(1970, 1, 1, 0, 0, 0, 0); return Convert.ToInt64(ts.TotalMilliseconds); } #endregion #region converts a timestamp to a time /// <summary> ///Convert a second timestamp to time format (seconds) /// </summary> ///< param name = "secondtimestamp" > second timestamp < / param > ///< returns > time after conversion < / returns > public static DateTime? SecondStampToDateTime(long secondTimestamp) { // Make a simple judgment if (secondTimestamp <= 0) { return null; } // Starting from 1970-1-1, calculate the corresponding time by calculating the time difference DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0); dateTime = dateTime.AddSeconds(secondTimestamp).ToLocalTime(); return dateTime; } /// <summary> ///Convert a string second timestamp to time format (seconds) /// </summary> ///< param name = "secondtimestampstr" > string second timestamp < / param > ///< returns > time after conversion < / returns > public static DateTime? SecondStampToDateTime(string secondTimestampStr) { // If it is empty, null will be returned directly if (string.IsNullOrEmpty(secondTimestampStr)) { return null; } // First, convert the string timestamp to a number long secondTimestamp = 0; long.TryParse(secondTimestampStr, out secondTimestamp); // call return SecondStampToDateTime(secondTimestamp); } /// <summary> ///Converts a string timestamp to a time format in milliseconds /// </summary> ///< param name = "secondtimestampstr" > string millisecond timestamp < / param > ///< returns > time after conversion < / returns > public static DateTime? MilliSecondStampToDateTime(long secondTimestamp) { // Make a simple judgment if (secondTimestamp <= 0) { return null; } // Starting from 1970-1-1, calculate the corresponding time by calculating the time difference DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0); dateTime = dateTime.AddMilliseconds(secondTimestamp).ToLocalTime(); return dateTime; } /// <summary> ///Convert a millisecond timestamp to time format (milliseconds) /// </summary> ///< param name = "millisecondstampstr" > millisecond timestamp < / param > ///< returns > time after conversion < / returns > public static DateTime? MilliSecondStampToDateTime(string milliSecondStampStr) { // If it is empty, null will be returned directly if (string.IsNullOrEmpty(milliSecondStampStr)) { return null; } // First, convert the string timestamp to a number long milliSecondStamp = 0; long.TryParse(milliSecondStampStr, out milliSecondStamp); // call return MilliSecondStampToDateTime(milliSecondStamp); } #endregion } }
2, The file path is recognized as the file name
Haha, there is another interesting thing recently, that is, on Windows, the creation of file paths is correct, but when deployed to CentOS, all paths of the created files become file names, and all files are in the root directory.
I found the reason online, which is the problem of left slash and right slash in the file path. On Windows, there is no problem with left slash or right slash, but in linux, only right slash is supported. The path operations used in the code are uniformly modified to right slash, and the problem is solved. File path 1 / file path 2 / file name
3, Summary
Looking back, these two problems are both system compatibility problems. When you think about it carefully, it is also a habitual problem, especially the problem of file path. We should get used to using the right slash.
We'll write it later net program, whether it will be adopted or not netcore realizes the deployment of linux system. We should also think about the compatibility of different systems. We adopt a common way to realize it. Then we will have less trouble in project upgrading and system migration in the future. Hey, hey, I'll come here first today. I'll share other information later netcore is the pit of actual combat. Thank you for reading.
Hi, dear friends, I've been busy recently. I haven't shared anything with you for a long time. I've worked two extra days this weekend. The company's new projects are all based on netcore to develop, in the development process, we also stepped into some pits. Here we summarize two pits, which are about the compatibility between Linux (CentOS) and windows. Our initial development environment interface call has always been deployed in the windows environment, and everything runs normally. However, when deployed in the Linux (CentOS) environment, these two problems arise. In fact, the problem is also simple: the actual time is 8 hours less than the system time; The file path {is recognized as a file name. Here's a simple solution to share. In fact, as long as you look at it, you will find that it's very simple. The reason why you share it is when you start users netcore can have a prompt function, hey hey!
END
In order to communicate more, you are welcome to pay attention to my official account.