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

Side by Side Diff: fusl/src/time/__secs_to_tm.c

Issue 1573973002: Add a "fork" of musl as //fusl. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 11 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
« no previous file with comments | « fusl/src/time/__month_to_secs.c ('k') | fusl/src/time/__tm_to_secs.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #include "time_impl.h"
2 #include <limits.h>
3
4 /* 2000-03-01 (mod 400 year, immediately after feb29 */
5 #define LEAPOCH (946684800LL + 86400*(31+29))
6
7 #define DAYS_PER_400Y (365*400 + 97)
8 #define DAYS_PER_100Y (365*100 + 24)
9 #define DAYS_PER_4Y (365*4 + 1)
10
11 int __secs_to_tm(long long t, struct tm *tm)
12 {
13 long long days, secs, years;
14 int remdays, remsecs, remyears;
15 int qc_cycles, c_cycles, q_cycles;
16 int months;
17 int wday, yday, leap;
18 static const char days_in_month[] = {31,30,31,30,31,31,30,31,30,31,31,29 };
19
20 /* Reject time_t values whose year would overflow int */
21 if (t < INT_MIN * 31622400LL || t > INT_MAX * 31622400LL)
22 return -1;
23
24 secs = t - LEAPOCH;
25 days = secs / 86400;
26 remsecs = secs % 86400;
27 if (remsecs < 0) {
28 remsecs += 86400;
29 days--;
30 }
31
32 wday = (3+days)%7;
33 if (wday < 0) wday += 7;
34
35 qc_cycles = days / DAYS_PER_400Y;
36 remdays = days % DAYS_PER_400Y;
37 if (remdays < 0) {
38 remdays += DAYS_PER_400Y;
39 qc_cycles--;
40 }
41
42 c_cycles = remdays / DAYS_PER_100Y;
43 if (c_cycles == 4) c_cycles--;
44 remdays -= c_cycles * DAYS_PER_100Y;
45
46 q_cycles = remdays / DAYS_PER_4Y;
47 if (q_cycles == 25) q_cycles--;
48 remdays -= q_cycles * DAYS_PER_4Y;
49
50 remyears = remdays / 365;
51 if (remyears == 4) remyears--;
52 remdays -= remyears * 365;
53
54 leap = !remyears && (q_cycles || !c_cycles);
55 yday = remdays + 31 + 28 + leap;
56 if (yday >= 365+leap) yday -= 365+leap;
57
58 years = remyears + 4*q_cycles + 100*c_cycles + 400LL*qc_cycles;
59
60 for (months=0; days_in_month[months] <= remdays; months++)
61 remdays -= days_in_month[months];
62
63 if (years+100 > INT_MAX || years+100 < INT_MIN)
64 return -1;
65
66 tm->tm_year = years + 100;
67 tm->tm_mon = months + 2;
68 if (tm->tm_mon >= 12) {
69 tm->tm_mon -=12;
70 tm->tm_year++;
71 }
72 tm->tm_mday = remdays + 1;
73 tm->tm_wday = wday;
74 tm->tm_yday = yday;
75
76 tm->tm_hour = remsecs / 3600;
77 tm->tm_min = remsecs / 60 % 60;
78 tm->tm_sec = remsecs % 60;
79
80 return 0;
81 }
OLDNEW
« no previous file with comments | « fusl/src/time/__month_to_secs.c ('k') | fusl/src/time/__tm_to_secs.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698