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