32
32
/* Note: EmpathyTime is always in UTC. */
35
35
empathy_time_get_current (void)
41
empathy_time_get_local_time (struct tm *tm)
47
g_setenv ("TZ", "", TRUE);
54
g_setenv ("TZ", tz, TRUE);
64
/* The format is: "20021209T23:51:30" and is in UTC. 0 is returned on
65
* failure. The alternative format "20021209" is also accepted.
68
empathy_time_parse (const gchar *str)
74
memset (&tm, 0, sizeof (struct tm));
76
n_parsed = sscanf (str, "%4d%2d%2dT%2d:%2d:%2d",
77
&year, &month, &tm.tm_mday, &tm.tm_hour,
78
&tm.tm_min, &tm.tm_sec);
79
if (n_parsed != 3 && n_parsed != 6) {
83
tm.tm_year = year - 1900;
84
tm.tm_mon = month - 1;
87
return empathy_time_get_local_time (&tm);
40
now = g_date_time_new_now_utc ();
41
result = g_date_time_to_unix (now);
42
g_date_time_unref (now);
90
47
/* Converts the UTC timestamp to a string, also in UTC. Returns NULL on failure. */
92
empathy_time_to_string_utc (time_t t,
49
empathy_time_to_string_utc (gint64 t,
93
50
const gchar *format)
98
55
g_return_val_if_fail (format != NULL, NULL);
101
if (strftime (stamp, sizeof (stamp), format, tm) == 0) {
57
d = g_date_time_new_from_unix_utc (t);
58
result = g_date_time_format (d, format);
59
g_date_time_unref (d);
105
return g_strdup (stamp);
108
64
/* Converts the UTC timestamp to a string, in local time. Returns NULL on failure. */
110
empathy_time_to_string_local (time_t t,
66
empathy_time_to_string_local (gint64 t,
111
67
const gchar *format)
116
72
g_return_val_if_fail (format != NULL, NULL);
119
if (strftime (stamp, sizeof (stamp), format, tm) == 0) {
123
return g_strdup (stamp);
74
d = g_date_time_new_from_unix_utc (t);
75
local = g_date_time_to_local (d);
76
g_date_time_unref (d);
78
result = g_date_time_format (local, format);
79
g_date_time_unref (local);
127
empathy_time_to_string_relative (time_t then)
85
empathy_time_to_string_relative (gint64 t)
87
GDateTime *now, *then;
133
seconds = now - then;
92
now = g_date_time_new_now_utc ();
93
then = g_date_time_new_from_unix_utc (t);
95
delta = g_date_time_difference (now, then);
96
seconds = delta / G_TIME_SPAN_SECOND;
135
98
if (seconds > 0) {
136
99
if (seconds < 60) {
137
return g_strdup_printf (ngettext ("%d second ago",
100
result = g_strdup_printf (ngettext ("%d second ago",
138
101
"%d seconds ago", seconds), seconds);
140
103
else if (seconds < (60 * 60)) {
142
return g_strdup_printf (ngettext ("%d minute ago",
105
result = g_strdup_printf (ngettext ("%d minute ago",
143
106
"%d minutes ago", seconds), seconds);
145
108
else if (seconds < (60 * 60 * 24)) {
146
109
seconds /= 60 * 60;
147
return g_strdup_printf (ngettext ("%d hour ago",
110
result = g_strdup_printf (ngettext ("%d hour ago",
148
111
"%d hours ago", seconds), seconds);
150
113
else if (seconds < (60 * 60 * 24 * 7)) {
151
114
seconds /= 60 * 60 * 24;
152
return g_strdup_printf (ngettext ("%d day ago",
115
result = g_strdup_printf (ngettext ("%d day ago",
153
116
"%d days ago", seconds), seconds);
155
118
else if (seconds < (60 * 60 * 24 * 30)) {
156
119
seconds /= 60 * 60 * 24 * 7;
157
return g_strdup_printf (ngettext ("%d week ago",
120
result = g_strdup_printf (ngettext ("%d week ago",
158
121
"%d weeks ago", seconds), seconds);
161
124
seconds /= 60 * 60 * 24 * 30;
162
return g_strdup_printf (ngettext ("%d month ago",
125
result = g_strdup_printf (ngettext ("%d month ago",
163
126
"%d months ago", seconds), seconds);
167
return g_strdup (_("in the future"));
130
result = g_strdup (_("in the future"));
133
g_date_time_unref (now);
134
g_date_time_unref (then);