blob: a672dc8da12895093796ec7de5c2798435bda4af [file] [log] [blame]
Vishal Bhoj82c80712015-12-15 21:13:33 +05301/** @file
2 timegm implementation
3
4 Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available under
6 the terms and conditions of the BSD License that accompanies this distribution.
7 The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 * Copyright (c) 1987, 1989 Regents of the University of California.
14 * All rights reserved.
15 *
16 * This code is derived from software contributed to Berkeley by
17 * Arthur David Olson of the National Cancer Institute.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 * 3. All advertising materials mentioning features or use of this software
28 * must display the following acknowledgement:
29 * This product includes software developed by the University of
30 * California, Berkeley and its contributors.
31 * 4. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46
47static char *sccsid = "from: @(#)ctime.c 5.26 (Berkeley) 2/23/91";
48
49
50 * This implementation of mktime is lifted straight from the NetBSD (BSD 4.4)
51 * version. I modified it slightly to divorce it from the internals of the
52 * ctime library. Thus this version can't use details of the internal
53 * timezone state file to figure out strange unnormalized struct tm values,
54 * as might result from someone doing date math on the tm struct then passing
55 * it to mktime.
56 *
57 * It just does as well as it can at normalizing the tm input, then does a
58 * binary search of the time space using the system's localtime() function.
59 *
60 * The original binary search was defective in that it didn't consider the
61 * setting of tm_isdst when comparing tm values, causing the search to be
62 * flubbed for times near the dst/standard time changeover. The original
63 * code seems to make up for this by grubbing through the timezone info
64 * whenever the binary search barfed. Since I don't have that luxury in
65 * portable code, I have to take care of tm_isdst in the comparison routine.
66 * This requires knowing how many minutes offset dst is from standard time.
67 *
68 * So, if you live somewhere in the world where dst is not 60 minutes offset,
69 * and your vendor doesn't supply mktime(), you'll have to edit this variable
70 * by hand. Sorry about that.
71
72 $NetBSD: mktime.c,v 1.4 2006/06/11 19:34:10 kardel Exp $
73**/
74
75#include <LibConfig.h>
76#include <time.h>
77
78/*
79 This funciton is in Time.c, which has a different license than timegm.
80*/
81time_t
82time2(struct tm * const tmp, void (* const funcp)(const time_t*, long, struct tm*),
83 const long offset, int * const okayp);
84
85/*
86 This funciton is in Time.c, which has a different license than timegm.
87*/
88void
89gmtsub(
90 const time_t * const timep,
91 const long offset,
92 struct tm * const tmp
93 );
94
95#ifndef WRONG
96#define WRONG (-1)
97#endif /* !defined WRONG */
98
99/*
100 Convert a tm structure to a GMT based time_t.
101*/
102time_t timegm( struct tm * tmp )
103{
104 register time_t t;
105 int okay;
106
107 tmp->tm_isdst = 0;
108 t = time2(tmp, gmtsub, 0, &okay);
109 if (okay || tmp->tm_isdst < 0)
110 return t;
111
112 return WRONG;
113}