posseth.global.ulid

2025-12-10 0 385

Posseth.UlidFactory

Overview

This library is a C# library that provides a Ulid type in .Net
(C#,VB.Net tested by DEV)

What is a ULID?

A ULID (Universally Unique Lexicographically Sortable Identifier) is a 128-bit identifier that is designed to be unique and time-ordered.
It is similar to a UUID (Universally Unique Identifier) but has the additional property of being sortable by time.
A ULID consists of two components: a 48-bit timestamp and a 80-bit random value.
The timestamp is encoded in Crockford\’s Base32 format, which allows the ULID to be lexicographically sortable.
The random value provides uniqueness and helps prevent collisions between ULIDs generated at the same time.

Features

Posseth.UlidFactory library provides the following features:
ULID generation Parsing and DateTime , Epoch , UnixTime conversion from the ulid.
it can handle new Ulids but also existing Ulids read from Database and optionally convert them to DateTime , Epoch , UnixTime

Installation

To install Posseth.UlidFactory, add the following reference to your project:
using Posseth.UlidFactory;

Usage

To use Posseth.UlidFactory in a C# project, add the following using statement to your C# file:

Generating a New ULID
You can generate a new ULID using the NewUlid method. This can be done with the current timestamp or with a specified timestamp.

using System;
using Posseth.UlidFactory;

class Program
{
    static void Main()
    {
        // Generate a new ULID with the current timestamp
        Ulid newUlid = Ulid.NewUlid();
        Console.WriteLine(\"New ULID: \" + newUlid);

        // Generate a new ULID with a specified DateTime
        DateTime specifiedTime = new DateTime(2023, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        Ulid ulidWithTime = Ulid.NewUlid(specifiedTime);
        Console.WriteLine(\"ULID with specified time: \" + ulidWithTime);

        // Generate a new ULID with a specified Unix timestamp
        long unixTimestamp = 1672444800000; // Unix timestamp for 2023-01-01 00:00:00 UTC
        Ulid ulidWithUnixTime = Ulid.NewUlid(unixTimestamp);
        Console.WriteLine(\"ULID with specified Unix timestamp: \" + ulidWithUnixTime);
    }
}

Parsing an Existing ULID String
You can parse an existing ULID string using the Parse method. If the ULID string is invalid, an exception will be thrown.
Alternatively, you can us the TryParse method to avoid exceptions and handle invalid ULIDs more gracefully.

using System;
using Posseth.UlidFactory;

class Program
{
    static void Main()
    {
        // Parse a valid ULID string
        string ulidString = \"01BX5ZZKBKACTAV9WEVGEMMVRZ\";
        try
        {
            Ulid parsedUlid = Ulid.Parse(ulidString);
            Console.WriteLine(\"Parsed ULID: \" + parsedUlid);
        }
        catch (ArgumentException ex)
        {
            Console.WriteLine(\"Invalid ULID string: \" + ex.Message);
        }

        // Try to parse a ULID string and handle invalid ULID gracefully
        Ulid? ulid;
        bool success = Ulid.TryParse(ulidString, out ulid);
        if (success)
        {
            Console.WriteLine(\"Successfully parsed ULID: \" + ulid);
        }
        else
        {
            Console.WriteLine(\"Failed to parse ULID.\");
        }
    }
}

Extracting the Timestamp from a ULID
You can extract the timestamp from a ULID and convert it to a DateTime or Unix timestamp using the ToDateTime, ToEpoch, or ToUnixTime methods.

using System;
using Posseth.UlidFactory;

class Program
{
    static void Main()
    {
        // Generate a new ULID
        Ulid newUlid = Ulid.NewUlid();
        Console.WriteLine(\"New ULID: \" + newUlid);

        // Get the timestamp as a DateTime
        DateTime timestamp = newUlid.ToDateTime();
        Console.WriteLine(\"Timestamp as DateTime: \" + timestamp);

        // Get the Unix timestamp in milliseconds
        long unixTimestamp = newUlid.ToUnixTime();
        Console.WriteLine(\"Timestamp as Unix time: \" + unixTimestamp);

        // Extract timestamp using static method
        DateTime extractedTimestamp = Ulid.GetTimestampFromUlid(newUlid);
        Console.WriteLine(\"Extracted timestamp: \" + extractedTimestamp);
    }
}

In summary, the Ulid class provides methods for generating, parsing, and extracting timestamps from ULIDs.
By following the examples above, you can incorporate ULID generation and handling in your C# applications.
The class ensures that the ULIDs generated are unique and time-ordered, providing a robust solution for use cases requiring such identifiers.

Posseth.Global.UlidFactory.MSSQL

This library is a C# library that provides a Ulid type in .Net MSSQL CLR

Installation

Build the project and add the Posseth.Global.UlidFactory.MSSQL.dll assembly to your MSSQL database

First, you need to enable CLR integration on your SQL Server instance by running the following command:

sp_configure \'clr enabled\', 1;
RECONFIGURE;

First you need to create a file hash of the assembly

certutil -hashfile \"C:\\path\\to\\your\\Posseth.Global.UlidFactory.MSSQL.dll\" SHA512

the result should be something like this
\”b424f44500e83e72c8b4b60528250a40a4eabfddbb6b9c0e93208e6ddc63815e80180f26814f85bdad5ba2c88209d732397c7599e809da6df5146cc94daa250e\”

Before you add the assembly to the database you need to tell MSSQL that you trust the assembly by running the following command

-- Step 1: Declare a variabel and convert the hash to a binary value
DECLARE @binaryHash varbinary(64);
SET @binaryHash = CONVERT(varbinary(64), 0xB424F44500E83E72C8B4B60528250A40A4EABFDDBB6B9C0E93208E6DDC63815E80180F26814F85BDAD5BA2C88209D732397C7599E809DA6DF5146CC94DAA250E, 1);

-- Step 2:  use the variabel as parameter in the stored procedure
EXEC sp_add_trusted_assembly 
    @hash = @binaryHash;

Now you can add the assembly to the database

CREATE ASSEMBLY UlidFactoryMSSQL
FROM \'C:\\path\\to\\your\\Posseth.Global.UlidFactory.MSSQL.dll\'
WITH PERMISSION_SET = SAFE;

The execute should give you \’Commands completed successfully.\’
now you can create the Ulid type in the database

CREATE FUNCTION dbo.GenerateUlid()
RETURNS NVARCHAR(100)
AS EXTERNAL NAME [UlidFactoryMSSQL].[Posseth.Global.UlidFactory.MSSQL.CLR.UlidFunctionsHelpers].[GenerateUlid];
GO

CREATE FUNCTION dbo.GenerateUlidWithTimestamp(@timestamp DATETIME)
RETURNS NVARCHAR(100)
AS EXTERNAL NAME [UlidFactoryMSSQL].[Posseth.Global.UlidFactory.MSSQL.CLR.UlidFunctionsHelpers].[GenerateUlidWithTimestamp];
GO

CREATE FUNCTION dbo.ExtractDateFromUlid(@ulidString NVARCHAR(100))
RETURNS DATETIME
AS EXTERNAL NAME [UlidFactoryMSSQL].[Posseth.Global.UlidFactory.MSSQL.CLR.UlidFunctionsHelpers].[ExtractDateFromUlid];
GO

The execute should give you \’Commands completed successfully.\’
Now you can use the functions in your database

SELECT dbo.GenerateUlid();
SELECT dbo.GenerateUlidWithTimestamp(GETDATE());
SELECT dbo.ExtractDateFromUlid(\'01F8MECHZX3TBDSZ7FBJ4H7FJ6\');

The execute should give you a new Ulid, a new Ulid with a timestamp and the timestamp extracted from the Ulid (1982-11-13 14:32:34.560)
for your convenience I added the compiled DLL to the repository so you can use the above commands to add the assembly to your database

下载源码

通过命令行克隆项目:

git clone https://github.com/MPCoreDeveloper/posseth.global.ulid.git

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

申明:本文由第三方发布,内容仅代表作者观点,与本网站无关。对本文以及其中全部或者部分内容的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。本网发布或转载文章出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,也不代表本网对其真实性负责。

左子网 编程相关 posseth.global.ulid https://www.zuozi.net/33181.html

lsfiles
下一篇: lsfiles
常见问题
  • 1、自动:拍下后,点击(下载)链接即可下载;2、手动:拍下后,联系卖家发放即可或者联系官方找开发者发货。
查看详情
  • 1、源码默认交易周期:手动发货商品为1-3天,并且用户付款金额将会进入平台担保直到交易完成或者3-7天即可发放,如遇纠纷无限期延长收款金额直至纠纷解决或者退款!;
查看详情
  • 1、描述:源码描述(含标题)与实际源码不一致的(例:货不对板); 2、演示:有演示站时,与实际源码小于95%一致的(但描述中有”不保证完全一样、有变化的可能性”类似显著声明的除外); 3、发货:不发货可无理由退款; 4、安装:免费提供安装服务的源码但卖家不履行的; 5、收费:价格虚标,额外收取其他费用的(但描述中有显著声明或双方交易前有商定的除外); 6、其他:如质量方面的硬性常规问题BUG等。 注:经核实符合上述任一,均支持退款,但卖家予以积极解决问题则除外。
查看详情
  • 1、左子会对双方交易的过程及交易商品的快照进行永久存档,以确保交易的真实、有效、安全! 2、左子无法对如“永久包更新”、“永久技术支持”等类似交易之后的商家承诺做担保,请买家自行鉴别; 3、在源码同时有网站演示与图片演示,且站演与图演不一致时,默认按图演作为纠纷评判依据(特别声明或有商定除外); 4、在没有”无任何正当退款依据”的前提下,商品写有”一旦售出,概不支持退款”等类似的声明,视为无效声明; 5、在未拍下前,双方在QQ上所商定的交易内容,亦可成为纠纷评判依据(商定与描述冲突时,商定为准); 6、因聊天记录可作为纠纷评判依据,故双方联系时,只与对方在左子上所留的QQ、手机号沟通,以防对方不承认自我承诺。 7、虽然交易产生纠纷的几率很小,但一定要保留如聊天记录、手机短信等这样的重要信息,以防产生纠纷时便于左子介入快速处理。
查看详情

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务