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

Side by Side 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 unified diff | Download patch
OLDNEW
1 #include "time_impl.h" 1 #include "time_impl.h"
2 #include <limits.h> 2 #include <limits.h>
3 3
4 /* 2000-03-01 (mod 400 year, immediately after feb29 */ 4 /* 2000-03-01 (mod 400 year, immediately after feb29 */
5 #define LEAPOCH (946684800LL + 86400*(31+29)) 5 #define LEAPOCH (946684800LL + 86400 * (31 + 29))
6 6
7 #define DAYS_PER_400Y (365*400 + 97) 7 #define DAYS_PER_400Y (365 * 400 + 97)
8 #define DAYS_PER_100Y (365*100 + 24) 8 #define DAYS_PER_100Y (365 * 100 + 24)
9 #define DAYS_PER_4Y (365*4 + 1) 9 #define DAYS_PER_4Y (365 * 4 + 1)
10 10
11 int __secs_to_tm(long long t, struct tm *tm) 11 int __secs_to_tm(long long t, struct tm* tm) {
12 { 12 long long days, secs, years;
13 » long long days, secs, years; 13 int remdays, remsecs, remyears;
14 » int remdays, remsecs, remyears; 14 int qc_cycles, c_cycles, q_cycles;
15 » int qc_cycles, c_cycles, q_cycles; 15 int months;
16 » int months; 16 int wday, yday, leap;
17 » int wday, yday, leap; 17 static const char days_in_month[] = {31, 30, 31, 30, 31, 31,
18 » static const char days_in_month[] = {31,30,31,30,31,31,30,31,30,31,31,29 }; 18 30, 31, 30, 31, 31, 29};
19 19
20 » /* Reject time_t values whose year would overflow int */ 20 /* Reject time_t values whose year would overflow int */
21 » if (t < INT_MIN * 31622400LL || t > INT_MAX * 31622400LL) 21 if (t < INT_MIN * 31622400LL || t > INT_MAX * 31622400LL)
22 » » return -1; 22 return -1;
23 23
24 » secs = t - LEAPOCH; 24 secs = t - LEAPOCH;
25 » days = secs / 86400; 25 days = secs / 86400;
26 » remsecs = secs % 86400; 26 remsecs = secs % 86400;
27 » if (remsecs < 0) { 27 if (remsecs < 0) {
28 » » remsecs += 86400; 28 remsecs += 86400;
29 » » days--; 29 days--;
30 » } 30 }
31 31
32 » wday = (3+days)%7; 32 wday = (3 + days) % 7;
33 » if (wday < 0) wday += 7; 33 if (wday < 0)
34 wday += 7;
34 35
35 » qc_cycles = days / DAYS_PER_400Y; 36 qc_cycles = days / DAYS_PER_400Y;
36 » remdays = days % DAYS_PER_400Y; 37 remdays = days % DAYS_PER_400Y;
37 » if (remdays < 0) { 38 if (remdays < 0) {
38 » » remdays += DAYS_PER_400Y; 39 remdays += DAYS_PER_400Y;
39 » » qc_cycles--; 40 qc_cycles--;
40 » } 41 }
41 42
42 » c_cycles = remdays / DAYS_PER_100Y; 43 c_cycles = remdays / DAYS_PER_100Y;
43 » if (c_cycles == 4) c_cycles--; 44 if (c_cycles == 4)
44 » remdays -= c_cycles * DAYS_PER_100Y; 45 c_cycles--;
46 remdays -= c_cycles * DAYS_PER_100Y;
45 47
46 » q_cycles = remdays / DAYS_PER_4Y; 48 q_cycles = remdays / DAYS_PER_4Y;
47 » if (q_cycles == 25) q_cycles--; 49 if (q_cycles == 25)
48 » remdays -= q_cycles * DAYS_PER_4Y; 50 q_cycles--;
51 remdays -= q_cycles * DAYS_PER_4Y;
49 52
50 » remyears = remdays / 365; 53 remyears = remdays / 365;
51 » if (remyears == 4) remyears--; 54 if (remyears == 4)
52 » remdays -= remyears * 365; 55 remyears--;
56 remdays -= remyears * 365;
53 57
54 » leap = !remyears && (q_cycles || !c_cycles); 58 leap = !remyears && (q_cycles || !c_cycles);
55 » yday = remdays + 31 + 28 + leap; 59 yday = remdays + 31 + 28 + leap;
56 » if (yday >= 365+leap) yday -= 365+leap; 60 if (yday >= 365 + leap)
61 yday -= 365 + leap;
57 62
58 » years = remyears + 4*q_cycles + 100*c_cycles + 400LL*qc_cycles; 63 years = remyears + 4 * q_cycles + 100 * c_cycles + 400LL * qc_cycles;
59 64
60 » for (months=0; days_in_month[months] <= remdays; months++) 65 for (months = 0; days_in_month[months] <= remdays; months++)
61 » » remdays -= days_in_month[months]; 66 remdays -= days_in_month[months];
62 67
63 » if (years+100 > INT_MAX || years+100 < INT_MIN) 68 if (years + 100 > INT_MAX || years + 100 < INT_MIN)
64 » » return -1; 69 return -1;
65 70
66 » tm->tm_year = years + 100; 71 tm->tm_year = years + 100;
67 » tm->tm_mon = months + 2; 72 tm->tm_mon = months + 2;
68 » if (tm->tm_mon >= 12) { 73 if (tm->tm_mon >= 12) {
69 » » tm->tm_mon -=12; 74 tm->tm_mon -= 12;
70 » » tm->tm_year++; 75 tm->tm_year++;
71 » } 76 }
72 » tm->tm_mday = remdays + 1; 77 tm->tm_mday = remdays + 1;
73 » tm->tm_wday = wday; 78 tm->tm_wday = wday;
74 » tm->tm_yday = yday; 79 tm->tm_yday = yday;
75 80
76 » tm->tm_hour = remsecs / 3600; 81 tm->tm_hour = remsecs / 3600;
77 » tm->tm_min = remsecs / 60 % 60; 82 tm->tm_min = remsecs / 60 % 60;
78 » tm->tm_sec = remsecs % 60; 83 tm->tm_sec = remsecs % 60;
79 84
80 » return 0; 85 return 0;
81 } 86 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698