MCPcopy Create free account
hub / github.com/git/git / show_date_relative

Function show_date_relative

date.c:137–210  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

135}
136
137void show_date_relative(timestamp_t time, struct strbuf *timebuf)
138{
139 struct timeval now;
140 timestamp_t diff;
141
142 get_time(&now);
143 if (now.tv_sec < time) {
144 strbuf_addstr(timebuf, _("in the future"));
145 return;
146 }
147 diff = now.tv_sec - time;
148 if (diff < 90) {
149 strbuf_addf(timebuf,
150 Q_("%"PRItime" second ago", "%"PRItime" seconds ago", diff), diff);
151 return;
152 }
153 /* Turn it into minutes */
154 diff = (diff + 30) / 60;
155 if (diff < 90) {
156 strbuf_addf(timebuf,
157 Q_("%"PRItime" minute ago", "%"PRItime" minutes ago", diff), diff);
158 return;
159 }
160 /* Turn it into hours */
161 diff = (diff + 30) / 60;
162 if (diff < 36) {
163 strbuf_addf(timebuf,
164 Q_("%"PRItime" hour ago", "%"PRItime" hours ago", diff), diff);
165 return;
166 }
167 /* We deal with number of days from here on */
168 diff = (diff + 12) / 24;
169 if (diff < 14) {
170 strbuf_addf(timebuf,
171 Q_("%"PRItime" day ago", "%"PRItime" days ago", diff), diff);
172 return;
173 }
174 /* Say weeks for the past 10 weeks or so */
175 if (diff < 70) {
176 strbuf_addf(timebuf,
177 Q_("%"PRItime" week ago", "%"PRItime" weeks ago", (diff + 3) / 7),
178 (diff + 3) / 7);
179 return;
180 }
181 /* Say months for the past 12 months or so */
182 if (diff < 365) {
183 strbuf_addf(timebuf,
184 Q_("%"PRItime" month ago", "%"PRItime" months ago", (diff + 15) / 30),
185 (diff + 15) / 30);
186 return;
187 }
188 /* Give years and months for 5 years or so */
189 if (diff < 1825) {
190 timestamp_t totalmonths = (diff * 12 * 2 + 365) / (365 * 2);
191 timestamp_t years = totalmonths / 12;
192 timestamp_t months = totalmonths % 12;
193 if (months) {
194 struct strbuf sb = STRBUF_INIT;

Callers 3

show_date_normalFunction · 0.85
show_dateFunction · 0.85
show_relative_datesFunction · 0.85

Calls 4

get_timeFunction · 0.85
strbuf_addstrFunction · 0.85
strbuf_addfFunction · 0.85
strbuf_releaseFunction · 0.85

Tested by 1

show_relative_datesFunction · 0.68