Silence two -Wlogical-op warnings.
[gnupg.git] / common / gettime.c
index 115f725..e5da4fb 100644 (file)
@@ -24,7 +24,7 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ * along with this program; if not, see <https://www.gnu.org/licenses/>.
  */
 
 #include <config.h>
@@ -60,6 +60,9 @@ time_t
 gnupg_get_time ()
 {
   time_t current = time (NULL);
+  if (current == (time_t)(-1))
+    log_fatal ("time() failed\n");
+
   if (timemode == NORMAL)
     return current;
   else if (timemode == FROZEN)
@@ -99,22 +102,16 @@ void
 gnupg_get_isotime (gnupg_isotime_t timebuf)
 {
   time_t atime = gnupg_get_time ();
+  struct tm *tp;
+  struct tm tmbuf;
 
-  if (atime == (time_t)(-1))
+  tp = gnupg_gmtime (&atime, &tmbuf);
+  if (!tp)
     *timebuf = 0;
   else
-    {
-      struct tm *tp;
-      struct tm tmbuf;
-
-      tp = gnupg_gmtime (&atime, &tmbuf);
-      if (!tp)
-        *timebuf = 0;
-      else
-        snprintf (timebuf, 16, "%04d%02d%02dT%02d%02d%02d",
-                  1900 + tp->tm_year, tp->tm_mon+1, tp->tm_mday,
-                  tp->tm_hour, tp->tm_min, tp->tm_sec);
-    }
+    snprintf (timebuf, 16, "%04d%02d%02dT%02d%02d%02d",
+              1900 + tp->tm_year, tp->tm_mon+1, tp->tm_mday,
+              tp->tm_hour, tp->tm_min, tp->tm_sec);
 }
 
 
@@ -164,9 +161,6 @@ u32
 make_timestamp (void)
 {
   time_t t = gnupg_get_time ();
-
-  if (t == (time_t)-1)
-    log_fatal ("gnupg_get_time() failed\n");
   return (u32)t;
 }
 
@@ -723,6 +717,39 @@ asctimestamp (u32 stamp)
 }
 
 
+/* Return the timestamp STAMP in RFC-2822 format.  This is always done
+ * in the C locale.  We return the gmtime to avoid computing the
+ * timezone. The caller must release the returned string.
+ *
+ * Example: "Mon, 27 Jun 2016 1:42:00 +0000".
+ */
+char *
+rfctimestamp (u32 stamp)
+{
+  time_t atime = stamp;
+  struct tm tmbuf, *tp;
+
+
+  if (IS_INVALID_TIME_T (atime))
+    {
+      gpg_err_set_errno (EINVAL);
+      return NULL;
+    }
+
+  tp = gnupg_gmtime (&atime, &tmbuf);
+  if (!tp)
+    return NULL;
+  return xtryasprintf ("%.3s, %02d %.3s %04d %02d:%02d:%02d +0000",
+                       &"SunMonTueWedThuFriSat"[(tp->tm_wday%7)*3],
+                       tp->tm_mday,
+                       &"JanFebMarAprMayJunJulAugSepOctNovDec"
+                       [(tp->tm_mon%12)*3],
+                       tp->tm_year + 1900,
+                       tp->tm_hour,
+                       tp->tm_min,
+                       tp->tm_sec);
+}
+
 
 static int
 days_per_year (int y)