|
| 1 | +# small_time library, courtesy @thatstoasty , 2025 |
| 2 | +# https://github.com/thatstoasty/small-time/ |
| 3 | +from sys import external_call |
| 4 | +from sys.ffi import c_uchar |
| 5 | +from memory import UnsafePointer, Pointer, stack_allocation |
| 6 | + |
| 7 | + |
| 8 | +@register_passable("trivial") |
| 9 | +struct TimeVal: |
| 10 | + """Time value.""" |
| 11 | + var tv_sec: Int |
| 12 | + """Seconds.""" |
| 13 | + var tv_usec: Int |
| 14 | + """Microseconds.""" |
| 15 | + |
| 16 | + fn __init__(out self, tv_sec: Int = 0, tv_usec: Int = 0): |
| 17 | + """Initializes a new time value. |
| 18 | + |
| 19 | + Args: |
| 20 | + tv_sec: Seconds. |
| 21 | + tv_usec: Microseconds. |
| 22 | + """ |
| 23 | + self.tv_sec = tv_sec |
| 24 | + self.tv_usec = tv_usec |
| 25 | + |
| 26 | + |
| 27 | +@register_passable("trivial") |
| 28 | +struct Tm: |
| 29 | + """C Tm struct.""" |
| 30 | + |
| 31 | + var tm_sec: Int32 |
| 32 | + """Seconds.""" |
| 33 | + var tm_min: Int32 |
| 34 | + """Minutes.""" |
| 35 | + var tm_hour: Int32 |
| 36 | + """Hour.""" |
| 37 | + var tm_mday: Int32 |
| 38 | + """Day of the month.""" |
| 39 | + var tm_mon: Int32 |
| 40 | + """Month.""" |
| 41 | + var tm_year: Int32 |
| 42 | + """Year minus 1900.""" |
| 43 | + var tm_wday: Int32 |
| 44 | + """Day of the week.""" |
| 45 | + var tm_yday: Int32 |
| 46 | + """Day of the year.""" |
| 47 | + var tm_isdst: Int32 |
| 48 | + """Daylight savings flag.""" |
| 49 | + var tm_gmtoff: Int64 |
| 50 | + """Localtime zone offset seconds.""" |
| 51 | + |
| 52 | + fn __init__(out self): |
| 53 | + """Initializes a new time struct.""" |
| 54 | + self.tm_sec = 0 |
| 55 | + self.tm_min = 0 |
| 56 | + self.tm_hour = 0 |
| 57 | + self.tm_mday = 0 |
| 58 | + self.tm_mon = 0 |
| 59 | + self.tm_year = 0 |
| 60 | + self.tm_wday = 0 |
| 61 | + self.tm_yday = 0 |
| 62 | + self.tm_isdst = 0 |
| 63 | + self.tm_gmtoff = 0 |
| 64 | + |
| 65 | + |
| 66 | +fn gettimeofday() -> TimeVal: |
| 67 | + """Gets the current time. It's a wrapper around libc `gettimeofday`. |
| 68 | + |
| 69 | + Returns: |
| 70 | + Current time. |
| 71 | + """ |
| 72 | + var tv = stack_allocation[1, TimeVal]() |
| 73 | + _ = external_call["gettimeofday", Int32](tv, 0) |
| 74 | + return tv.take_pointee() |
| 75 | + |
| 76 | + |
| 77 | +fn time() -> Int: |
| 78 | + """Returns the current time in seconds since the Epoch. |
| 79 | + |
| 80 | + Returns: |
| 81 | + Current time in seconds. |
| 82 | + """ |
| 83 | + var time = 0 |
| 84 | + return external_call["time", Int](Pointer.address_of(time)) |
| 85 | + |
| 86 | + |
| 87 | +fn localtime(owned tv_sec: Int) -> Tm: |
| 88 | + """Converts a time value to a broken-down local time. |
| 89 | +
|
| 90 | + Args: |
| 91 | + tv_sec: Time value in seconds since the Epoch. |
| 92 | + |
| 93 | + Returns: |
| 94 | + Broken down local time. |
| 95 | + """ |
| 96 | + return external_call["localtime", UnsafePointer[Tm]](UnsafePointer.address_of(tv_sec)).take_pointee() |
| 97 | + |
| 98 | + |
| 99 | +fn strptime(time_str: String, time_format: String) -> Tm: |
| 100 | + """Parses a time string according to a format string. |
| 101 | +
|
| 102 | + Args: |
| 103 | + time_str: Time string. |
| 104 | + time_format: Time format string. |
| 105 | + |
| 106 | + Returns: |
| 107 | + Broken down time. |
| 108 | + """ |
| 109 | + var tm = stack_allocation[1, Tm]() |
| 110 | + _ = external_call[ |
| 111 | + "strptime", |
| 112 | + NoneType, |
| 113 | + UnsafePointer[c_uchar], |
| 114 | + UnsafePointer[c_uchar], |
| 115 | + UnsafePointer[Tm] |
| 116 | + ](time_str.unsafe_ptr(), time_format.unsafe_ptr(), tm) |
| 117 | + return tm.take_pointee() |
| 118 | + |
| 119 | + |
| 120 | +fn gmtime(owned tv_sec: Int) -> Tm: |
| 121 | + """Converts a time value to a broken-down UTC time. |
| 122 | + |
| 123 | + Args: |
| 124 | + tv_sec: Time value in seconds since the Epoch. |
| 125 | + |
| 126 | + Returns: |
| 127 | + Broken down UTC time. |
| 128 | + """ |
| 129 | + return external_call["gmtime", UnsafePointer[Tm]](Pointer.address_of(tv_sec)).take_pointee() |
0 commit comments