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 gstreamer.DateTime;
26 
27 private import glib.ConstructionException;
28 private import glib.DateTime : GLibDateTime = DateTime;
29 private import glib.Str;
30 private import gobject.ObjectG;
31 private import gstreamerc.gstreamer;
32 public  import gstreamerc.gstreamertypes;
33 private import gtkd.Loader;
34 
35 
36 /**
37  * Struct to store date, time and timezone information altogether.
38  * #GstDateTime is refcounted and immutable.
39  * 
40  * Date information is handled using the proleptic Gregorian calendar.
41  * 
42  * Provides basic creation functions and accessor functions to its fields.
43  */
44 public class DateTime
45 {
46 	/** the main Gtk struct */
47 	protected GstDateTime* gstDateTime;
48 	protected bool ownedRef;
49 
50 	/** Get the main Gtk struct */
51 	public GstDateTime* getDateTimeStruct(bool transferOwnership = false)
52 	{
53 		if (transferOwnership)
54 			ownedRef = false;
55 		return gstDateTime;
56 	}
57 
58 	/** the main Gtk struct as a void* */
59 	protected void* getStruct()
60 	{
61 		return cast(void*)gstDateTime;
62 	}
63 
64 	/**
65 	 * Sets our main struct and passes it to the parent class.
66 	 */
67 	public this (GstDateTime* gstDateTime, bool ownedRef = false)
68 	{
69 		this.gstDateTime = gstDateTime;
70 		this.ownedRef = ownedRef;
71 	}
72 
73 	~this ()
74 	{
75 		if (  Linker.isLoaded(LIBRARY_GSTREAMER) && ownedRef )
76 			gst_date_time_unref(gstDateTime);
77 	}
78 
79 	/**
80 	 * Creates a new GstDateTime representing the current date and time.
81 	 *
82 	 * Params:
83 	 *     utc  = If true use utc else use the local timezone.
84 	 * Throws: ConstructionException GTK+ fails to create the object.
85 	 */
86 	public this (bool utc)
87 	{
88 		GstDateTime* p;
89 		
90 		if ( utc )
91 		{
92 			p = gst_date_time_new_now_utc();
93 		}
94 		else
95 		{
96 			p = gst_date_time_new_now_local_time();
97 		}
98 		
99 		if(p is null)
100 		{
101 			throw new ConstructionException("null returned by gst_date_time_new_now_local_time()");
102 		}
103 		this(p); //, true);
104 	}
105 	
106 	/**
107 	 * Creates a new GstDateTime using the time since Jan 1, 1970 specified by
108 	 * secs.
109 	 *
110 	 * Params:
111 	 *     secs = Seconds from the Unix epoch
112 	 *     utc  = If true use utc else use the local timezone.
113 	 * Throws: ConstructionException GTK+ fails to create the object.
114 	 */
115 	public this (long secs, bool utc)
116 	{
117 		GstDateTime* p;
118 		
119 		if ( utc )
120 		{
121 			p = gst_date_time_new_from_unix_epoch_utc(secs);
122 		}
123 		else
124 		{
125 			p = gst_date_time_new_from_unix_epoch_local_time(secs);
126 		}
127 		
128 		if(p is null)
129 		{
130 			throw new ConstructionException("null returned by gst_date_time_new_from_unix_epoch_local_time(secs)");
131 		}
132 		this(p); //, true);
133 	}
134 
135 	/**
136 	 */
137 
138 	/** */
139 	public static GType getType()
140 	{
141 		return gst_date_time_get_type();
142 	}
143 
144 	/**
145 	 * Creates a new #GstDateTime using the date and times in the gregorian calendar
146 	 * in the supplied timezone.
147 	 *
148 	 * @year should be from 1 to 9999, @month should be from 1 to 12, @day from
149 	 * 1 to 31, @hour from 0 to 23, @minutes and @seconds from 0 to 59.
150 	 *
151 	 * Note that @tzoffset is a float and was chosen so for being able to handle
152 	 * some fractional timezones, while it still keeps the readability of
153 	 * representing it in hours for most timezones.
154 	 *
155 	 * If value is -1 then all over value will be ignored. For example
156 	 * if @month == -1, then #GstDateTime will created only for @year. If
157 	 * @day == -1, then #GstDateTime will created for @year and @month and
158 	 * so on.
159 	 *
160 	 * Free-function: gst_date_time_unref
161 	 *
162 	 * Params:
163 	 *     tzoffset = Offset from UTC in hours.
164 	 *     year = the gregorian year
165 	 *     month = the gregorian month
166 	 *     day = the day of the gregorian month
167 	 *     hour = the hour of the day
168 	 *     minute = the minute of the hour
169 	 *     seconds = the second of the minute
170 	 *
171 	 * Returns: the newly created #GstDateTime
172 	 *
173 	 * Throws: ConstructionException GTK+ fails to create the object.
174 	 */
175 	public this(float tzoffset, int year, int month, int day, int hour, int minute, double seconds)
176 	{
177 		auto p = gst_date_time_new(tzoffset, year, month, day, hour, minute, seconds);
178 		
179 		if(p is null)
180 		{
181 			throw new ConstructionException("null returned by new");
182 		}
183 		
184 		this(cast(GstDateTime*) p);
185 	}
186 
187 	/**
188 	 * Creates a new #GstDateTime from a #GDateTime object.
189 	 *
190 	 * Free-function: gst_date_time_unref
191 	 *
192 	 * Params:
193 	 *     dt = the #GDateTime. The new #GstDateTime takes ownership.
194 	 *
195 	 * Returns: a newly created #GstDateTime,
196 	 *     or %NULL on error
197 	 *
198 	 * Throws: ConstructionException GTK+ fails to create the object.
199 	 */
200 	public this(GLibDateTime dt)
201 	{
202 		auto p = gst_date_time_new_from_g_date_time((dt is null) ? null : dt.getDateTimeStruct(true));
203 		
204 		if(p is null)
205 		{
206 			throw new ConstructionException("null returned by new_from_g_date_time");
207 		}
208 		
209 		this(cast(GstDateTime*) p);
210 	}
211 
212 	/**
213 	 * Tries to parse common variants of ISO-8601 datetime strings into a
214 	 * #GstDateTime. Possible input formats are (for example):
215 	 * 2012-06-30T22:46:43Z, 2012, 2012-06, 2012-06-30, 2012-06-30T22:46:43-0430,
216 	 * 2012-06-30T22:46Z, 2012-06-30T22:46-0430, 2012-06-30 22:46,
217 	 * 2012-06-30 22:46:43, 2012-06-00, 2012-00-00, 2012-00-30, 22:46:43Z, 22:46Z,
218 	 * 22:46:43-0430, 22:46-0430, 22:46:30, 22:46
219 	 * If no date is provided, it is assumed to be "today" in the timezone
220 	 * provided (if any), otherwise UTC.
221 	 *
222 	 * Free-function: gst_date_time_unref
223 	 *
224 	 * Params:
225 	 *     str = ISO 8601-formatted datetime string.
226 	 *
227 	 * Returns: a newly created #GstDateTime,
228 	 *     or %NULL on error
229 	 *
230 	 * Throws: ConstructionException GTK+ fails to create the object.
231 	 */
232 	public this(string str)
233 	{
234 		auto p = gst_date_time_new_from_iso8601_string(Str.toStringz(str));
235 		
236 		if(p is null)
237 		{
238 			throw new ConstructionException("null returned by new_from_iso8601_string");
239 		}
240 		
241 		this(cast(GstDateTime*) p);
242 	}
243 
244 	/**
245 	 * Creates a new #GstDateTime using the date and times in the gregorian calendar
246 	 * in the local timezone.
247 	 *
248 	 * @year should be from 1 to 9999, @month should be from 1 to 12, @day from
249 	 * 1 to 31, @hour from 0 to 23, @minutes and @seconds from 0 to 59.
250 	 *
251 	 * If @month is -1, then the #GstDateTime created will only contain @year,
252 	 * and all other fields will be considered not set.
253 	 *
254 	 * If @day is -1, then the #GstDateTime created will only contain @year and
255 	 * @month and all other fields will be considered not set.
256 	 *
257 	 * If @hour is -1, then the #GstDateTime created will only contain @year and
258 	 * @month and @day, and the time fields will be considered not set. In this
259 	 * case @minute and @seconds should also be -1.
260 	 *
261 	 * Free-function: gst_date_time_unref
262 	 *
263 	 * Params:
264 	 *     year = the gregorian year
265 	 *     month = the gregorian month, or -1
266 	 *     day = the day of the gregorian month, or -1
267 	 *     hour = the hour of the day, or -1
268 	 *     minute = the minute of the hour, or -1
269 	 *     seconds = the second of the minute, or -1
270 	 *
271 	 * Returns: the newly created #GstDateTime
272 	 *
273 	 * Throws: ConstructionException GTK+ fails to create the object.
274 	 */
275 	public this(int year, int month, int day, int hour, int minute, double seconds)
276 	{
277 		auto p = gst_date_time_new_local_time(year, month, day, hour, minute, seconds);
278 		
279 		if(p is null)
280 		{
281 			throw new ConstructionException("null returned by new_local_time");
282 		}
283 		
284 		this(cast(GstDateTime*) p);
285 	}
286 
287 	/**
288 	 * Creates a new #GstDateTime using the date and times in the gregorian calendar
289 	 * in the local timezone.
290 	 *
291 	 * @year should be from 1 to 9999.
292 	 *
293 	 * Free-function: gst_date_time_unref
294 	 *
295 	 * Params:
296 	 *     year = the gregorian year
297 	 *
298 	 * Returns: the newly created #GstDateTime
299 	 *
300 	 * Throws: ConstructionException GTK+ fails to create the object.
301 	 */
302 	public this(int year)
303 	{
304 		auto p = gst_date_time_new_y(year);
305 		
306 		if(p is null)
307 		{
308 			throw new ConstructionException("null returned by new_y");
309 		}
310 		
311 		this(cast(GstDateTime*) p);
312 	}
313 
314 	/**
315 	 * Creates a new #GstDateTime using the date and times in the gregorian calendar
316 	 * in the local timezone.
317 	 *
318 	 * @year should be from 1 to 9999, @month should be from 1 to 12.
319 	 *
320 	 * If value is -1 then all over value will be ignored. For example
321 	 * if @month == -1, then #GstDateTime will created only for @year.
322 	 *
323 	 * Free-function: gst_date_time_unref
324 	 *
325 	 * Params:
326 	 *     year = the gregorian year
327 	 *     month = the gregorian month
328 	 *
329 	 * Returns: the newly created #GstDateTime
330 	 *
331 	 * Throws: ConstructionException GTK+ fails to create the object.
332 	 */
333 	public this(int year, int month)
334 	{
335 		auto p = gst_date_time_new_ym(year, month);
336 		
337 		if(p is null)
338 		{
339 			throw new ConstructionException("null returned by new_ym");
340 		}
341 		
342 		this(cast(GstDateTime*) p);
343 	}
344 
345 	/**
346 	 * Creates a new #GstDateTime using the date and times in the gregorian calendar
347 	 * in the local timezone.
348 	 *
349 	 * @year should be from 1 to 9999, @month should be from 1 to 12, @day from
350 	 * 1 to 31.
351 	 *
352 	 * If value is -1 then all over value will be ignored. For example
353 	 * if @month == -1, then #GstDateTime will created only for @year. If
354 	 * @day == -1, then #GstDateTime will created for @year and @month and
355 	 * so on.
356 	 *
357 	 * Free-function: gst_date_time_unref
358 	 *
359 	 * Params:
360 	 *     year = the gregorian year
361 	 *     month = the gregorian month
362 	 *     day = the day of the gregorian month
363 	 *
364 	 * Returns: the newly created #GstDateTime
365 	 *
366 	 * Throws: ConstructionException GTK+ fails to create the object.
367 	 */
368 	public this(int year, int month, int day)
369 	{
370 		auto p = gst_date_time_new_ymd(year, month, day);
371 		
372 		if(p is null)
373 		{
374 			throw new ConstructionException("null returned by new_ymd");
375 		}
376 		
377 		this(cast(GstDateTime*) p);
378 	}
379 
380 	/**
381 	 * Returns the day of the month of this #GstDateTime.
382 	 * Call gst_date_time_has_day before, to avoid warnings.
383 	 *
384 	 * Returns: The day of this #GstDateTime
385 	 */
386 	public int getDay()
387 	{
388 		return gst_date_time_get_day(gstDateTime);
389 	}
390 
391 	/**
392 	 * Retrieves the hour of the day represented by @datetime in the gregorian
393 	 * calendar. The return is in the range of 0 to 23.
394 	 * Call gst_date_time_has_haur before, to avoid warnings.
395 	 *
396 	 * Returns: the hour of the day
397 	 */
398 	public int getHour()
399 	{
400 		return gst_date_time_get_hour(gstDateTime);
401 	}
402 
403 	/**
404 	 * Retrieves the fractional part of the seconds in microseconds represented by
405 	 * @datetime in the gregorian calendar.
406 	 *
407 	 * Returns: the microsecond of the second
408 	 */
409 	public int getMicrosecond()
410 	{
411 		return gst_date_time_get_microsecond(gstDateTime);
412 	}
413 
414 	/**
415 	 * Retrieves the minute of the hour represented by @datetime in the gregorian
416 	 * calendar.
417 	 * Call gst_date_time_has_minute before, to avoid warnings.
418 	 *
419 	 * Returns: the minute of the hour
420 	 */
421 	public int getMinute()
422 	{
423 		return gst_date_time_get_minute(gstDateTime);
424 	}
425 
426 	/**
427 	 * Returns the month of this #GstDateTime. January is 1, February is 2, etc..
428 	 * Call gst_date_time_has_month before, to avoid warnings.
429 	 *
430 	 * Returns: The month of this #GstDateTime
431 	 */
432 	public int getMonth()
433 	{
434 		return gst_date_time_get_month(gstDateTime);
435 	}
436 
437 	/**
438 	 * Retrieves the second of the minute represented by @datetime in the gregorian
439 	 * calendar.
440 	 * Call gst_date_time_has_second before, to avoid warnings.
441 	 *
442 	 * Returns: the second represented by @datetime
443 	 */
444 	public int getSecond()
445 	{
446 		return gst_date_time_get_second(gstDateTime);
447 	}
448 
449 	/**
450 	 * Retrieves the offset from UTC in hours that the timezone specified
451 	 * by @datetime represents. Timezones ahead (to the east) of UTC have positive
452 	 * values, timezones before (to the west) of UTC have negative values.
453 	 * If @datetime represents UTC time, then the offset is zero.
454 	 *
455 	 * Returns: the offset from UTC in hours
456 	 */
457 	public float getTimeZoneOffset()
458 	{
459 		return gst_date_time_get_time_zone_offset(gstDateTime);
460 	}
461 
462 	/**
463 	 * Returns the year of this #GstDateTime
464 	 * Call gst_date_time_has_year before, to avoid warnings.
465 	 *
466 	 * Returns: The year of this #GstDateTime
467 	 */
468 	public int getYear()
469 	{
470 		return gst_date_time_get_year(gstDateTime);
471 	}
472 
473 	/**
474 	 * Returns: %TRUE if @datetime<!-- -->'s day field is set, otherwise %FALSE
475 	 */
476 	public bool hasDay()
477 	{
478 		return gst_date_time_has_day(gstDateTime) != 0;
479 	}
480 
481 	/**
482 	 * Returns: %TRUE if @datetime<!-- -->'s month field is set, otherwise %FALSE
483 	 */
484 	public bool hasMonth()
485 	{
486 		return gst_date_time_has_month(gstDateTime) != 0;
487 	}
488 
489 	/**
490 	 * Returns: %TRUE if @datetime<!-- -->'s second field is set, otherwise %FALSE
491 	 */
492 	public bool hasSecond()
493 	{
494 		return gst_date_time_has_second(gstDateTime) != 0;
495 	}
496 
497 	/**
498 	 * Returns: %TRUE if @datetime<!-- -->'s hour and minute fields are set,
499 	 *     otherwise %FALSE
500 	 */
501 	public bool hasTime()
502 	{
503 		return gst_date_time_has_time(gstDateTime) != 0;
504 	}
505 
506 	/**
507 	 * Returns: %TRUE if @datetime<!-- -->'s year field is set (which should always
508 	 *     be the case), otherwise %FALSE
509 	 */
510 	public bool hasYear()
511 	{
512 		return gst_date_time_has_year(gstDateTime) != 0;
513 	}
514 
515 	/**
516 	 * Atomically increments the reference count of @datetime by one.
517 	 *
518 	 * Returns: the reference @datetime
519 	 */
520 	public DateTime doref()
521 	{
522 		auto p = gst_date_time_ref(gstDateTime);
523 		
524 		if(p is null)
525 		{
526 			return null;
527 		}
528 		
529 		return ObjectG.getDObject!(DateTime)(cast(GstDateTime*) p, true);
530 	}
531 
532 	/**
533 	 * Creates a new #GDateTime from a fully defined #GstDateTime object.
534 	 *
535 	 * Free-function: g_date_time_unref
536 	 *
537 	 * Returns: a newly created #GDateTime, or
538 	 *     %NULL on error
539 	 */
540 	public GLibDateTime toGDateTime()
541 	{
542 		auto p = gst_date_time_to_g_date_time(gstDateTime);
543 		
544 		if(p is null)
545 		{
546 			return null;
547 		}
548 		
549 		return new GLibDateTime(cast(GDateTime*) p, true);
550 	}
551 
552 	/**
553 	 * Create a minimal string compatible with ISO-8601. Possible output formats
554 	 * are (for example): 2012, 2012-06, 2012-06-23, 2012-06-23T23:30Z,
555 	 * 2012-06-23T23:30+0100, 2012-06-23T23:30:59Z, 2012-06-23T23:30:59+0100
556 	 *
557 	 * Returns: a newly allocated string formatted according
558 	 *     to ISO 8601 and only including the datetime fields that are
559 	 *     valid, or %NULL in case there was an error. The string should
560 	 *     be freed with g_free().
561 	 */
562 	public string toIso8601String()
563 	{
564 		auto retStr = gst_date_time_to_iso8601_string(gstDateTime);
565 		
566 		scope(exit) Str.freeString(retStr);
567 		return Str.toString(retStr);
568 	}
569 
570 	/**
571 	 * Atomically decrements the reference count of @datetime by one.  When the
572 	 * reference count reaches zero, the structure is freed.
573 	 */
574 	public void unref()
575 	{
576 		gst_date_time_unref(gstDateTime);
577 	}
578 }