Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3297)

Unified Diff: fusl/src/time/__secs_to_tm.c

Issue 1714623002: [fusl] clang-format fusl (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: headers too Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: fusl/src/time/__secs_to_tm.c
diff --git a/fusl/src/time/__secs_to_tm.c b/fusl/src/time/__secs_to_tm.c
index 3a3123a1b457bb65dc7b9c61ecaf017db3f671b1..6017b61ff847e7abb41c6d421672df3f1f09c315 100644
--- a/fusl/src/time/__secs_to_tm.c
+++ b/fusl/src/time/__secs_to_tm.c
@@ -2,80 +2,85 @@
#include <limits.h>
/* 2000-03-01 (mod 400 year, immediately after feb29 */
-#define LEAPOCH (946684800LL + 86400*(31+29))
-
-#define DAYS_PER_400Y (365*400 + 97)
-#define DAYS_PER_100Y (365*100 + 24)
-#define DAYS_PER_4Y (365*4 + 1)
-
-int __secs_to_tm(long long t, struct tm *tm)
-{
- long long days, secs, years;
- int remdays, remsecs, remyears;
- int qc_cycles, c_cycles, q_cycles;
- int months;
- int wday, yday, leap;
- static const char days_in_month[] = {31,30,31,30,31,31,30,31,30,31,31,29};
-
- /* Reject time_t values whose year would overflow int */
- if (t < INT_MIN * 31622400LL || t > INT_MAX * 31622400LL)
- return -1;
-
- secs = t - LEAPOCH;
- days = secs / 86400;
- remsecs = secs % 86400;
- if (remsecs < 0) {
- remsecs += 86400;
- days--;
- }
-
- wday = (3+days)%7;
- if (wday < 0) wday += 7;
-
- qc_cycles = days / DAYS_PER_400Y;
- remdays = days % DAYS_PER_400Y;
- if (remdays < 0) {
- remdays += DAYS_PER_400Y;
- qc_cycles--;
- }
-
- c_cycles = remdays / DAYS_PER_100Y;
- if (c_cycles == 4) c_cycles--;
- remdays -= c_cycles * DAYS_PER_100Y;
-
- q_cycles = remdays / DAYS_PER_4Y;
- if (q_cycles == 25) q_cycles--;
- remdays -= q_cycles * DAYS_PER_4Y;
-
- remyears = remdays / 365;
- if (remyears == 4) remyears--;
- remdays -= remyears * 365;
-
- leap = !remyears && (q_cycles || !c_cycles);
- yday = remdays + 31 + 28 + leap;
- if (yday >= 365+leap) yday -= 365+leap;
-
- years = remyears + 4*q_cycles + 100*c_cycles + 400LL*qc_cycles;
-
- for (months=0; days_in_month[months] <= remdays; months++)
- remdays -= days_in_month[months];
-
- if (years+100 > INT_MAX || years+100 < INT_MIN)
- return -1;
-
- tm->tm_year = years + 100;
- tm->tm_mon = months + 2;
- if (tm->tm_mon >= 12) {
- tm->tm_mon -=12;
- tm->tm_year++;
- }
- tm->tm_mday = remdays + 1;
- tm->tm_wday = wday;
- tm->tm_yday = yday;
-
- tm->tm_hour = remsecs / 3600;
- tm->tm_min = remsecs / 60 % 60;
- tm->tm_sec = remsecs % 60;
-
- return 0;
+#define LEAPOCH (946684800LL + 86400 * (31 + 29))
+
+#define DAYS_PER_400Y (365 * 400 + 97)
+#define DAYS_PER_100Y (365 * 100 + 24)
+#define DAYS_PER_4Y (365 * 4 + 1)
+
+int __secs_to_tm(long long t, struct tm* tm) {
+ long long days, secs, years;
+ int remdays, remsecs, remyears;
+ int qc_cycles, c_cycles, q_cycles;
+ int months;
+ int wday, yday, leap;
+ static const char days_in_month[] = {31, 30, 31, 30, 31, 31,
+ 30, 31, 30, 31, 31, 29};
+
+ /* Reject time_t values whose year would overflow int */
+ if (t < INT_MIN * 31622400LL || t > INT_MAX * 31622400LL)
+ return -1;
+
+ secs = t - LEAPOCH;
+ days = secs / 86400;
+ remsecs = secs % 86400;
+ if (remsecs < 0) {
+ remsecs += 86400;
+ days--;
+ }
+
+ wday = (3 + days) % 7;
+ if (wday < 0)
+ wday += 7;
+
+ qc_cycles = days / DAYS_PER_400Y;
+ remdays = days % DAYS_PER_400Y;
+ if (remdays < 0) {
+ remdays += DAYS_PER_400Y;
+ qc_cycles--;
+ }
+
+ c_cycles = remdays / DAYS_PER_100Y;
+ if (c_cycles == 4)
+ c_cycles--;
+ remdays -= c_cycles * DAYS_PER_100Y;
+
+ q_cycles = remdays / DAYS_PER_4Y;
+ if (q_cycles == 25)
+ q_cycles--;
+ remdays -= q_cycles * DAYS_PER_4Y;
+
+ remyears = remdays / 365;
+ if (remyears == 4)
+ remyears--;
+ remdays -= remyears * 365;
+
+ leap = !remyears && (q_cycles || !c_cycles);
+ yday = remdays + 31 + 28 + leap;
+ if (yday >= 365 + leap)
+ yday -= 365 + leap;
+
+ years = remyears + 4 * q_cycles + 100 * c_cycles + 400LL * qc_cycles;
+
+ for (months = 0; days_in_month[months] <= remdays; months++)
+ remdays -= days_in_month[months];
+
+ if (years + 100 > INT_MAX || years + 100 < INT_MIN)
+ return -1;
+
+ tm->tm_year = years + 100;
+ tm->tm_mon = months + 2;
+ if (tm->tm_mon >= 12) {
+ tm->tm_mon -= 12;
+ tm->tm_year++;
+ }
+ tm->tm_mday = remdays + 1;
+ tm->tm_wday = wday;
+ tm->tm_yday = yday;
+
+ tm->tm_hour = remsecs / 3600;
+ tm->tm_min = remsecs / 60 % 60;
+ tm->tm_sec = remsecs % 60;
+
+ return 0;
}

Powered by Google App Engine
This is Rietveld 408576698