Mekumiao.IdGen.Ntp 1.1.1

dotnet add package Mekumiao.IdGen.Ntp --version 1.1.1
                    
NuGet\Install-Package Mekumiao.IdGen.Ntp -Version 1.1.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Mekumiao.IdGen.Ntp" Version="1.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Mekumiao.IdGen.Ntp" Version="1.1.1" />
                    
Directory.Packages.props
<PackageReference Include="Mekumiao.IdGen.Ntp" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Mekumiao.IdGen.Ntp --version 1.1.1
                    
#r "nuget: Mekumiao.IdGen.Ntp, 1.1.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Mekumiao.IdGen.Ntp@1.1.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Mekumiao.IdGen.Ntp&version=1.1.1
                    
Install as a Cake Addin
#tool nuget:?package=Mekumiao.IdGen.Ntp&version=1.1.1
                    
Install as a Cake Tool

IdGen.Ntp

将 NTP(网络时间协议)时间获取与 IdGen 分布式 ID 生成库集成。

功能

  • NtpClient — 轻量级基于 UDP 的 NTP 客户端,从 NTP 服务器获取当前时间(默认:ntp1.aliyun.com)。发送标准 48 字节 NTP 请求包,将响应中的时间戳解析为 DateTime(UTC)。

  • NtpTimeSource — 实现 IdGen.ITimeSource 接口,为 IdGenerator 提供 NTP 同步时间。内部使用 Stopwatch 进行高精度时间测量,支持自定义纪元(epoch)和刻度间隔(默认 1 毫秒)。

使用示例

using IdGen.Ntp;

// 获取网络时间
var ntpClient = new NtpClient();
DateTime networkTime = ntpClient.GetNetworkTime();

// 与 IdGen 配合使用
var epoch = new DateTimeOffset(2026, 1, 1, 0, 0, 0, TimeSpan.Zero);
var timeSource = new NtpTimeSource(networkTime, epoch);
var structure = new IdStructure(45, 2, 16);
var options = new IdGeneratorOptions(structure, timeSource);
var generator = new IdGenerator(0, options);
long id = generator.CreateId();

API

NtpClient(string ntpServer = "ntp1.aliyun.com")

  • DateTime GetNetworkTime() — 通过 UDP 发送 NTP 请求,返回网络时间(UTC)。

NtpTimeSource(DateTimeOffset initialized, DateTimeOffset epoch)

NtpTimeSource(DateTimeOffset initialized, DateTimeOffset epoch, TimeSpan tickDuration)

  • DateTimeOffset Epoch { get; }
  • TimeSpan TickDuration { get; }
  • long GetTicks() — 返回自纪元以来经过的刻度数。

安装

NuGet

dotnet add package Mekumiao.IdGen.Ntp

环境要求

  • .NET 10.0+
  • C# 14.0+

开发

dotnet build
dotnet test

许可证

MIT

版权所有 (c) 2026 mekumiao

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.1 131 5/18/2026