blob: fb5a4a71f006fbf23a441cf597fcba754c383121 [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
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
Elliott Hughesee178bf2013-07-12 11:25:20 -070045
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}
Elliott Hughes7843d442013-08-22 11:37:32 -070057
Elliott Hughes0c401522013-10-18 16:21:54 -070058#if __BIONIC__
Elliott Hughes7843d442013-08-22 11:37:32 -070059TEST(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
Elliott Hughes0c401522013-10-18 16:21:54 -070066#if !defined(__LP64__)
67 // 32-bit bionic stupidly had a signed 32-bit time_t.
Elliott Hughes7843d442013-08-22 11:37:32 -070068 ASSERT_EQ(-1, mktime(&t));
69 ASSERT_EQ(-1, mktime_tz(&t, "UTC"));
Elliott Hughes0c401522013-10-18 16:21:54 -070070#else
71 // Everyone else should be using a signed 64-bit time_t.
72 ASSERT_GE(sizeof(time_t) * 8, 64U);
73
74 setenv("TZ", "America/Los_Angeles", 1);
75 tzset();
76 ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&t));
77 ASSERT_EQ(static_cast<time_t>(4108320000U), mktime_tz(&t, "UTC"));
78
79 setenv("TZ", "UTC", 1);
80 tzset();
81 ASSERT_EQ(static_cast<time_t>(4108320000U), mktime(&t));
82 ASSERT_EQ(static_cast<time_t>(4108348800U), mktime_tz(&t, "America/Los_Angeles"));
83#endif
Elliott Hughes7843d442013-08-22 11:37:32 -070084}
85#endif