track total size of static array and Unit/Class/Func
[hiphop-php.git] / hphp / runtime / base / timezone.h
blob3db052a46fbc1b197efdaff1aa9db62b6360dbc4
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_TIMEZONE_H_
18 #define incl_HPHP_TIMEZONE_H_
20 #include "hphp/runtime/base/resource-data.h"
21 #include "hphp/runtime/base/type-string.h"
22 #include "hphp/runtime/ext/std/ext_std_misc.h"
24 #include <map>
25 #include <memory>
27 extern "C" {
28 #include <timelib.h>
31 namespace HPHP {
33 struct Array;
35 ///////////////////////////////////////////////////////////////////////////////
37 /**
38 * Handles all timezone related functions.
40 struct TimeZone : SweepableResourceData {
41 DECLARE_RESOURCE_ALLOCATION(TimeZone);
43 /**
44 * Get/set current timezone that controls how local time is interpreted.
46 static String CurrentName(); // current timezone's name
47 static req::ptr<TimeZone> Current(); // current timezone
48 static bool SetCurrent(const char* name); // returns false if invalid
50 /**
51 * TimeZone database queries.
53 static bool IsValid(const char* name);
54 static Array GetAbbreviations();
55 static String AbbreviationToName(String abbr, int utcoffset = -1,
56 int isdst = 1);
58 public:
59 /**
60 * Constructing a timezone object by name or a raw pointer (internal).
62 TimeZone();
63 explicit TimeZone(const String& name);
64 explicit TimeZone(timelib_tzinfo *tzi);
66 static StaticString& classnameof() {
67 static StaticString result("TimeZone");
68 return result;
70 // overriding ResourceData
71 const String& o_getClassNameHook() const override { return classnameof(); }
73 /**
74 * Whether this represents a valid timezone.
76 bool isValid() const {
77 switch (m_tztype) {
78 case 0:
79 return false;
80 case TIMELIB_ZONETYPE_ID:
81 return getTZInfo();
82 case TIMELIB_ZONETYPE_OFFSET:
83 case TIMELIB_ZONETYPE_ABBR:
84 return true;
86 always_assert(false && "invalid tztype");
89 /**
90 * Get timezone's name or abbreviation.
92 String name() const;
93 String abbr(int type = 0) const;
94 int type() const;
96 /**
97 * Get offset from UTC at the specified timestamp under this timezone.
99 int offset(int64_t timestamp) const;
102 * Test whether it was running under DST at specified timestamp.
104 bool dst(int64_t timestamp) const;
107 * Query transition times for DST.
109 Array transitions(int64_t timestamp_begin = k_PHP_INT_MIN,
110 int64_t timestamp_end = k_PHP_INT_MAX) const;
113 * Get information about a timezone
115 Array getLocation() const;
118 * Timezone Database version
120 static String getVersion() {
121 const timelib_tzdb* db = GetDatabase();
122 return String(db->version, CopyString);
126 * Make a copy of this timezone object, so it can be changed independently.
128 req::ptr<TimeZone> cloneTimeZone() const;
130 protected:
131 friend struct DateTime;
132 friend struct TimeStamp;
133 friend struct DateInterval;
136 * Returns raw pointer. For internal use only.
138 * If type() !== TIMELIB_ZONETYPE_ID, this will definitely return nullptr,
139 * even if isValid().
141 timelib_tzinfo *getTZInfo() const { return m_tzi; }
143 private:
144 static const timelib_tzdb *GetDatabase();
147 * Look up cache and if found return it, otherwise, read it from database.
149 static timelib_tzinfo* GetTimeZoneInfoRaw(char* name, const timelib_tzdb* db);
151 unsigned int m_tztype = 0;
152 timelib_tzinfo* m_tzi = nullptr;
153 int m_offset;
154 int m_dst;
155 String m_abbr;
158 ///////////////////////////////////////////////////////////////////////////////
160 void timezone_init();
161 const timelib_tzdb* timezone_get_tzdb();
162 extern const timelib_tzdb* (*timezone_raw_get_tzdb)();
164 ///////////////////////////////////////////////////////////////////////////////
167 #endif // incl_HPHP_TIMEZONE_H_