|
2 | 2 | //// |
3 | 3 | //// For more control (e.g. work with `time.Time` or `bytes.Bytes` directly, use the given unit instead of the most optimal one), look at the `time`, `bytes` or `bytes1024` modules. |
4 | 4 |
|
| 5 | +import gleam/float |
5 | 6 | import gleam/int |
| 7 | +import gleam/time/calendar.{type Date, type TimeOfDay, Date, TimeOfDay} |
| 8 | +import gleam/time/duration.{type Duration} |
| 9 | +import gleam/time/timestamp.{type Timestamp} |
6 | 10 |
|
7 | 11 | import humanise/bytes |
8 | 12 | import humanise/bytes1024 |
9 | 13 | import humanise/time |
10 | 14 |
|
| 15 | +pub fn date_relative(from date: Timestamp, now current: Timestamp) -> String { |
| 16 | + let relative = current |> timestamp.difference(date) |> time.from_duration |
| 17 | + |
| 18 | + let decompose = fn(a) { |
| 19 | + case a { |
| 20 | + time.Nanoseconds(n) -> #(time.Nanoseconds, n) |
| 21 | + time.Days(n) -> #(time.Days, n) |
| 22 | + time.Hours(n) -> #(time.Hours, n) |
| 23 | + time.Microseconds(n) -> #(time.Microseconds, n) |
| 24 | + time.Milliseconds(n) -> #(time.Milliseconds, n) |
| 25 | + time.Minutes(n) -> #(time.Minutes, n) |
| 26 | + time.Seconds(n) -> #(time.Seconds, n) |
| 27 | + time.Weeks(n) -> #(time.Weeks, n) |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + let #(constructor, n) = decompose(relative) |
| 32 | + |
| 33 | + case n >=. 0.0 { |
| 34 | + True -> "in " <> time.to_string(relative) |
| 35 | + False -> time.to_string(constructor(float.absolute_value(n))) <> " ago" |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/// Format a `Date`, `TimeOfDay` pair, automatically omitting redundant information (omit year if it matches the current year, omit month and day if it also matches the current day) |
| 40 | +/// |
| 41 | +/// The given date will be compared against the provided "current" date to determine what information to omit. |
| 42 | +/// |
| 43 | +/// This function does not currently support internationalization, and simply returns a string in the following largest-to-smallest format: |
| 44 | +/// ``` |
| 45 | +/// <maybe year> <maybe <month> <day>> <hours>:<minutes>:<seconds> |
| 46 | +/// ``` |
| 47 | +/// Note that hours are in 24 hour format, not 12 hours with AM/PM. |
| 48 | +pub fn date(from date: #(Date, TimeOfDay), now current: Date) -> String { |
| 49 | + let year_matches = case current, date.0 { |
| 50 | + Date(current, ..), Date(given, ..) if current == given -> True |
| 51 | + _, _ -> False |
| 52 | + } |
| 53 | + |
| 54 | + let day_matches = case current, date.0 { |
| 55 | + Date(_, _, current), Date(_, _, given) if current == given -> True |
| 56 | + _, _ -> False |
| 57 | + } |
| 58 | + |
| 59 | + let Date(year, month, day) = date.0 |
| 60 | + let TimeOfDay(hours, minutes, seconds, _) = date.1 |
| 61 | + |
| 62 | + let maybe_year = case year_matches { |
| 63 | + True -> "" |
| 64 | + False -> int.to_string(year) <> " " |
| 65 | + } |
| 66 | + let maybe_month = case year_matches && day_matches, month { |
| 67 | + True, _ -> "" |
| 68 | + _, calendar.April -> "April " |
| 69 | + _, calendar.August -> "August " |
| 70 | + _, calendar.December -> "December " |
| 71 | + _, calendar.February -> "February " |
| 72 | + _, calendar.January -> "January " |
| 73 | + _, calendar.July -> "July " |
| 74 | + _, calendar.June -> "June " |
| 75 | + _, calendar.March -> "March " |
| 76 | + _, calendar.May -> "May " |
| 77 | + _, calendar.November -> "November " |
| 78 | + _, calendar.October -> "October " |
| 79 | + _, calendar.September -> "September " |
| 80 | + } |
| 81 | + let maybe_day = case year_matches && day_matches { |
| 82 | + True -> "" |
| 83 | + False -> int.to_string(day) <> " " |
| 84 | + } |
| 85 | + |
| 86 | + let hours = case hours < 10 { |
| 87 | + True -> "0" <> int.to_string(hours) |
| 88 | + False -> int.to_string(hours) |
| 89 | + } |
| 90 | + let minutes = case minutes < 10 { |
| 91 | + True -> "0" <> int.to_string(minutes) |
| 92 | + False -> int.to_string(minutes) |
| 93 | + } |
| 94 | + let seconds = case seconds < 10 { |
| 95 | + True -> "0" <> int.to_string(seconds) |
| 96 | + False -> int.to_string(seconds) |
| 97 | + } |
| 98 | + |
| 99 | + maybe_year |
| 100 | + <> maybe_month |
| 101 | + <> maybe_day |
| 102 | + <> hours |
| 103 | + <> ":" |
| 104 | + <> minutes |
| 105 | + <> ":" |
| 106 | + <> seconds |
| 107 | +} |
| 108 | + |
| 109 | +/// Format a `Duration`, using the most optimal unit. |
| 110 | +pub fn duration(from duration: Duration) -> String { |
| 111 | + time.from_duration(duration) |> time.to_string |
| 112 | +} |
| 113 | + |
| 114 | +/// Format *n* nanoseconds as a `Float`, converting to a more optimal unit if possible. |
| 115 | +pub fn nanoseconds_float(from n: Float) -> String { |
| 116 | + time.Nanoseconds(n) |> time.humanise |> time.to_string |
| 117 | +} |
| 118 | + |
| 119 | +/// Format *n* nanoseconds as a `Float`, converting to a more optimal unit if possible. |
| 120 | +pub fn nanoseconds_int(from n: Int) -> String { |
| 121 | + time.Nanoseconds(int.to_float(n)) |> time.humanise |> time.to_string |
| 122 | +} |
| 123 | + |
11 | 124 | /// Format *n* microseconds as a `Float`, converting to a more optimal unit if possible. |
12 | 125 | pub fn microseconds_float(from n: Float) -> String { |
13 | 126 | time.Microseconds(n) |> time.humanise |> time.to_string |
|
0 commit comments