37
"Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
38
};
39
40
+static time_t gm_time_t(unsigned long time, int tz)
41
+{
42
+ int minutes;
43
+
44
+ minutes = tz < 0 ? -tz : tz;
45
+ minutes = (minutes / 100)*60 + (minutes % 100);
46
+ minutes = tz < 0 ? -minutes : minutes;
47
+ return time + minutes * 60;
48
+}
49
+
50
/*
51
* The "tz" thing is passed in as this strange "decimal parse of tz"
52
* thing, which means that tz -0100 is passed in as the integer -100,
54
*/
55
static struct tm *time_to_tm(unsigned long time, int tz)
56
{
47
- time_t t;
48
- int minutes;
49
-
50
- minutes = tz < 0 ? -tz : tz;
51
- minutes = (minutes / 100)*60 + (minutes % 100);
52
- minutes = tz < 0 ? -minutes : minutes;
53
- t = time + minutes * 60;
57
+ time_t t = gm_time_t(time, tz);
58
return gmtime(&t);
59
}
60
57
-const char *show_date(unsigned long time, int tz)
61
+const char *show_date(unsigned long time, int tz, int relative)
62
{
63
struct tm *tm;
64
static char timebuf[200];
65
66
+ if (relative) {
67
+ unsigned long diff;
68
+ time_t t = gm_time_t(time, tz);
69
+ struct timeval now;
70
+ gettimeofday(&now, NULL);
71
+ if (now.tv_sec < t)
72
+ return "in the future";
73
+ diff = now.tv_sec - t;
74
+ if (diff < 90) {
75
+ snprintf(timebuf, sizeof(timebuf), "%lu seconds ago", diff);
76
+ return timebuf;
77
+ }
78
+ /* Turn it into minutes */
79
+ diff = (diff + 30) / 60;
80
+ if (diff < 90) {
81
+ snprintf(timebuf, sizeof(timebuf), "%lu minutes ago", diff);
82
+ return timebuf;
83
+ }
84
+ /* Turn it into hours */
85
+ diff = (diff + 30) / 60;
86
+ if (diff < 36) {
87
+ snprintf(timebuf, sizeof(timebuf), "%lu hours ago", diff);
88
+ return timebuf;
89
+ }
90
+ /* We deal with number of days from here on */
91
+ diff = (diff + 12) / 24;
92
+ if (diff < 14) {
93
+ snprintf(timebuf, sizeof(timebuf), "%lu days ago", diff);
94
+ return timebuf;
95
+ }
96
+ /* Say weeks for the past 10 weeks or so */
97
+ if (diff < 70) {
98
+ snprintf(timebuf, sizeof(timebuf), "%lu weeks ago", (diff + 3) / 7);
99
+ return timebuf;
100
+ }
101
+ /* Say months for the past 12 months or so */
102
+ if (diff < 360) {
103
+ snprintf(timebuf, sizeof(timebuf), "%lu months ago", (diff + 15) / 30);
104
+ return timebuf;
105
+ }
106
+ /* Else fall back on absolute format.. */
107
+ }
108
+
109
tm = time_to_tm(time, tz);
110
if (!tm)
111
return NULL;