@@ -120,7 +120,7 @@ module std.uuid;
120
120
assert (id.empty);
121
121
}
122
122
123
- import core.time : dur;
123
+ import core.time : dur, MonoTimeImpl ;
124
124
import std.datetime.systime : SysTime;
125
125
import std.datetime : Clock , DateTime , UTC ;
126
126
import std.range.primitives ;
@@ -323,21 +323,37 @@ public struct UUID
323
323
* random = UUID V7 has 74 bits of random data, which rounds to 10 ubyte's.
324
324
* If no random data is given, random data is generated.
325
325
*/
326
+ @safe pure this (TMonoTime)(TMonoTime timestamp, ubyte [10 ] random = generateV7RandomData())
327
+ if (isInstanceOf! (MonoTimeImpl, TMonoTime))
328
+ {
329
+ import std.datetime ;
330
+
331
+ TMonoTime epochStart = TMonoTime.zero + unixTimeToStdTime(0 ).seconds;
332
+ ulong epoch = (timestamp - epochStart).total! " msecs" ;
333
+ this (epoch, random);
334
+ }
335
+
336
+ // / ditto
326
337
@safe pure this (SysTime timestamp, ubyte [10 ] random = generateV7RandomData())
327
338
{
328
339
ulong epoch = (timestamp - SysTime.fromUnixTime(0 )).total! " msecs" ;
329
- this .data[6 .. $] = random[];
340
+ this (epoch, random);
341
+ }
330
342
331
- this .data[0 ] = cast (ubyte )((epoch >> 40 ) & 0xFF );
332
- this .data[1 ] = cast (ubyte )((epoch >> 32 ) & 0xFF );
333
- this .data[2 ] = cast (ubyte )((epoch >> 24 ) & 0xFF );
334
- this .data[3 ] = cast (ubyte )((epoch >> 16 ) & 0xFF );
335
- this .data[4 ] = cast (ubyte )((epoch >> 8 ) & 0xFF );
336
- this .data[5 ] = cast (ubyte )(epoch & 0xFF );
343
+ // /
344
+ @system unittest
345
+ {
346
+ import core.time : Duration;
347
+ import std.datetime ;
337
348
338
- // version and variant
339
- this .data[6 ] = (this .data[6 ] & 0x0F ) | 0x70 ;
340
- this .data[8 ] = (this .data[8 ] & 0x3F ) | 0x80 ;
349
+ MonoTime mt = MonoTime.currTime;
350
+ MonoTime mt_epoch_start = MonoTime.zero + unixTimeToStdTime(0 ).seconds;
351
+ Duration mt_dur = mt - mt_epoch_start;
352
+
353
+ UUID u = UUID (mt);
354
+ Duration u_dur = u.v7Milliseconds().msecs;
355
+
356
+ assert (u_dur == mt_dur, mt.toString() ~ " | " ~ u.toString());
341
357
}
342
358
343
359
// /
@@ -350,6 +366,22 @@ public struct UUID
350
366
assert (o == st, st.toString() ~ " | " ~ o.toString());
351
367
}
352
368
369
+ private @safe pure this (long epoch, ubyte [10 ] random)
370
+ {
371
+ this .data[6 .. $] = random[];
372
+
373
+ this .data[0 ] = cast (ubyte )((epoch >> 40 ) & 0xFF );
374
+ this .data[1 ] = cast (ubyte )((epoch >> 32 ) & 0xFF );
375
+ this .data[2 ] = cast (ubyte )((epoch >> 24 ) & 0xFF );
376
+ this .data[3 ] = cast (ubyte )((epoch >> 16 ) & 0xFF );
377
+ this .data[4 ] = cast (ubyte )((epoch >> 8 ) & 0xFF );
378
+ this .data[5 ] = cast (ubyte )(epoch & 0xFF );
379
+
380
+ // version and variant
381
+ this .data[6 ] = (this .data[6 ] & 0x0F ) | 0x70 ;
382
+ this .data[8 ] = (this .data[8 ] & 0x3F ) | 0x80 ;
383
+ }
384
+
353
385
/**
354
386
* <a name =" UUID(string)" ></a>
355
387
* Parse a UUID from its canonical string form. An UUID in its
@@ -561,18 +593,23 @@ public struct UUID
561
593
* returns, otherwise and UUIDParsingException is thrown.
562
594
*/
563
595
SysTime v7Timestamp () const {
596
+ return SysTime (DateTime (1970 , 1 , 1 ), UTC ()) + dur! " msecs" (v7Milliseconds());
597
+ }
598
+
599
+ // / ditto
600
+ ulong v7Milliseconds () const {
564
601
if (this .uuidVersion != Version.timestampRandom)
565
602
{
566
603
throw new UUIDParsingException(" The UUID is not of version" ~
567
604
" v7 therefore no timestamp exist" , 0 );
568
605
}
569
- ulong milli = (cast (ulong )(this .data[0 ]) << 40 ) |
606
+
607
+ return (cast (ulong )(this .data[0 ]) << 40 ) |
570
608
(cast (ulong )(this .data[1 ]) << 32 ) |
571
609
(cast (ulong )(this .data[2 ]) << 24 ) |
572
610
(cast (ulong )(this .data[3 ]) << 16 ) |
573
611
(cast (ulong )(this .data[4 ]) << 8 ) |
574
612
(cast (ulong )(this .data[5 ]));
575
- return SysTime (DateTime (1970 , 1 , 1 ), UTC ()) + dur! " msecs" (milli);
576
613
}
577
614
578
615
/**
@@ -1384,7 +1421,8 @@ if (isInputRange!RNG && isIntegral!(ElementType!RNG))
1384
1421
*/
1385
1422
UUID timestampRandomUUID ()
1386
1423
{
1387
- return UUID (Clock .currTime());
1424
+ import core.time : MonoTime;
1425
+ return UUID (MonoTime.currTime);
1388
1426
}
1389
1427
1390
1428
// /
0 commit comments