C# realizes the helper class for converting between Beijing time and timestamp

C# is used to convert Beijing time to timestamp, and convert timestamp to Beijing time. The timestamp lengths are 10 bits and 13 bits respectively.


 

using System;

namespace TimeConverter
{
    public static class TimestampConversionHelper
    {
        private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

        /// <summary>
        /// 将北京时间转换为时间戳(10位)
        /// </summary>
        /// <param name="beijingTime">北京时间</param>
        /// <returns>时间戳(10位)</returns>
        public static long ToUnixTimeSeconds(DateTime beijingTime)
        {
            var utcTime = beijingTime.ToUniversalTime();
            var elapsedTime = utcTime - UnixEpoch;
            return (long)elapsedTime.TotalSeconds;
        }

        /// <summary>
        /// 将北京时间转换为时间戳(13位)
        /// </summary>
        /// <param name="beijingTime">北京时间</param>
        /// <returns>时间戳(13位)</returns>
        public static long ToUnixTimeMilliseconds(DateTime beijingTime)
        {
            var utcTime = beijingTime.ToUniversalTime();
            var elapsedTime = utcTime - UnixEpoch;
            return (long)elapsedTime.TotalMilliseconds;
        }

        /// <summary>
        /// 将时间戳(10位)转换为北京时间
        /// </summary>
        /// <param name="unixTimeSeconds">时间戳(10位)</param>
        /// <returns>北京时间</returns>
        public static DateTime FromUnixTimeSeconds(long unixTimeSeconds)
        {
            var utcTime = UnixEpoch.AddSeconds(unixTimeSeconds);
            return utcTime.ToLocalTime();
        }

        /// <summary>
        /// 将时间戳(13位)转换为北京时间
        /// </summary>
        /// <param name="unixTimeMilliseconds">时间戳(13位)</param>
        /// <returns>北京时间</returns>
        public static DateTime FromUnixTimeMilliseconds(long unixTimeMilliseconds)
        {
            var utcTime = UnixEpoch.AddMilliseconds(unixTimeMilliseconds);
            return utcTime.ToLocalTime();
        }
    }
}

This helper class contains four methods:

  • ToUnixTimeSeconds: Convert Beijing time to timestamp (10 digits).
  • ToUnixTimeMilliseconds: Convert Beijing time to timestamp (13 digits).
  • FromUnixTimeSeconds: Convert the timestamp (10 digits) to Beijing time.
  • FromUnixTimeMilliseconds: Convert the timestamp (13 digits) to Beijing time.

These methods are static methods, so they can be called directly through the class name.

Call example:

Console.WriteLine("现在时间:"+DateTime.Now);
Console.WriteLine(TimestampConversionHelper.ToUnixTimeSeconds(DateTime.Now));
            Console.WriteLine(TimestampConversionHelper.ToUnixTimeMilliseconds(DateTime.Now));
Console.WriteLine(TimestampConversionHelper.FromUnixTimeSeconds(1686900730));
            Console.WriteLine(TimestampConversionHelper.FromUnixTimeMilliseconds(1686900730123));

The output shows the following:

Current time: 2023-06-16 15:50:27
1686901827
1686901827147
2023-06-16 15:32:10
2023-06-16 15:32:10

In this helper class, we have used DateTimeclasses and TimeSpanclasses for time and interval calculations. We also define a UnixEpochfield that represents the Unix epoch time (January 1, 1970 00:00:00 UTC). We use this field to calculate the timestamp.

In ToUnixTimeSecondsthe and ToUnixTimeMillisecondsmethod, we first convert Beijing time to Coordinated Universal Time (UTC) time, then calculate the elapsed time interval since the Unix epoch time, and convert it to seconds or milliseconds. Finally, we longreturn the timestamp as a type.

In the FromUnixTimeSecondsand FromUnixTimeMillisecondsmethod, we first use UnixEpochthe field and the timestamp to calculate the UTC time, and then convert it to the local time (Beijing time). Finally, we DateTimereturn Beijing time as a type.

This helper class can easily convert Beijing time and timestamp, so that we can easily handle timestamps in C# applications.

The above content is used to record your own learning and sharing, and sharing makes technology go further!

Technology comes from the innovation of freedom of thought, independent thinking, dialectical analysis, logical thinking and human freedom, to serve the various conveniences of human life, not to use technology to create ideological divides and siege to create stupidity; technology is good and bad, It is better for good people to use technology, and it is worse for bad people to use technology. Recommended books: George Orwell's "1984", there are multiple translations, just choose a good one you like; there are also Plato's "Utopia", Hayek's "The Road to Serfdom" (Hong Kong version) ) (The Hong Kong version is better translated than the mainland version, and the original English version is best if you have good English), and now it is sold in online stores! Reading good books, reading books that most people have never read and not in classrooms, and reading knowledge that is not available in this land can increase a kind of speculative thinking and wisdom, and only then can we get out of the cave of narrow thinking and prejudice!

Plato wrote the cave theory in the seventh volume of his masterpiece "Utopia": a group of people who were imprisoned and lived in the cave since childhood, under the light outside the cave, they saw black shadows when they looked inside, and black shadows when they looked outside. Bright sea and sky, the more you look, the brighter it is!

People who are tolerant are more confident and have more diverse thinking. A culture that is tolerant makes people stronger. The more confident you are, the more tolerant you are, the stronger you are!

Guess you like

Origin blog.csdn.net/m0_58015531/article/details/131248764