Skip to content

Commit f9c21af

Browse files
Update hexTimestamp calculations
1 parent 203f514 commit f9c21af

File tree

8 files changed

+13
-13
lines changed

8 files changed

+13
-13
lines changed

templates/android/library/src/main/java/io/package/ID.kt.twig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ class ID {
99
// Generate an hex ID based on timestamp
1010
// Recreated from https://www.php.net/manual/en/function.uniqid.php
1111
private fun hexTimestamp(): String {
12-
val now = Instant.now().toEpochMilli() / 1000.0
13-
val sec = floor(now).toLong()
14-
val usec = ((now - sec) * 1_000_000).toLong()
12+
val now = Instant.now()
13+
val sec = now.epochSecond
14+
val usec = System.nanoTime() / 1000
1515

1616
// Convert both parts to hexadecimal format
1717
val hexTimestamp = "%08x%05x".format(sec, usec)

templates/dart/lib/id.dart.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class ID {
99
static String _hexTimestamp() {
1010
final now = DateTime.now();
1111
final sec = (now.millisecondsSinceEpoch / 1000).floor();
12-
final usec = now.microsecondsSinceEpoch - sec * 1000000;
12+
final usec = (now.microsecondsSinceEpoch - sec) * 1000000;
1313
return sec.toRadixString(16) +
1414
usec.toRadixString(16).padLeft(5, '0');
1515
}

templates/deno/src/id.ts.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ export class ID {
44
static #hexTimestamp(): string {
55
const now = new Date();
66
const sec = Math.floor(now.getTime() / 1000);
7-
const usec = now.getMilliseconds() * 1000;
7+
const msec = now.getMilliseconds();
88

99
// Convert to hexadecimal
10-
const hexTimestamp = sec.toString(16) + usec.toString(16).padStart(5, '0');
10+
const hexTimestamp = sec.toString(16) + msec.toString(16).padStart(5, '0');
1111
return hexTimestamp;
1212
}
1313

templates/dotnet/src/Appwrite/ID.cs.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace {{ spec.title | caseUcfirst }}
1111
var now = DateTime.UtcNow;
1212
var epoch = (now - new DateTime(1970, 1, 1));
1313
var sec = (long)epoch.TotalSeconds;
14-
var usec = (long)(epoch.TotalMilliseconds % 1000); // Corrected calculation for milliseconds
14+
var usec = (long)(epoch.TotalMilliseconds * 1000);
1515

1616
// Convert to hexadecimal
1717
var hexTimestamp = sec.ToString("x") + usec.ToString("x").PadLeft(5, '0');

templates/kotlin/src/main/kotlin/io/appwrite/ID.kt.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class ID {
1111
private fun hexTimestamp(): String {
1212
val now = Instant.now()
1313
val sec = now.epochSecond
14-
val millis = now.toEpochMilli() % 1000
14+
val usec = System.nanoTime() / 1000
1515

1616
val hexTimestamp = "%08x%05x".format(sec, millis)
1717

templates/node/lib/id.js.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ class ID {
44
static #hexTimestamp = () => {
55
const now = new Date();
66
const sec = Math.floor(now.getTime() / 1000);
7-
const usec = now.getMilliseconds() * 1000;
7+
const msec = now.getMilliseconds();
88

99
// Convert to hexadecimal
10-
const hexTimestamp = sec.toString(16) + usec.toString(16).padStart(5, '0');
10+
const hexTimestamp = sec.toString(16) + msec.toString(16).padStart(5, '0');
1111
return hexTimestamp;
1212
}
1313

templates/python/package/id.py.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class ID:
99
def __hex_timestamp():
1010
now = datetime.now()
1111
sec = int(now.timestamp())
12-
usec = now.microsecond
12+
usec = (now.microsecond % 1000)
1313
hex_timestamp = f'{sec:08x}{usec:05x}'
1414
return hex_timestamp
1515

templates/web/src/id.ts.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ export class ID {
44
static #hexTimestamp(): string {
55
const now = new Date();
66
const sec = Math.floor(now.getTime() / 1000);
7-
const usec = now.getMilliseconds() * 1000;
7+
const msec = now.getMilliseconds();
88

99
// Convert to hexadecimal
10-
const hexTimestamp = sec.toString(16) + usec.toString(16).padStart(5, '0');
10+
const hexTimestamp = sec.toString(16) + msec.toString(16).padStart(5, '0');
1111
return hexTimestamp;
1212
}
1313

0 commit comments

Comments
 (0)