diff options
author | Christian Grothoff <christian@grothoff.org> | 2013-09-08 18:07:21 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2013-09-08 18:07:21 +0000 |
commit | 834c2cf9268d7738b314bfb1e0f11484500d4dbd (patch) | |
tree | f8b190e7f7a3af4266f9c2437caf74bae5a5293f /src/util/time.c | |
parent | af0104f64c68a5c5d0b00507494e81d9080b1aa1 (diff) | |
download | gnunet-834c2cf9268d7738b314bfb1e0f11484500d4dbd.tar.gz gnunet-834c2cf9268d7738b314bfb1e0f11484500d4dbd.zip |
-moving time functions from FS to TIME
Diffstat (limited to 'src/util/time.c')
-rw-r--r-- | src/util/time.c | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/util/time.c b/src/util/time.c index 5e9a01b09..fea0947bd 100644 --- a/src/util/time.c +++ b/src/util/time.c | |||
@@ -559,4 +559,74 @@ GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a) | |||
559 | } | 559 | } |
560 | 560 | ||
561 | 561 | ||
562 | /** | ||
563 | * Return the current year (i.e. '2011'). | ||
564 | */ | ||
565 | unsigned int | ||
566 | GNUNET_TIME_get_current_year () | ||
567 | { | ||
568 | time_t tp; | ||
569 | struct tm *t; | ||
570 | |||
571 | tp = time (NULL); | ||
572 | t = gmtime (&tp); | ||
573 | if (t == NULL) | ||
574 | return 0; | ||
575 | return t->tm_year + 1900; | ||
576 | } | ||
577 | |||
578 | |||
579 | /** | ||
580 | * Convert an expiration time to the respective year (rounds) | ||
581 | * | ||
582 | * @param at absolute time | ||
583 | * @return year a year (after 1970), 0 on error | ||
584 | */ | ||
585 | unsigned int | ||
586 | GNUNET_TIME_time_to_year (struct GNUNET_TIME_Absolute at) | ||
587 | { | ||
588 | struct tm *t; | ||
589 | time_t tp; | ||
590 | |||
591 | tp = at.abs_value_us / 1000LL / 1000LL; /* microseconds to seconds */ | ||
592 | t = gmtime (&tp); | ||
593 | if (t == NULL) | ||
594 | return 0; | ||
595 | return t->tm_year + 1900; | ||
596 | |||
597 | } | ||
598 | |||
599 | |||
600 | /** | ||
601 | * Convert a year to an expiration time of January 1st of that year. | ||
602 | * | ||
603 | * @param year a year (after 1970, please ;-)). | ||
604 | * @return absolute time for January 1st of that year. | ||
605 | */ | ||
606 | struct GNUNET_TIME_Absolute | ||
607 | GNUNET_TIME_year_to_time (unsigned int year) | ||
608 | { | ||
609 | struct GNUNET_TIME_Absolute ret; | ||
610 | time_t tp; | ||
611 | struct tm t; | ||
612 | |||
613 | memset (&t, 0, sizeof (t)); | ||
614 | if (year < 1900) | ||
615 | { | ||
616 | GNUNET_break (0); | ||
617 | return GNUNET_TIME_absolute_get (); /* now */ | ||
618 | } | ||
619 | t.tm_year = year - 1900; | ||
620 | t.tm_mday = 1; | ||
621 | t.tm_mon = 1; | ||
622 | t.tm_wday = 1; | ||
623 | t.tm_yday = 1; | ||
624 | tp = mktime (&t); | ||
625 | GNUNET_break (tp != (time_t) - 1); | ||
626 | ret.abs_value_us = tp * 1000LL * 1000LL; /* seconds to microseconds */ | ||
627 | return ret; | ||
628 | } | ||
629 | |||
630 | |||
631 | |||
562 | /* end of time.c */ | 632 | /* end of time.c */ |