Fix snowflake id generation

This commit is contained in:
Laura Hausmann 2024-01-13 20:36:37 +01:00
parent 4271210ec2
commit 9b7029dbf3
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 5 additions and 5 deletions

View file

@ -7,8 +7,8 @@ public static class IdHelpers {
private const long Time2000 = 946684800000; private const long Time2000 = 946684800000;
public static string GenerateSlowflakeId() { public static string GenerateSlowflakeId() {
var cuid = new Cuid2(16); var cuid = new Cuid2(8);
var now = (long)DateTime.UtcNow.Subtract(DateTime.UnixEpoch).TotalSeconds; var now = (long)DateTime.UtcNow.Subtract(DateTime.UnixEpoch).TotalMilliseconds;
var time = Math.Max(now - Time2000, 0); var time = Math.Max(now - Time2000, 0);
var timestamp = time.ToBase36().PadLeft(8, '0'); var timestamp = time.ToBase36().PadLeft(8, '0');
return timestamp + cuid; return timestamp + cuid;

View file

@ -3,14 +3,14 @@ using System.Text;
namespace Iceshrimp.Backend.Core.Helpers; namespace Iceshrimp.Backend.Core.Helpers;
public static class NumberExtensions { public static class NumberExtensions {
private const string Base36Charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; private const string Base36Charset = "0123456789abcdefghijklmnopqrstuvwxyz";
public static string ToBase36(this long input) { public static string ToBase36(this long input) {
if (input == 0) return "0"; if (input == 0) return "0";
var result = new StringBuilder(); var result = new StringBuilder();
while (input > 0) { while (input > 0) {
result.Append(Base36Charset[(int)(input % 36)]); result.Insert(0, Base36Charset[(int)(input % 36)]);
input /= 36; input /= 36;
} }
@ -22,7 +22,7 @@ public static class NumberExtensions {
var result = new StringBuilder(); var result = new StringBuilder();
while (input >= 0) { while (input >= 0) {
result.Append(Base36Charset[input % 36]); result.Insert(0, Base36Charset[input % 36]);
input /= 36; input /= 36;
} }