blob: 869ee4baa6a214bec81905ac84dbce0299130e63 [file] [log] [blame]
Elliott Hughese0175ca2013-03-14 14:38:08 -07001/*
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
Christopher Ferrisf04935c2013-12-20 18:43:21 -080023#if defined(__BIONIC__) // mktime_tz is a bionic extension.
Elliott Hughese0175ca2013-03-14 14:38:08 -070024#include <libc/private/bionic_time.h>
Christopher Ferrisf04935c2013-12-20 18:43:21 -080025#endif // __BIONIC__
26
Elliott Hughese0175ca2013-03-14 14:38:08 -070027TEST(time, mktime_tz) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -080028#if defined(__BIONIC__)
Elliott Hughese0175ca2013-03-14 14:38:08 -070029 struct tm epoch;
30 memset(&epoch, 0, sizeof(tm));
31 epoch.tm_year = 1970 - 1900;
32 epoch.tm_mon = 1;
33 epoch.tm_mday = 1;
34
35 // Alphabetically first. Coincidentally equivalent to UTC.
36 ASSERT_EQ(2678400, mktime_tz(&epoch, "Africa/Abidjan"));
37
38 // Alphabetically last. Coincidentally equivalent to UTC.
39 ASSERT_EQ(2678400, mktime_tz(&epoch, "Zulu"));
40
41 // Somewhere in the middle, not UTC.
42 ASSERT_EQ(2707200, mktime_tz(&epoch, "America/Los_Angeles"));
43
44 // Missing. Falls back to UTC.
45 ASSERT_EQ(2678400, mktime_tz(&epoch, "PST"));
Christopher Ferrisf04935c2013-12-20 18:43:21 -080046#else // __BIONIC__
47 GTEST_LOG_(INFO) << "This test does nothing.\n";
48#endif // __BIONIC__
Elliott Hughese0175ca2013-03-14 14:38:08 -070049}
Elliott Hughesee178bf2013-07-12 11:25:20 -070050
51TEST(time, gmtime) {
52 time_t t = 0;
53 tm* broken_down = gmtime(&t);
54 ASSERT_TRUE(broken_down != NULL);
55 ASSERT_EQ(0, broken_down->tm_sec);
56 ASSERT_EQ(0, broken_down->tm_min);
57 ASSERT_EQ(0, broken_down->tm_hour);
58 ASSERT_EQ(1, broken_down->tm_mday);
59 ASSERT_EQ(0, broken_down->tm_mon);
60 ASSERT_EQ(1970, broken_down->tm_year + 1900);
61}
Elliott Hughes7843d442013-08-22 11:37:32 -070062
Elliott Hughes7843d442013-08-22 11:37:32 -070063TEST(time, mktime_10310929) {
64 struct tm t;
65 memset(&t, 0, sizeof(tm));
66 t.tm_year = 200;
67 t.tm_mon = 2;
68 t.tm_mday = 10;
69
Elliott Hughes0c401522013-10-18 16:21:54 -070070#if !defined(__LP64__)
71 // 32-bit bionic stupidly had a signed 32-bit time_t.
Elliott Hughes7843d442013-08-22 11:37:32 -070072 ASSERT_EQ(-1, mktime(&t));
Christopher Ferrisf04935c2013-12-20 18:43:21 -080073#if defined(__BIONIC__)
Elliott Hughes7843d442013-08-22 11:37:32 -070074 ASSERT_EQ(-1, mktime_tz(&t, "UTC"));
Christopher Ferrisf04935c2013-12-20 18:43:21 -080075#endif
Elliott Hughes0c401522013-10-18 16:21:54 -070076#else
77 // Everyone else should be using a signed 64-bit time_t.
78 ASSERT_GE(sizeof(time_t) * 8, 64U);
79
80 setenv("TZ", "America/Los_Angeles", 1);
81 tzset();
82 ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&t));
Christopher Ferrisf04935c2013-12-20 18:43:21 -080083#if defined(__BIONIC__)
Elliott Hughes0c401522013-10-18 16:21:54 -070084 ASSERT_EQ(static_cast<time_t>(4108320000U), mktime_tz(&t, "UTC"));
Christopher Ferrisf04935c2013-12-20 18:43:21 -080085#endif
Elliott Hughes0c401522013-10-18 16:21:54 -070086
87 setenv("TZ", "UTC", 1);
88 tzset();
89 ASSERT_EQ(static_cast<time_t>(4108320000U), mktime(&t));
Christopher Ferrisf04935c2013-12-20 18:43:21 -080090#if defined(__BIONIC__)
Elliott Hughes0c401522013-10-18 16:21:54 -070091 ASSERT_EQ(static_cast<time_t>(4108348800U), mktime_tz(&t, "America/Los_Angeles"));
92#endif
Elliott Hughes7843d442013-08-22 11:37:32 -070093#endif
Christopher Ferrisf04935c2013-12-20 18:43:21 -080094}