Skip to content

Commit b7cb064

Browse files
Update variables and msec calculations
1 parent 11ac457 commit b7cb064

File tree

10 files changed

+37
-40
lines changed

10 files changed

+37
-40
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ class ID {
1010
// Recreated from https://www.php.net/manual/en/function.uniqid.php
1111
private fun hexTimestamp(): String {
1212
val now = Instant.now().toEpochMilli() / 1000.0
13-
val secondsSinceEpoch = floor(now).toLong()
14-
val msecs = ((now - secondsSinceEpoch) * 1_000_000).toLong()
13+
val sec = floor(now).toLong()
14+
val usec = ((now - sec) * 1_000_000).toLong()
1515

1616
// Convert both parts to hexadecimal format
17-
val hexTimestamp = "%08x%05x".format(secondsSinceEpoch, msecs)
17+
val hexTimestamp = "%08x%05x".format(sec, usec)
1818
return hexTimestamp
1919
}
2020

templates/dart/lib/id.dart.twig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ class ID {
88
// Recreated from https://www.php.net/manual/en/function.uniqid.php
99
static String _hexTimestamp() {
1010
final now = DateTime.now();
11-
final secondsSinceEpoch = (now.millisecondsSinceEpoch / 1000).floor();
12-
final msecs = now.microsecondsSinceEpoch - secondsSinceEpoch * 1000000;
13-
return secondsSinceEpoch.toRadixString(16) +
14-
msecs.toRadixString(16).padLeft(5, '0');
11+
final sec = (now.millisecondsSinceEpoch / 1000).floor();
12+
final usec = now.microsecondsSinceEpoch - secondsSinceEpoch * 1000000;
13+
return sec.toRadixString(16) +
14+
usec.toRadixString(16).padLeft(5, '0');
1515
}
1616

1717
// Generate a unique ID with padding to have a longer ID

templates/deno/src/id.ts.twig

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ export class ID {
33
// Recreated from https://www.php.net/manual/en/function.uniqid.php
44
static #hexTimestamp(): string {
55
const now = new Date();
6-
const secondsSinceEpoch = Math.floor(now.getTime() / 1000);
7-
const msecs = now.getTime() - secondsSinceEpoch * 1000;
8-
const microsecondsPart = msecs * 1000; // Convert milliseconds to microseconds
6+
const sec = Math.floor(now.getTime() / 1000);
7+
const usec = now.getMilliseconds() * 1000;
98

109
// Convert to hexadecimal
11-
const hexTimestamp = secondsSinceEpoch.toString(16) + microsecondsPart.toString(16).padStart(5, '0');
10+
const hexTimestamp = sec.toString(16) + usec.toString(16).padStart(5, '0');
1211
return hexTimestamp;
1312
}
1413

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ namespace {{ spec.title | caseUcfirst }}
1010
{
1111
var now = DateTime.UtcNow;
1212
var epoch = (now - new DateTime(1970, 1, 1));
13-
var secondsSinceEpoch = (long)epoch.TotalSeconds;
14-
var msecs = epoch.TotalMilliseconds - secondsSinceEpoch * 1000;
15-
var microsecondsPart = (long)(msecs * 1000); // Convert milliseconds to microseconds
16-
13+
var sec = (long)epoch.TotalSeconds;
14+
var usec = epoch.TotalMilliseconds - secondsSinceEpoch * 1000000;
15+
1716
// Convert to hexadecimal
18-
var hexTimestamp = secondsSinceEpoch.ToString("x")
19-
+ microsecondsPart.ToString("x").PadLeft(5, '0');
17+
var hexTimestamp = sec.ToString("x")
18+
+ usec.ToString("x").PadLeft(5, '0');
2019
return hexTimestamp;
2120
}
2221

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ 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 secondsSinceEpoch = floor(now).toLong()
14-
val msecs = ((now - secondsSinceEpoch) * 1_000_000).toLong()
12+
val now = Instant.now()
13+
val sec = now.epochSecond
14+
val millis = now.toEpochMilli() % 1000
15+
16+
val hexTimestamp = "%08x%03x".format(sec, millis)
1517

16-
// Convert both parts to hexadecimal format
17-
val hexTimestamp = "%08x%05x".format(secondsSinceEpoch, msecs)
1818
return hexTimestamp
1919
}
2020

templates/node/lib/id.js.twig

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ class ID {
33
// Recreated from https://www.php.net/manual/en/function.uniqid.php
44
static #hexTimestamp = () => {
55
const now = new Date();
6-
const secondsSinceEpoch = Math.floor(now.getTime() / 1000);
7-
const msecs = now.getTime() - secondsSinceEpoch * 1000;
8-
const microsecondsPart = msecs * 1000; // Convert milliseconds to microseconds
6+
const sec = Math.floor(now.getTime() / 1000);
7+
const usec = now.getMilliseconds() * 1000;
98

109
// Convert to hexadecimal
11-
const hexTimestamp = secondsSinceEpoch.toString(16)
12-
+ microsecondsPart.toString(16).padStart(5, '0');
10+
const hexTimestamp = sec.toString(16) + usec.toString(16).padStart(5, '0');
1311
return hexTimestamp;
1412
}
1513

templates/python/package/id.py.twig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ class ID:
88
@staticmethod
99
def __hex_timestamp():
1010
now = datetime.now()
11-
seconds_since_epoch = int(now.timestamp())
12-
microseconds_part = now.microsecond
13-
hex_timestamp = f'{seconds_since_epoch:08x}{microseconds_part:05x}'
11+
sec = int(now.timestamp())
12+
usec = now.microsecond
13+
hex_timestamp = f'{sec:08x}{usec:05x}'
1414
return hex_timestamp
1515

1616
@staticmethod

templates/ruby/lib/container/id.rb.twig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ module {{spec.title | caseUcfirst}}
2020
#Recreated from https://www.php.net/manual/en/function.uniqid.php
2121
def self.hex_timestamp
2222
now = Time.now
23-
seconds_since_epoch = now.to_i
24-
microseconds_part = now.usec
25-
hex_timestamp = "%08x%05x" % [seconds_since_epoch, microseconds_part]
23+
sec = now.to_i
24+
usec = now.usec
25+
hex_timestamp = "%08x%05x" % [sec, usec]
2626
hex_timestamp
2727
end
2828
end

templates/swift/Sources/ID.swift.twig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ public class ID {
55
// Recreated from https://www.php.net/manual/en/function.uniqid.php
66
private static func hexTimestamp() -> String {
77
let now = Date()
8-
let secondsSinceEpoch = Int(now.timeIntervalSince1970)
9-
let microsecondsPart = Int((now.timeIntervalSince1970 - Double(secondsSinceEpoch)) * 1_000_000)
10-
let hexTimestamp = String(format: "%08x%05x", secondsSinceEpoch, microsecondsPart)
8+
let secs = Int(now.timeIntervalSince1970)
9+
let usec = Int((now.timeIntervalSince1970 - Double(secs)) * 1_000_000)
10+
let hexTimestamp = String(format: "%08x%05x", secs, usec)
1111
return hexTimestamp
1212
}
1313

templates/web/src/id.ts.twig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ export class ID {
33
// Recreated from https://www.php.net/manual/en/function.uniqid.php
44
static #hexTimestamp(): string {
55
const now = new Date();
6-
const secondsSinceEpoch = Math.floor(now.getTime() / 1000); // Get Unix Timestamp
7-
const microsecondsPart = now.getMilliseconds() * 1000; // Convert milliseconds to microseconds
8-
const hexTimestamp = secondsSinceEpoch.toString(16)
9-
+ microsecondsPart.toString(16).padStart(5, '0');
6+
const sec = Math.floor(now.getTime() / 1000);
7+
const usec = now.getMilliseconds() * 1000;
8+
9+
// Convert to hexadecimal
10+
const hexTimestamp = sec.toString(16) + usec.toString(16).padStart(5, '0');
1011
return hexTimestamp;
1112
}
1213

0 commit comments

Comments
 (0)