Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
relibc
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
schyrsivochter
relibc
Commits
5f098c89
Verified
Commit
5f098c89
authored
6 years ago
by
jD91mZM2
Browse files
Options
Downloads
Patches
Plain Diff
Add more tests for mktime and localtime
parent
a9ea2ef6
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/time/src/lib.rs
+4
-6
4 additions, 6 deletions
src/time/src/lib.rs
tests/mktime.c
+30
-4
30 additions, 4 deletions
tests/mktime.c
with
34 additions
and
10 deletions
src/time/src/lib.rs
+
4
−
6
View file @
5f098c89
...
...
@@ -291,6 +291,8 @@ pub unsafe extern "C" fn mktime(t: *mut tm) -> time_t {
let
mut
month
=
(
*
t
)
.tm_mon
;
let
mut
day
=
(
*
t
)
.tm_mday
as
i64
-
1
;
let
leap
=
if
leap_year
(
year
)
{
1
}
else
{
0
};
if
year
<
1970
{
day
=
MONTH_DAYS
[
if
leap_year
(
year
)
{
1
}
else
{
0
}][(
*
t
)
.tm_mon
as
usize
]
as
i64
-
day
;
...
...
@@ -299,8 +301,6 @@ pub unsafe extern "C" fn mktime(t: *mut tm) -> time_t {
day
+=
if
leap_year
(
year
)
{
366
}
else
{
365
};
}
let
leap
=
if
leap_year
(
year
)
{
1
}
else
{
0
};
while
month
<
11
{
month
+=
1
;
day
+=
MONTH_DAYS
[
leap
][
month
as
usize
]
as
i64
;
...
...
@@ -312,15 +312,13 @@ pub unsafe extern "C" fn mktime(t: *mut tm) -> time_t {
+
(
*
t
)
.tm_sec
as
i64
))
}
else
{
while
year
>
1970
{
day
+=
if
leap_year
(
year
)
{
366
}
else
{
365
};
year
-=
1
;
day
+=
if
leap_year
(
year
)
{
366
}
else
{
365
};
}
let
leap
=
if
leap_year
(
year
)
{
1
}
else
{
0
};
while
month
>
0
{
day
+=
MONTH_DAYS
[
leap
][
month
as
usize
]
as
i64
;
month
-=
1
;
day
+=
MONTH_DAYS
[
leap
][
month
as
usize
]
as
i64
;
}
(
day
*
(
60
*
60
*
24
)
...
...
This diff is collapsed.
Click to expand it.
tests/mktime.c
+
30
−
4
View file @
5f098c89
#include
<assert.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<time.h>
int
check
(
time_t
input
)
{
struct
tm
*
t
=
localtime
(
&
input
);
printf
(
"%ld = %ld
\n
"
,
input
,
mktime
(
t
));
if
(
input
!=
mktime
(
t
))
{
printf
(
"Year %d, Day of year: %d, Month %d, Day of month: %d, Day of week: %d, %d:%d:%d
\n
"
,
t
->
tm_year
,
t
->
tm_yday
,
t
->
tm_mon
,
t
->
tm_mday
,
t
->
tm_wday
,
t
->
tm_hour
,
t
->
tm_min
,
t
->
tm_sec
);
puts
(
"Failed!"
);
return
-
1
;
}
return
0
;
}
int
main
()
{
struct
tm
t
=
{};
...
...
@@ -12,11 +29,20 @@ int main() {
int
day
=
60
*
60
*
24
;
time_t
inputs
[]
=
{
-
(
day
*
33
),
-
day
,
-
500
,
0
,
1531454950
};
for
(
int
i
=
0
;
i
<
5
;
i
+=
1
)
{
struct
tm
*
t2
=
localtime
(
&
inputs
[
i
]);
if
(
check
(
inputs
[
i
]))
{
return
-
1
;
}
}
srand
(
time
(
NULL
));
printf
(
"%ld = %ld
\n
"
,
inputs
[
i
],
mktime
(
t2
));
if
(
inputs
[
i
]
!=
mktime
(
t2
))
{
puts
(
"Failed!"
);
for
(
int
i
=
0
;
i
<
10
;
i
+=
1
)
{
time_t
input
=
(
time_t
)
rand
();
struct
tm
*
time
=
localtime
(
&
input
);
time_t
output
=
mktime
(
time
);
if
(
input
!=
output
)
{
// asctime has newline
printf
(
"Comparison %ld == %ld failed. Time: %s"
,
input
,
output
,
asctime
(
time
));
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment