Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / include / timegm.h
1 //  (C) Copyright Howard Hinnant
2 //  (C) Copyright 2010-2011 Vicente J. Botet Escriba
3 //  Use, modification and distribution are subject to the Boost Software License,
4 //  Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 //  http://www.boost.org/LICENSE_1_0.txt).
6
7 //===-------------------------- locale ------------------------------------===//
8 //
9 //                     The LLVM Compiler Infrastructure
10 //
11 // This file is dual licensed under the MIT and the University of Illinois Open
12 // Source Licenses. See LICENSE.TXT for details.
13 //
14 //===----------------------------------------------------------------------===//
15
16 // This code was adapted by Vicente from Howard Hinnant's experimental work
17 // on chrono i/o to Boost and some functions from libc++/locale to emulate the missing time_get::get()
18
19 #ifndef BOOST_CHRONO_IO_TIME_POINT_IO_H
20 #define BOOST_CHRONO_IO_TIME_POINT_IO_H
21
22 #include <time.h>
23
24 static int32_t is_leap(int32_t year) {
25   if(year % 400 == 0)
26     return 1;
27   if(year % 100 == 0)
28     return 0;
29   if(year % 4 == 0)
30     return 1;
31   return 0;
32 }
33
34 static int32_t days_from_0(int32_t year) {
35   year--;
36   return 365 * year + (year / 400) - (year/100) + (year / 4);
37 }
38
39 int32_t static days_from_1970(int32_t year) {
40   static const int days_from_0_to_1970 = days_from_0(1970);
41   return days_from_0(year) - days_from_0_to_1970;
42 }
43
44 static int32_t days_from_1jan(int32_t year,int32_t month,int32_t day) {
45   static const int32_t days[2][12] =
46   {
47     { 0,31,59,90,120,151,181,212,243,273,304,334},
48     { 0,31,60,91,121,152,182,213,244,274,305,335}
49   };
50
51   return days[is_leap(year)][month-1] + day - 1;
52 }
53
54 static  time_t internal_timegm(tm const *t) {
55   int year = t->tm_year + 1900;
56   int month = t->tm_mon;
57   if(month > 11)
58   {
59     year += month/12;
60     month %= 12;
61   }
62   else if(month < 0)
63   {
64     int years_diff = (-month + 11)/12;
65     year -= years_diff;
66     month+=12 * years_diff;
67   }
68   month++;
69   int day = t->tm_mday;
70   int day_of_year = days_from_1jan(year,month,day);
71   int days_since_epoch = days_from_1970(year) + day_of_year ;
72
73   time_t seconds_in_day = 3600 * 24;
74   time_t result = seconds_in_day * days_since_epoch + 3600 * t->tm_hour + 60 * t->tm_min + t->tm_sec;
75
76   return result;
77 }
78
79 #endif