Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
0c12cbed
Commit
0c12cbed
authored
Jul 04, 2018
by
antirez
Browse files
Localtime: compute year, month and day of the month.
parent
06ca400f
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/localtime.c
View file @
0c12cbed
...
...
@@ -69,4 +69,30 @@ void nolocks_localtime(struct tm *tmp, time_t t, time_t tz, int dst) {
* where sunday = 0, so to calculate the day of the week we have to add 4
* and take the modulo by 7. */
tmp
->
tm_wday
=
(
days
+
4
)
%
7
;
/* Calculate the current year. */
tmp
->
tm_year
=
1970
;
while
(
1
)
{
/* Leap years have one year more. */
time_t
days_this_year
=
365
+
is_leap_year
(
tmp
->
tm_year
);
if
(
days_this_year
>
days
)
break
;
days
-=
days_this_year
;
tmp
->
tm_year
++
;
}
tmp
->
tm_yday
=
days
;
/* Number of day of the current year. */
/* We need to calculate in which month and day of the month we are. To do
* so we need to skip days according to how many days there are in each
* month, and adjust for the leap year that has one more day in February. */
int
mdays
[
12
]
=
{
31
,
28
,
31
,
30
,
31
,
30
,
31
,
31
,
30
,
31
,
30
,
31
};
mdays
[
1
]
+=
is_leap_year
(
tmp
->
tm_year
);
tmp
->
tm_mon
=
0
;
while
(
days
>=
mdays
[
tmp
->
tm_mon
])
{
days
-=
mdays
[
tmp
->
tm_mon
];
tmp
->
tm_mon
++
;
}
tmp
->
tm_mday
=
days
;
tmp
->
tm_year
-=
1900
;
/* Surprisingly tm_year is year-1900. */
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment