working again
[oweals/gnunet.git] / src / util / time.c
index 5e9a01b0948fb60c228b0f063a6876b5d2ca2caa..a615756280b803db0a4925edf6341e9a73de0742 100644 (file)
@@ -24,7 +24,7 @@
  * @brief functions for handling time and time arithmetic
  */
 #include "platform.h"
-#include "gnunet_time_lib.h"
+#include "gnunet_util_lib.h"
 
 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
 
@@ -50,7 +50,7 @@ GNUNET_TIME_set_offset (long long offset)
  *
  * @return the offset we currently skew the locale time by
  */
-long long 
+long long
 GNUNET_TIME_get_offset ()
 {
   return timestamp_offset;
@@ -385,6 +385,8 @@ GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
 
   if (0 == factor)
     return GNUNET_TIME_UNIT_ZERO;
+  if (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
+    return GNUNET_TIME_UNIT_FOREVER_REL;
   ret.rel_value_us = rel.rel_value_us * (unsigned long long) factor;
   if (ret.rel_value_us / factor != rel.rel_value_us)
   {
@@ -559,4 +561,74 @@ GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a)
 }
 
 
+/**
+ * Return the current year (i.e. '2011').
+ */
+unsigned int
+GNUNET_TIME_get_current_year ()
+{
+  time_t tp;
+  struct tm *t;
+
+  tp = time (NULL);
+  t = gmtime (&tp);
+  if (t == NULL)
+    return 0;
+  return t->tm_year + 1900;
+}
+
+
+/**
+ * Convert an expiration time to the respective year (rounds)
+ *
+ * @param at absolute time
+ * @return year a year (after 1970), 0 on error
+ */
+unsigned int
+GNUNET_TIME_time_to_year (struct GNUNET_TIME_Absolute at)
+{
+  struct tm *t;
+  time_t tp;
+
+  tp = at.abs_value_us / 1000LL / 1000LL;    /* microseconds to seconds */
+  t = gmtime (&tp);
+  if (t == NULL)
+    return 0;
+  return t->tm_year + 1900;
+
+}
+
+
+/**
+ * Convert a year to an expiration time of January 1st of that year.
+ *
+ * @param year a year (after 1970, please ;-)).
+ * @return absolute time for January 1st of that year.
+ */
+struct GNUNET_TIME_Absolute
+GNUNET_TIME_year_to_time (unsigned int year)
+{
+  struct GNUNET_TIME_Absolute ret;
+  time_t tp;
+  struct tm t;
+
+  memset (&t, 0, sizeof (t));
+  if (year < 1900)
+  {
+    GNUNET_break (0);
+    return GNUNET_TIME_absolute_get (); /* now */
+  }
+  t.tm_year = year - 1900;
+  t.tm_mday = 1;
+  t.tm_mon = 1;
+  t.tm_wday = 1;
+  t.tm_yday = 1;
+  tp = mktime (&t);
+  GNUNET_break (tp != (time_t) - 1);
+  ret.abs_value_us = tp * 1000LL * 1000LL;  /* seconds to microseconds */
+  return ret;
+}
+
+
+
 /* end of time.c */