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