@@ -100,6 +100,8 @@ def validate_discord_token(token: str) -> bool:
100
100
else :
101
101
return True
102
102
103
+ def pluralize (count : int , singular : str ) -> str :
104
+ return singular if count == 1 else singular + "s"
103
105
104
106
def natural_time (
105
107
td : datetime .timedelta ,
@@ -113,23 +115,45 @@ def natural_time(
113
115
future = then > now
114
116
115
117
ago = "{delta} from now" if future else "{delta} ago"
116
-
117
118
seconds = round (td .total_seconds ())
119
+ years , seconds = divmod (seconds , 60 * 60 * 24 * 365 )
120
+ months , seconds = divmod (seconds , 60 * 60 * 24 * 30 )
118
121
weeks , seconds = divmod (seconds , 60 * 60 * 24 * 7 )
119
122
days , seconds = divmod (seconds , 60 * 60 * 24 )
120
123
hours , seconds = divmod (seconds , 60 * 60 )
121
124
minutes , seconds = divmod (seconds , 60 )
122
125
123
126
ret = ""
124
127
125
- if weeks :
126
- ret += f"{ weeks } weeks,"
127
- if days :
128
- ret += f"{ days } days,"
129
- if hours :
130
- ret += f"{ hours } hours,"
131
- if minutes :
132
- ret += f"{ minutes } minutes and "
133
- ret += f"{ seconds } seconds"
134
-
135
- return ago .format (delta = ret )
128
+ if years :
129
+ ret += f"{ years } { pluralize (years , 'year' )} "
130
+ if months :
131
+ ret += f", { months } { pluralize (months , 'month' )} "
132
+ elif months :
133
+ ret += f"{ months } { pluralize (months , 'month' )} "
134
+ elif weeks :
135
+ ret += f"{ weeks } { pluralize (weeks , 'week' )} "
136
+ if days :
137
+ ret += f", { days } { pluralize (days , 'day' )} "
138
+ elif days :
139
+ ret += f"{ days } { pluralize (days , 'day' )} "
140
+
141
+ if hours and not years and not months and not weeks and not days :
142
+ if ret :
143
+ ret += ", "
144
+ ret += f"{ hours } { pluralize (hours , 'hour' )} "
145
+ if (
146
+ minutes
147
+ and not years
148
+ and not months
149
+ and not weeks
150
+ and not days
151
+ and not hours
152
+ ):
153
+ if ret :
154
+ ret += ", "
155
+ ret += f"{ minutes } { pluralize (minutes , 'minute' )} "
156
+
157
+ formatted_ret = ", " .join (ret .split (", " )[:2 ])
158
+
159
+ return ago .format (delta = formatted_ret )
0 commit comments