Skip to content

Commit 11ac457

Browse files
Fix things
1 parent a499c3b commit 11ac457

File tree

5 files changed

+28
-25
lines changed

5 files changed

+28
-25
lines changed

templates/android/library/src/main/java/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().toEpochMilli() / 1000.0
1313
val secondsSinceEpoch = floor(now).toLong()
14-
val msecs = ((now - secondsPart) * 1_000_000).toLong()
14+
val msecs = ((now - secondsSinceEpoch) * 1_000_000).toLong()
1515

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

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ import kotlin.math.floor
55
import kotlin.random.Random
66

77
class ID {
8-
// Generate an hex ID based on timestamp
9-
// Recreated from https://www.php.net/manual/en/function.uniqid.php
10-
private fun hexTimestamp(): String {
11-
val now = Instant.now().toEpochMilli() / 1000.0
12-
val secondsSinceEpoch = floor(now).toLong()
13-
val millisecondsSinceEpoch = ((now - secondsSinceEpoch) * 1_000_000).toLong()
8+
companion object {
9+
// Generate an hex ID based on timestamp
10+
// Recreated from https://www.php.net/manual/en/function.uniqid.php
11+
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()
1415

15-
// Convert both parts to hexadecimal format
16-
val hexTimestamp = "%08x%05x".format(secondsSinceEpoch, millisecondsSinceEpoch)
17-
return hexTimestamp
18-
}
16+
// Convert both parts to hexadecimal format
17+
val hexTimestamp = "%08x%05x".format(secondsSinceEpoch, msecs)
18+
return hexTimestamp
19+
}
1920

20-
companion object {
2121
fun custom(id: String): String
2222
= id
2323
// Generate a unique ID with padding to have a longer ID

templates/python/package/id.py.twig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import datetime
2+
import math
23
import os
34

45
class ID:
@@ -20,6 +21,6 @@ class ID:
2021
@staticmethod
2122
def unique(padding = 7):
2223
base_id = ID.__hex_timestamp()
23-
random_bytes = os.urandom(ceil(padding / 2))
24+
random_bytes = os.urandom(math.ceil(padding / 2))
2425
random_padding = random_bytes.hex()[:padding]
2526
return base_id + random_padding

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,6 @@ require 'securerandom'
22

33
module {{spec.title | caseUcfirst}}
44
class ID
5-
#Generate an hex ID based on timestamp
6-
#Recreated from https://www.php.net/manual/en/function.uniqid.php
7-
private def self.hex_timestamp
8-
now = Time.now
9-
seconds_since_epoch = now.to_i
10-
microseconds_part = now.usec
11-
hex_timestamp = "%08x%05x" % [seconds_since_epoch, microseconds_part]
12-
hex_timestamp
13-
end
14-
155
def self.custom(id)
166
id
177
end
@@ -23,5 +13,17 @@ module {{spec.title | caseUcfirst}}
2313
random_padding = random_padding[0...padding]
2414
base_id + random_padding
2515
end
16+
17+
private
18+
19+
#Generate an hex ID based on timestamp
20+
#Recreated from https://www.php.net/manual/en/function.uniqid.php
21+
def self.hex_timestamp
22+
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]
26+
hex_timestamp
27+
end
2628
end
2729
end

templates/swift/Sources/ID.swift.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Foundation
33
public class ID {
44
// Generate an hex ID based on timestamp
55
// Recreated from https://www.php.net/manual/en/function.uniqid.php
6-
private func hexTimestamp() -> String {
6+
private static func hexTimestamp() -> String {
77
let now = Date()
88
let secondsSinceEpoch = Int(now.timeIntervalSince1970)
99
let microsecondsPart = Int((now.timeIntervalSince1970 - Double(secondsSinceEpoch)) * 1_000_000)
@@ -17,7 +17,7 @@ public class ID {
1717

1818
// Generate a unique ID with padding to have a longer ID
1919
public static func unique(padding: Int = 7) -> String {
20-
let baseId = ID.hexTimestamp()
20+
let baseId = Self.hexTimestamp()
2121
let randomPadding = (1...padding).map {
2222
_ in String(format: "%x", Int.random(in: 0..<16))
2323
}.joined()

0 commit comments

Comments
 (0)