|
| 1 | +# 📅 DateTime - OSL Package |
| 2 | + |
| 3 | +A comprehensive datetime utility library for OSL with formatting, manipulation, and relative time functionality. |
| 4 | + |
| 5 | +## 🚀 Installation |
| 6 | + |
| 7 | +```sh |
| 8 | +opal install datetime |
| 9 | +``` |
| 10 | + |
| 11 | +## 📖 Usage |
| 12 | + |
| 13 | +```osl |
| 14 | +import 'datetime/v1' from 'packages' as 'dt' |
| 15 | +
|
| 16 | +// Get current timestamp |
| 17 | +local now = dt.now() |
| 18 | +
|
| 19 | +// Format a timestamp |
| 20 | +local formatted = dt.format(now, "YYYY-MM-DD HH:mm:ss") |
| 21 | +log formatted // Output: 2025-07-06 14:30:25 |
| 22 | +
|
| 23 | +// Make timestamp relative |
| 24 | +local relative = dt.makeRelative(now) |
| 25 | +log relative // Output: "just now" or "2 minutes ago" |
| 26 | +
|
| 27 | +// Add ordinal suffix to numbers |
| 28 | +local suffix = dt.addSuffix(21) |
| 29 | +log suffix // Output: "21st" |
| 30 | +``` |
| 31 | + |
| 32 | +## 🔧 API Reference |
| 33 | + |
| 34 | +### Methods |
| 35 | + |
| 36 | +#### `now()` |
| 37 | + |
| 38 | +Returns the current timestamp. |
| 39 | + |
| 40 | +**Returns:** `number` - Current timestamp |
| 41 | + |
| 42 | +#### `format(timestamp, format)` |
| 43 | + |
| 44 | +Formats a timestamp according to the provided format string. |
| 45 | + |
| 46 | +**Parameters:** |
| 47 | + |
| 48 | +- `timestamp` (number) - The timestamp to format |
| 49 | +- `format` (string) - Format string with tokens |
| 50 | + |
| 51 | +**Format Tokens:** |
| 52 | + |
| 53 | +- `YYYY` - 4-digit year (e.g., 2025) |
| 54 | +- `YY` - 2-digit year (e.g., 25) |
| 55 | +- `MMMM` - Full month name (e.g., January) |
| 56 | +- `MMM` - Short month name (e.g., Jan) |
| 57 | +- `MM` - 2-digit month (e.g., 01) |
| 58 | +- `M` - Month number (e.g., 1) |
| 59 | +- `DD` - 2-digit day (e.g., 06) |
| 60 | +- `Do` - Day with ordinal suffix (e.g., 6th) |
| 61 | +- `D` - Day number (e.g., 6) |
| 62 | +- `HH` - 2-digit hour (e.g., 14) |
| 63 | +- `H` - Hour number (e.g., 14) |
| 64 | +- `mm` - 2-digit minute (e.g., 30) |
| 65 | +- `m` - Minute number (e.g., 30) |
| 66 | +- `ss` - 2-digit second (e.g., 25) |
| 67 | +- `s` - Second number (e.g., 25) |
| 68 | +- `SSS` - 3-digit milliseconds (e.g., 123) |
| 69 | +- `SS` - 2-digit milliseconds (e.g., 12) |
| 70 | +- `S` - 1-digit milliseconds (e.g., 1) |
| 71 | +- `X` - Unix timestamp in seconds |
| 72 | +- `x` - Unix timestamp in milliseconds |
| 73 | + |
| 74 | +**Returns:** `string` - Formatted date string |
| 75 | + |
| 76 | +#### `makeRelative(timestamp)` |
| 77 | + |
| 78 | +Converts a timestamp to a relative time string. |
| 79 | + |
| 80 | +**Parameters:** |
| 81 | + |
| 82 | +- `timestamp` (number) - The timestamp to convert |
| 83 | + |
| 84 | +**Returns:** `string` - Relative time string (e.g., "2 minutes ago") |
| 85 | + |
| 86 | +#### `addSuffix(num)` |
| 87 | + |
| 88 | +Adds ordinal suffix to a number (1st, 2nd, 3rd, 4th, etc.). |
| 89 | + |
| 90 | +**Parameters:** |
| 91 | + |
| 92 | +- `num` (number|string) - The number to add suffix to |
| 93 | + |
| 94 | +**Returns:** `string` - Number with ordinal suffix |
| 95 | + |
| 96 | +### Properties |
| 97 | + |
| 98 | +#### `months` |
| 99 | + |
| 100 | +Array of month names: |
| 101 | + |
| 102 | +```osl |
| 103 | +["January", "February", "March", "April", "May", "June", |
| 104 | + "July", "August", "September", "October", "November", "December"] |
| 105 | +``` |
| 106 | + |
| 107 | +#### `mul` |
| 108 | + |
| 109 | +Time multiplication constants: |
| 110 | + |
| 111 | +```osl |
| 112 | +{ |
| 113 | + second: 1, |
| 114 | + minute: 60, |
| 115 | + hour: 3600, |
| 116 | + day: 86400, |
| 117 | + year: 31557600 |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +## 📝 Examples |
| 122 | + |
| 123 | +### Basic Formatting |
| 124 | + |
| 125 | +```osl |
| 126 | +import 'datetime/v1' from 'packages' as 'dt' |
| 127 | +
|
| 128 | +local now = dt.now() |
| 129 | +
|
| 130 | +// Different format examples |
| 131 | +log dt.format(now, "YYYY-MM-DD") // 2025-07-06 |
| 132 | +log dt.format(now, "MMMM Do, YYYY") // July 6th, 2025 |
| 133 | +log dt.format(now, "MMM DD, YY") // Jul 06, 25 |
| 134 | +log dt.format(now, "HH:mm:ss") // 14:30:25 |
| 135 | +log dt.format(now, "h:mm A") // 2:30 PM |
| 136 | +``` |
| 137 | + |
| 138 | +### Working with Ordinals |
| 139 | + |
| 140 | +```osl |
| 141 | +import 'datetime/v1' from 'packages' as 'dt' |
| 142 | +
|
| 143 | +log dt.addSuffix(1) // "1st" |
| 144 | +log dt.addSuffix(2) // "2nd" |
| 145 | +log dt.addSuffix(3) // "3rd" |
| 146 | +log dt.addSuffix(4) // "4th" |
| 147 | +log dt.addSuffix(11) // "11th" |
| 148 | +log dt.addSuffix(21) // "21st" |
| 149 | +log dt.addSuffix(22) // "22nd" |
| 150 | +``` |
| 151 | + |
| 152 | +### Time Calculations |
| 153 | + |
| 154 | +```osl |
| 155 | +import 'datetime/v1' from 'packages' as 'dt' |
| 156 | +
|
| 157 | +local now = dt.now() |
| 158 | +local oneHourAgo = now - dt.mul.hour |
| 159 | +local oneDayFromNow = now + dt.mul.day |
| 160 | +
|
| 161 | +log dt.format(oneHourAgo, "HH:mm") // One hour ago |
| 162 | +log dt.format(oneDayFromNow, "YYYY-MM-DD") // Tomorrow's date |
| 163 | +``` |
0 commit comments