time_test.cpp revision f0c10a4a16c74f5099cf6b272c0a2f11f454e63c
1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <sys/cdefs.h>
18#include <features.h>
19#include <gtest/gtest.h>
20
21#include <time.h>
22
23#ifdef __BIONIC__ // mktime_tz is a bionic extension.
24#include <libc/private/bionic_time.h>
25TEST(time, mktime_tz) {
26  struct tm epoch;
27  memset(&epoch, 0, sizeof(tm));
28  epoch.tm_year = 1970 - 1900;
29  epoch.tm_mon = 1;
30  epoch.tm_mday = 1;
31
32  // Alphabetically first. Coincidentally equivalent to UTC.
33  ASSERT_EQ(2678400, mktime_tz(&epoch, "Africa/Abidjan"));
34
35  // Alphabetically last. Coincidentally equivalent to UTC.
36  ASSERT_EQ(2678400, mktime_tz(&epoch, "Zulu"));
37
38  // Somewhere in the middle, not UTC.
39  ASSERT_EQ(2707200, mktime_tz(&epoch, "America/Los_Angeles"));
40
41  // Missing. Falls back to UTC.
42  ASSERT_EQ(2678400, mktime_tz(&epoch, "PST"));
43}
44#endif
45
46TEST(time, gmtime) {
47  time_t t = 0;
48  tm* broken_down = gmtime(&t);
49  ASSERT_TRUE(broken_down != NULL);
50  ASSERT_EQ(0, broken_down->tm_sec);
51  ASSERT_EQ(0, broken_down->tm_min);
52  ASSERT_EQ(0, broken_down->tm_hour);
53  ASSERT_EQ(1, broken_down->tm_mday);
54  ASSERT_EQ(0, broken_down->tm_mon);
55  ASSERT_EQ(1970, broken_down->tm_year + 1900);
56}
57
58#ifdef __BIONIC__
59TEST(time, mktime_10310929) {
60  struct tm t;
61  memset(&t, 0, sizeof(tm));
62  t.tm_year = 200;
63  t.tm_mon = 2;
64  t.tm_mday = 10;
65
66  ASSERT_EQ(-1, mktime(&t));
67  ASSERT_EQ(-1, mktime_tz(&t, "UTC"));
68}
69#endif
70