Talk about some of the things that .netcore Pit of system time and file path

Talk .netcore Pit something that the system time and file path

Hi, junior partner Hello everybody, recently busy working for a long time and there is no point to share things. This weekend plus two days of classes. The company's new projects are based on .netcore to develop, in the development process, also stepped on some pit, two pits in advance summary, these two pits are questions about compatibility under Linux (CentOS) and the windows. Our development environment interface call the beginning has been deployed windows environment to run as usual, but to deploy under Linux (CentOS) environment, there have been these two issues, in fact, the problem is simple: Get the system time, 8 less actual time hours; file path name of the file to be identified. Here's a simple solution to share, in fact, if you saw that a very simple reason for sharing it, when you begin user .netcore, can have a prompt effect, hey!

 

A, the DateTime.Now less time acquisition system for eight hours

.net core project, to deploy Linux ( when on CentOS), and found time to get DateTime.Now inconsistent with Windows, get to the actual system time less than 8 hours time system, find it a problem, we first time think of time zone differences. Internet search, I found there are a lot of small partners encountered the same problem, have given the corresponding solutions, as follows:

Specific reason: different time zones both Linux and Windows used in both time zones are: Linux: the IANA , Windows: Windows Time Zone IDs. This is the ultimate culprit it!

After finding the cause, then how to solve it? Very simple way is to use the same two time zones do not get away Well, ultimately the uniform application of IANA, on implementation can use third-party libraries: NodaTime. Specific codes are as follows:

       

/// <Summary> 

        /// Get the current time 

        /// </ Summary> 

        /// <Returns> System Current time </ Returns> 

        public static GetSysDateTimeNow the DateTime () 

        { 

            the Instant SystemClock.Instance.GetCurrentInstant = now () ; 

            var shanghaiZone DateTimeZoneProviders.Tzdb = [ "Asia / of Shanghai"]; 

            return now.InZone (shanghaiZone) .ToDateTimeUnspecified (); 

      }

  

It is not so easy?

In fact, we use the time when there will be a variety of ways, will do a lot of the time format conversion, such as: yyyy-MM-dd HH: mm: SS format time, time and time stamp conversion and so on. In order to unify the standard operation, the actual project, we operate according to the actual time required to do a unified package, of course, in the eyes of many is not much technical content, but also oh, its purpose is to achieve a unified control, convenience management, improve reusability of code. Now I also posted the code, if necessary, you can refer to, and I also generated a bag, put it on Nuget, the package name (XYH.Tools.DateTimeTools), if necessary, can block down use.

I have uploaded to the source on GitHub, we are interested can file down

Source Address: https://github.com/xuyuanhong0902/XYH.Tools.git

Source:

/ * ================================================ ============================== 
 * functional description: All operations related to the time of collection   
 * creator: journey exchange program to practice micro signal: 15908150902 
 * creation date: 2020-03-08 
 * the CLR Version: 1.0 
 * ================================ * ============================================== / 

a using NodaTime ; 
the using the System; 

/// <Summary> 
/// common help class 
/// </ summary> 
namespace XYH.Tools.DateTimeTools 
{ 
    /// <Summary> 
    /// time-related action class 
    /// </ summary > 
    public static class DateTimeTools 
    { 
        #region method of acquiring the current time of several system (return time + time formatted string) 

        /// <Summary> 
        /// Get the current time
        /// </ Summary> 
        /// <Returns> System Current time </ Returns> 
        public static GetSysDateTimeNow the DateTime () 
        { 
            the Instant SystemClock.Instance.GetCurrentInstant = now (); 
            var shanghaiZone DateTimeZoneProviders.Tzdb = [ "Asia / of Shanghai" ]; 
            return now.InZone (shanghaiZone) .ToDateTimeUnspecified (); 
        } 

        /// <Summary> 
        /// Get the current time 24-hour clock format string is formatted as (yyyy-mM-dd HH: mm: ss .fff) 
        /// </ Summary> 
        /// <Returns> current system time formatted string (the mM-dd-YYYY HH: mm: ss.fff) </ Returns> 
        public static string GetSysDateTimeNowStringYMD24HMSF () 
        { 
            return GetSysDateTimeNow ().ToStringYMD24HMSF(); 
        } 

        /// <Summary> 
        /// Get the current time in 12-hour format string is formatted as (the MM-dd-YYYY HH: mm: ss.fff) 
        /// </ Summary> 
        /// <Returns> current system formatted time string (the mM-dd-YYYY HH: mm: ss.fff) </ Returns> 
        public static string GetSysDateTimeNowStringYMD12HMSF (the this the DateTime time) 
        { 
            . GetSysDateTimeNow return () ToStringYMD12HMSF (); 
        } 

