zoukankan      html  css  js  c++  java
  • C#根据时间产生有序的GUID编码

    public static Guid GenerateGuid()
    {
        byte[] guidArray = Guid.NewGuid().ToByteArray();
    
        var baseDate = new DateTime(1900, 1, 1);
        DateTime now = DateTime.Now;
        var days = new TimeSpan(now.Ticks - baseDate.Ticks);
        TimeSpan msecs = now.TimeOfDay;
    
        byte[] daysArray = BitConverter.GetBytes(days.Days);
        byte[] msecsArray = BitConverter.GetBytes((long)(msecs.TotalMilliseconds / 3.333333));
    
        Array.Reverse(daysArray);
        Array.Reverse(msecsArray);
    
        Array.Copy(daysArray,daysArray.Length - 2,guidArray,guidArray.Length - 6,2);
        Array.Copy(msecsArray,msecsArray.Length - 4,guidArray,guidArray.Length - 4,4);
    
        return new Guid(guidArray);
    }
    public static Guid GenerateGuid()
    {
        byte[] guidArray = Guid.NewGuid().ToByteArray();
     
        var baseDate = new DateTime(1900, 1, 1);
        DateTime now = DateTime.Now;
        var days = new TimeSpan(now.Ticks - baseDate.Ticks);
        TimeSpan msecs = now.TimeOfDay;
     
        byte[] daysArray = BitConverter.GetBytes(days.Days);
        byte[] msecsArray = BitConverter.GetBytes((long)(msecs.TotalMilliseconds / 3.333333));
     
        Array.Reverse(daysArray);
        Array.Reverse(msecsArray);
     
        Array.Copy(daysArray,daysArray.Length - 2,guidArray,guidArray.Length - 6,2);
        Array.Copy(msecsArray,msecsArray.Length - 4,guidArray,guidArray.Length - 4,4);
     
        return new Guid(guidArray);
    }
  • 相关阅读:
    自定义vue必填验证指令
    福珑2日常存活策略
    树莓派系统安装和配置 WiringPi
    代码实现RabbitMQ死信队列的创建
    博客已经搬家
    浅谈PHP序列化与反序列化
    prometheus监控golang服务实践
    ETCD核心机制解析
    N1BOOK 记录
    利用Ubuntu虚拟机制作F2FS文件系统镜像
  • 原文地址:https://www.cnblogs.com/shiningrise/p/5690016.html
Copyright © 2011-2022 走看看