| 79 | | |
|---|
| 80 | | gchar * |
|---|
| 81 | | _get_readable_date (time_t mtime) |
|---|
| 82 | | { |
|---|
| 83 | | time_t now; |
|---|
| 84 | | struct tm file_time; |
|---|
| 85 | | |
|---|
| 86 | | /** |
|---|
| 87 | | * We reuse a static char. Now hold your breath and listen. This is not |
|---|
| 88 | | * wrong and I'll explain why: each component that uses this function |
|---|
| 89 | | * is called from the Gtk+ context. The Gtk+ context can never run in |
|---|
| 90 | | * parallel with another context because Gtk+ is locked using its |
|---|
| 91 | | * gdk_threads_enter/leave lock. This means that there's always at most |
|---|
| 92 | | * one thread doing Gtk+ things. This means that this function does not |
|---|
| 93 | | * have to be thread safe. And that means that we can have a static |
|---|
| 94 | | * buffer that we'll play with. Else we could also do a pthread_once |
|---|
| 95 | | * or something. |
|---|
| 96 | | * |
|---|
| 97 | | * I here the children cry things like: waaayyyy?? Well the reason is to |
|---|
| 98 | | * avoid a malloc(). This stuff happens quite often. If we can reuse one |
|---|
| 99 | | * buffer each time, that's fine! The GtkTreeView (if this time that is |
|---|
| 100 | | * consumer) makes its own copy. Same for any other Gtk+ component that |
|---|
| 101 | | * accepts strings (like a GtkEntry or a GtkLabel). It will make its own |
|---|
| 102 | | * copy anyway! If we are going to copy here, that's two copies rather |
|---|
| 103 | | * than just the one copy in Gtk+ that we have no control over. |
|---|
| 104 | | **/ |
|---|
| 105 | | |
|---|
| 106 | | static gchar readable_date[64]; |
|---|
| 107 | | |
|---|
| 108 | | gsize readable_date_size; |
|---|
| 109 | | int days_now, days_then; |
|---|
| 110 | | |
|---|
| 111 | | gmtime_r (&mtime, &file_time); |
|---|
| 112 | | now = time (NULL); |
|---|
| 113 | | |
|---|
| 114 | | /* The idea is that if the amount of days is not the same, we display |
|---|
| 115 | | * the full date. Else we just display the time. Note that there's a |
|---|
| 116 | | * hidden bug in this: days_then is the remote timezone, days_now is the |
|---|
| 117 | | * local timezone. At some point we should fix this! */ |
|---|
| 118 | | |
|---|
| 119 | | days_now = now / (24*60*60); |
|---|
| 120 | | days_then = mtime / (24*60*60); |
|---|
| 121 | | |
|---|
| 122 | | /* I think we can fix this using localtime_r */ |
|---|
| 123 | | |
|---|
| 124 | | if (days_now == days_then) { |
|---|
| 125 | | /* Translator: this date-format is the date format for when creating a |
|---|
| 126 | | * reply of a message if the date is today */ |
|---|
| 127 | | readable_date_size = strftime (readable_date, 63, _("%X"), &file_time); |
|---|
| 128 | | } else { |
|---|
| 129 | | /* Translator: this date-format is the date format for when creating a |
|---|
| 130 | | * reply of a message if the date is not today */ |
|---|
| 131 | | readable_date_size = strftime (readable_date, 63, _("%x"), &file_time); |
|---|
| 132 | | } |
|---|
| 133 | | |
|---|
| 134 | | if (readable_date_size > 0) |
|---|
| 135 | | return readable_date; |
|---|
| 136 | | |
|---|
| 137 | | return NULL; |
|---|
| 138 | | } |
|---|