        /// <Summary> 
        // / Get the current time 24-hour clock format string is formatted as (the mM-dd-YYYY HH: mm: SS) 
        /// </ Summary> 
        /// <Returns> the current system time formatted string ( the mM-dd-HH YYYY: mm: SS) </ Returns> 
        public static String GetSysDateTimeNowStringYMD24HMS (the this the DateTime Time) 
        { 
            return GetSysDateTimeNow () ToStringYMD24HMS ();.
        } 

        /// <Summary> 
        /// Get the current time in 12-hour format string is formatted as (the MM-dd-YYYY HH: mm: SS) 
        /// </ Summary> 
        /// <Returns> the current system time formatted string (the mM-dd-YYYY HH: mm: SS) </ Returns> 
        public static string GetSysDateTimeNowStringYMD12HMS (the this the DateTime time) 
        { 
            . GetSysDateTimeNow return () ToStringYMD12HMS (); 
        } 

        /// <Summary> 
        /// Get the current time is formatted as string format (yyyy-MM-dd) 
        /// </ Summary> 
        /// <Returns> current system time formatted string (yyyy-MM-dd) </ Returns> 
        public static String GetSysDateTimeNowStringYMD (the this the DateTime Time) 
        { 
            return GetSysDateTimeNow () ToStringYMD ().;
        } 

        #Endregion 
        /// <Returns>Formatted string time (yyyy-MM-dd hh: mm: ss.fff) </ returns>
 
        #region several extension format method of the DateTime

        /// <Summary> 
        /// 24-hour time format is formatted as (the MM-dd-YYYY HH: mm: ss.fff) 
        /// </ Summary> 
        /// <param name = "Time" > time format is </ param> 
        /// <Returns> formatted string time (the mM-dd-YYYY HH: mm: ss.fff) </ Returns> 
        public static string ToStringYMD24HMSF (the this the DateTime time) 
        { 
            time.ToString return ( "the mM-dd-YYYY HH: mm: ss.fff"); 
        } 

        /// <Summary> 
        /// 12-hour time format is formatted as (yyyy-mM-dd hh: mm : ss.fff) 
        /// </ Summary> 
        /// <param name = "time"> formatted time </ param>
        {
            return time.ToString("yyyy-MM-dd hh:mm:ss.fff");
        ToStringYMD12HMSF static String public (the this the DateTime Time) 
        } 

        /// <Summary> 
        /// 24-hour time format is formatted as (the MM-dd-YYYY HH: mm: SS) 
        /// </ Summary> 
        // / <param name = "time" > formatted time </ param> 
        /// <Returns> formatted string time (the mM-dd-YYYY HH: mm: SS) </ Returns> 
        public static string ToStringYMD24HMS (the this the DateTime time) 
        { 
            return time.ToString ( "the mM-dd-YYYY HH: mm: SS"); 
        } 

        /// <Summary> 
        /// 12-hour time format is formatted as (yyyy-MM- HH dd: mm: SS) 
        /// </ Summary> 
        /// <param name = "time"> formatted time </ param> 
        /// <returns> the formatted string time (yyyy-MM-dd hh: mm: ss) </ returns>
        ToStringYMD12HMS static String public (the this the DateTime Time) 
        {
            return time.ToString("yyyy-MM-dd hh:mm:ss");
        } 

        /// <Summary> 
        /// format is formatted as time (YYYY-the MM-dd) 
        /// </ Summary> 
        /// <param name = "Time" > formatted time </ param> 
        /// <Returns> string time (yyyy-MM-dd) < / returns> formatted 
        public static string ToStringYMD (the this the DateTime time) 
        { 
            return time.ToString ( "YYYY dd--MM "); 
        } 

        #endregion 

        #region obtain a timestamp 

        /// <Summary> 
        /// Get timestamp (in seconds) 
        /// </ Summary> 
        /// <Returns> second time stamp </ returns>
        public static long GetSecondTimestamp()
        { 
            // to 1970-1-1 for the second time beginning with the difference between the current system time is the second time stamp
            TS = GetSysDateTimeNow the TimeSpan () - the DateTime new new (1970,. 1,. 1, 0, 0, 0, 0); 
            return Convert.ToInt64 (ts.TotalSeconds); 
        } 

        /// <Summary> 
        /// Get timestamp (in milliseconds ) 
        /// </ Summary> 
        /// <Returns> ms time stamp </ Returns> 
        public static Long GetMilliSecondTimestamp () 
        { 
            // milliseconds as the time difference to the start 1970-1-1 current time is the same system ms stamp 
            the TimeSpan GetSysDateTimeNow = TS () - new new the DateTime (1970,. 1,. 1, 0, 0, 0, 0); 
            return Convert.ToInt64 (ts.TotalMilliseconds); 
        } 

        #endregion 

