1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module glib.TimeVal;
26 
27 private import glib.Str;
28 private import glib.c.functions;
29 public  import glib.c.types;
30 public  import gtkc.glibtypes;
31 private import gtkd.Loader;
32 
33 
34 /**
35  * Represents a precise time, with seconds and microseconds.
36  * Similar to the struct timeval returned by the gettimeofday()
37  * UNIX system call.
38  * 
39  * GLib is attempting to unify around the use of 64bit integers to
40  * represent microsecond-precision time. As such, this type will be
41  * removed from a future version of GLib.
42  */
43 public final class TimeVal
44 {
45 	/** the main Gtk struct */
46 	protected GTimeVal* gTimeVal;
47 	protected bool ownedRef;
48 
49 	/** Get the main Gtk struct */
50 	public GTimeVal* getTimeValStruct(bool transferOwnership = false)
51 	{
52 		if (transferOwnership)
53 			ownedRef = false;
54 		return gTimeVal;
55 	}
56 
57 	/** the main Gtk struct as a void* */
58 	protected void* getStruct()
59 	{
60 		return cast(void*)gTimeVal;
61 	}
62 
63 	/**
64 	 * Sets our main struct and passes it to the parent class.
65 	 */
66 	public this (GTimeVal* gTimeVal, bool ownedRef = false)
67 	{
68 		this.gTimeVal = gTimeVal;
69 		this.ownedRef = ownedRef;
70 	}
71 
72 	~this ()
73 	{
74 		if ( Linker.isLoaded(LIBRARY_GLIB) && ownedRef )
75 			g_free(gTimeVal);
76 	}
77 
78 
79 	/**
80 	 * seconds
81 	 */
82 	public @property glong tvSec()
83 	{
84 		return gTimeVal.tvSec;
85 	}
86 
87 	/** Ditto */
88 	public @property void tvSec(glong value)
89 	{
90 		gTimeVal.tvSec = value;
91 	}
92 
93 	/**
94 	 * microseconds
95 	 */
96 	public @property glong tvUsec()
97 	{
98 		return gTimeVal.tvUsec;
99 	}
100 
101 	/** Ditto */
102 	public @property void tvUsec(glong value)
103 	{
104 		gTimeVal.tvUsec = value;
105 	}
106 
107 	/**
108 	 * Adds the given number of microseconds to @time_. @microseconds can
109 	 * also be negative to decrease the value of @time_.
110 	 *
111 	 * Params:
112 	 *     microseconds = number of microseconds to add to @time
113 	 */
114 	public void add(glong microseconds)
115 	{
116 		g_time_val_add(gTimeVal, microseconds);
117 	}
118 
119 	/**
120 	 * Converts @time_ into an RFC 3339 encoded string, relative to the
121 	 * Coordinated Universal Time (UTC). This is one of the many formats
122 	 * allowed by ISO 8601.
123 	 *
124 	 * ISO 8601 allows a large number of date/time formats, with or without
125 	 * punctuation and optional elements. The format returned by this function
126 	 * is a complete date and time, with optional punctuation included, the
127 	 * UTC time zone represented as "Z", and the @tv_usec part included if
128 	 * and only if it is nonzero, i.e. either
129 	 * "YYYY-MM-DDTHH:MM:SSZ" or "YYYY-MM-DDTHH:MM:SS.fffffZ".
130 	 *
131 	 * This corresponds to the Internet date/time format defined by
132 	 * [RFC 3339](https://www.ietf.org/rfc/rfc3339.txt),
133 	 * and to either of the two most-precise formats defined by
134 	 * the W3C Note
135 	 * [Date and Time Formats](http://www.w3.org/TR/NOTE-datetime-19980827).
136 	 * Both of these documents are profiles of ISO 8601.
137 	 *
138 	 * Use g_date_time_format() or g_strdup_printf() if a different
139 	 * variation of ISO 8601 format is required.
140 	 *
141 	 * If @time_ represents a date which is too large to fit into a `struct tm`,
142 	 * %NULL will be returned. This is platform dependent, but it is safe to assume
143 	 * years up to 3000 are supported. The return value of g_time_val_to_iso8601()
144 	 * has been nullable since GLib 2.54; before then, GLib would crash under the
145 	 * same conditions.
146 	 *
147 	 * Returns: a newly allocated string containing an ISO 8601 date,
148 	 *     or %NULL if @time_ was too large
149 	 *
150 	 * Since: 2.12
151 	 */
152 	public string toIso8601()
153 	{
154 		auto retStr = g_time_val_to_iso8601(gTimeVal);
155 
156 		scope(exit) Str.freeString(retStr);
157 		return Str.toString(retStr);
158 	}
159 
160 	/**
161 	 * Converts a string containing an ISO 8601 encoded date and time
162 	 * to a #GTimeVal and puts it into @time_.
163 	 *
164 	 * @iso_date must include year, month, day, hours, minutes, and
165 	 * seconds. It can optionally include fractions of a second and a time
166 	 * zone indicator. (In the absence of any time zone indication, the
167 	 * timestamp is assumed to be in local time.)
168 	 *
169 	 * Params:
170 	 *     isoDate = an ISO 8601 encoded date string
171 	 *     time = a #GTimeVal
172 	 *
173 	 * Returns: %TRUE if the conversion was successful.
174 	 *
175 	 * Since: 2.12
176 	 */
177 	public static bool fromIso8601(string isoDate, out TimeVal time)
178 	{
179 		GTimeVal* outtime = gMalloc!GTimeVal();
180 
181 		auto p = g_time_val_from_iso8601(Str.toStringz(isoDate), outtime) != 0;
182 
183 		time = new TimeVal(outtime, true);
184 
185 		return p;
186 	}
187 
188 	/**
189 	 * Equivalent to the UNIX gettimeofday() function, but portable.
190 	 *
191 	 * You may find g_get_real_time() to be more convenient.
192 	 *
193 	 * Params:
194 	 *     result = #GTimeVal structure in which to store current time.
195 	 */
196 	public static void getCurrentTime(TimeVal result)
197 	{
198 		g_get_current_time((result is null) ? null : result.getTimeValStruct());
199 	}
200 
201 	/**
202 	 * Queries the system monotonic time.
203 	 *
204 	 * The monotonic clock will always increase and doesn't suffer
205 	 * discontinuities when the user (or NTP) changes the system time.  It
206 	 * may or may not continue to tick during times where the machine is
207 	 * suspended.
208 	 *
209 	 * We try to use the clock that corresponds as closely as possible to
210 	 * the passage of time as measured by system calls such as poll() but it
211 	 * may not always be possible to do this.
212 	 *
213 	 * Returns: the monotonic time, in microseconds
214 	 *
215 	 * Since: 2.28
216 	 */
217 	public static long getMonotonicTime()
218 	{
219 		return g_get_monotonic_time();
220 	}
221 
222 	/**
223 	 * Queries the system wall-clock time.
224 	 *
225 	 * This call is functionally equivalent to g_get_current_time() except
226 	 * that the return value is often more convenient than dealing with a
227 	 * #GTimeVal.
228 	 *
229 	 * You should only use this call if you are actually interested in the real
230 	 * wall-clock time.  g_get_monotonic_time() is probably more useful for
231 	 * measuring intervals.
232 	 *
233 	 * Returns: the number of microseconds since January 1, 1970 UTC.
234 	 *
235 	 * Since: 2.28
236 	 */
237 	public static long getRealTime()
238 	{
239 		return g_get_real_time();
240 	}
241 
242 	/**
243 	 * Pauses the current thread for the given number of microseconds.
244 	 *
245 	 * There are 1 million microseconds per second (represented by the
246 	 * #G_USEC_PER_SEC macro). g_usleep() may have limited precision,
247 	 * depending on hardware and operating system; don't rely on the exact
248 	 * length of the sleep.
249 	 *
250 	 * Params:
251 	 *     microseconds = number of microseconds to pause
252 	 */
253 	public static void usleep(gulong microseconds)
254 	{
255 		g_usleep(microseconds);
256 	}
257 }