        #region a time stamp is converted to a 

        /// <Summary> 
        /// converts a second timestamp format time (sec)
        /// </ Summary> 
        /// <param name = "secondTimestamp"> second time stamp </ param> 
        /// <Returns> time converted </ Returns> 
        public static the DateTime? SecondStampToDateTime (Long secondTimestamp) 
        { 
            / / determination to make a simple 
            IF (secondTimestamp <= 0) 
            { 
                return null; 
            } 

            // start the time to 1970-1-1, by calculating the time difference with which to calculate the corresponding time 
            dateTime dateTime = new System.DateTime (1970,. 1,. 1, 0, 0, 0, 0); 
            dateTime = dateTime.AddSeconds (secondTimestamp) .ToLocalTime (); 
            return dateTime; 
        } 
        /// </ summary>
 
        /// <Summary>
        /// Converts a string of second time stamp format (s) 
        /// <param name = "secondTimestampStr"> sec string time stamp </ param> 
        /// <Returns> time converted </ returns > 
        ? SecondStampToDateTime the DateTime public static (string secondTimestampStr) 
        { 
            // if it is empty, then the direct return null 
            IF (String.IsNullOrEmpty (secondTimestampStr)) 
            { 
                return null; 
            } 

            // first string is converted to a digital timestamp 
            long secondTimestamp = 0 ; 
            long.TryParse (secondTimestampStr, secondTimestamp OUT); 

            // call to 
            return SecondStampToDateTime (secondTimestamp); 
        } 

        /// <Summary> 
        /// converts a string to millisecond time stamp format (ms) 
        /// </ Summary>
        /// <param name = "secondTimestampStr" > msec time stamp string </ param> 
        /// <Returns> time converted </ Returns> 
        public static the DateTime? MilliSecondStampToDateTime (Long secondTimestamp) 
        { 
            // do a simple Analyzing 
            IF (secondTimestamp <= 0) 
            { 
                return null; 
            } 

            // start the time to 1970-1-1, by calculating the time difference with which to calculate the corresponding time 
            dateTime dateTime = new System.DateTime (1970, 1, . 1, 0, 0, 0, 0); 
            dateTime = dateTime.AddMilliseconds (secondTimestamp) .ToLocalTime (); 

            return dateTime; 
        } 
        /// </ Summary>

        /// <summary>
        /// will be converted to a time stamp format millisecond (ms)
        /// <param name = "milliSecondStampStr" > msec time stamp </ param> 
        /// <Returns> time converted </ Returns> 
        public static the DateTime? MilliSecondStampToDateTime (String milliSecondStampStr) 
        { 
            // if it is empty, then directly returns null 
            IF (String.IsNullOrEmpty (milliSecondStampStr)) 
            { 
                return null; 
            } 

            // string is first converted into a digital timestamp 
            Long milliSecondStamp = 0; 
            long.TryParse (milliSecondStampStr, milliSecondStamp OUT); 

            // call to 
            return MilliSecondStampToDateTime (milliSecondStamp) ; 
        } 

        #endregion 
    } 
}

  

Second, the file path name of the file to be identified

Haha, recently encountered an interesting thing, that is, on Windows, the file path of creation, is correct, but to deploy CentOS, create a file, all paths have become the name of the file, all files in the root the directory.

Internet to find the reasons, is the file path to the left and right slash slash problem. In the Windows either left or right slash slash, no problems, but supports only the right slash in linux, the path in the code used in the operation, have the right to modify the unified slash, the problem is solved. File Path 1/2 file path / filename

Third, the summary

In retrospect these two issues, compatibility issues are systematic, carefully think about, is a habitual problem, especially if the file path to this problem, we have to get used to using forward slashes.

We'll write when .net program, regardless of whether it will implement the use of .netcore linux system deployment, we should also think of compatibility of different systems are used in the realization of a common way to achieve, then later doing the project upgrade, migration time, it would be less trouble. Hey, hang onto this today, follow me share with you the other .netcore real step on the pit. Thank you for reading.

 

 

Hi, junior partner Hello everybody, recently busy working for a long time and there is no point to share things. This weekend plus two days of classes. The company's new projects are based on .netcore to develop, in the development process, also stepped on some pit, two pits in advance summary, these two pits are questions about compatibility under Linux (CentOS) and the windows. Our development environment interface call the beginning has been deployed windows environment to run as usual, but to deploy under Linux (CentOS) environment, there have been these two issues, in fact, the problem is simple: Get the system time, 8 less actual time hours; file path name of the file to be identified. Here's a simple solution to share, in fact, if you saw that a very simple reason for sharing it, when you begin user .netcore, can have a prompt effect, hey!

Guess you like

Origin www.cnblogs.com/xiaoXuZhi/p/netcoredatetime.html