1 module soup.Date;
2 
3 private import glib.ConstructionException;
4 private import glib.MemorySlice;
5 private import glib.Str;
6 private import glib.TimeVal;
7 private import gobject.ObjectG;
8 private import soup.c.functions;
9 public  import soup.c.types;
10 
11 
12 /**
13  * A date and time. The date is assumed to be in the (proleptic)
14  * Gregorian calendar. The time is in UTC if @utc is %TRUE. Otherwise,
15  * the time is a local time, and @offset gives the offset from UTC in
16  * minutes (such that adding @offset to the time would give the
17  * correct UTC time). If @utc is %FALSE and @offset is 0, then the
18  * %SoupDate represents a "floating" time with no associated timezone
19  * information.
20  */
21 public final class Date
22 {
23 	/** the main Gtk struct */
24 	protected SoupDate* soupDate;
25 	protected bool ownedRef;
26 
27 	/** Get the main Gtk struct */
28 	public SoupDate* getDateStruct(bool transferOwnership = false)
29 	{
30 		if (transferOwnership)
31 			ownedRef = false;
32 		return soupDate;
33 	}
34 
35 	/** the main Gtk struct as a void* */
36 	protected void* getStruct()
37 	{
38 		return cast(void*)soupDate;
39 	}
40 
41 	/**
42 	 * Sets our main struct and passes it to the parent class.
43 	 */
44 	public this (SoupDate* soupDate, bool ownedRef = false)
45 	{
46 		this.soupDate = soupDate;
47 		this.ownedRef = ownedRef;
48 	}
49 
50 	~this ()
51 	{
52 		if ( ownedRef )
53 			soup_date_free(soupDate);
54 	}
55 
56 
57 	/**
58 	 * the year, 1 to 9999
59 	 */
60 	public @property int year()
61 	{
62 		return soupDate.year;
63 	}
64 
65 	/** Ditto */
66 	public @property void year(int value)
67 	{
68 		soupDate.year = value;
69 	}
70 
71 	/**
72 	 * the month, 1 to 12
73 	 */
74 	public @property int month()
75 	{
76 		return soupDate.month;
77 	}
78 
79 	/** Ditto */
80 	public @property void month(int value)
81 	{
82 		soupDate.month = value;
83 	}
84 
85 	/**
86 	 * day of the month, 1 to 31
87 	 */
88 	public @property int day()
89 	{
90 		return soupDate.day;
91 	}
92 
93 	/** Ditto */
94 	public @property void day(int value)
95 	{
96 		soupDate.day = value;
97 	}
98 
99 	/**
100 	 * hour of the day, 0 to 23
101 	 */
102 	public @property int hour()
103 	{
104 		return soupDate.hour;
105 	}
106 
107 	/** Ditto */
108 	public @property void hour(int value)
109 	{
110 		soupDate.hour = value;
111 	}
112 
113 	/**
114 	 * minute, 0 to 59
115 	 */
116 	public @property int minute()
117 	{
118 		return soupDate.minute;
119 	}
120 
121 	/** Ditto */
122 	public @property void minute(int value)
123 	{
124 		soupDate.minute = value;
125 	}
126 
127 	/**
128 	 * second, 0 to 59 (or up to 61 in the case of leap seconds)
129 	 */
130 	public @property int second()
131 	{
132 		return soupDate.second;
133 	}
134 
135 	/** Ditto */
136 	public @property void second(int value)
137 	{
138 		soupDate.second = value;
139 	}
140 
141 	/**
142 	 * %TRUE if the date is in UTC
143 	 */
144 	public @property bool utc()
145 	{
146 		return soupDate.utc != 0;
147 	}
148 
149 	/** Ditto */
150 	public @property void utc(bool value)
151 	{
152 		soupDate.utc = value;
153 	}
154 
155 	/**
156 	 * offset from UTC
157 	 */
158 	public @property int offset()
159 	{
160 		return soupDate.offset;
161 	}
162 
163 	/** Ditto */
164 	public @property void offset(int value)
165 	{
166 		soupDate.offset = value;
167 	}
168 
169 	/** */
170 	public static GType getType()
171 	{
172 		return soup_date_get_type();
173 	}
174 
175 	/**
176 	 * Creates a #SoupDate representing the indicated time, UTC.
177 	 *
178 	 * Params:
179 	 *     year = the year (1-9999)
180 	 *     month = the month (1-12)
181 	 *     day = the day of the month (1-31, as appropriate for @month)
182 	 *     hour = the hour (0-23)
183 	 *     minute = the minute (0-59)
184 	 *     second = the second (0-59, or up to 61 for leap seconds)
185 	 *
186 	 * Returns: a new #SoupDate
187 	 *
188 	 * Throws: ConstructionException GTK+ fails to create the object.
189 	 */
190 	public this(int year, int month, int day, int hour, int minute, int second)
191 	{
192 		auto __p = soup_date_new(year, month, day, hour, minute, second);
193 
194 		if(__p is null)
195 		{
196 			throw new ConstructionException("null returned by new");
197 		}
198 
199 		this(cast(SoupDate*) __p);
200 	}
201 
202 	/**
203 	 * Creates a #SoupDate representing a time @offset_seconds after the
204 	 * current time (or before it, if @offset_seconds is negative). If
205 	 * offset_seconds is 0, returns the current time.
206 	 *
207 	 * If @offset_seconds would indicate a time not expressible as a
208 	 * <type>time_t</type>, the return value will be clamped into range.
209 	 *
210 	 * Params:
211 	 *     offsetSeconds = offset from current time
212 	 *
213 	 * Returns: a new #SoupDate
214 	 *
215 	 * Throws: ConstructionException GTK+ fails to create the object.
216 	 */
217 	public this(int offsetSeconds)
218 	{
219 		auto __p = soup_date_new_from_now(offsetSeconds);
220 
221 		if(__p is null)
222 		{
223 			throw new ConstructionException("null returned by new_from_now");
224 		}
225 
226 		this(cast(SoupDate*) __p);
227 	}
228 
229 	/**
230 	 * Parses @date_string and tries to extract a date from it. This
231 	 * recognizes all of the "HTTP-date" formats from RFC 2616, all ISO
232 	 * 8601 formats containing both a time and a date, RFC 2822 dates,
233 	 * and reasonable approximations thereof. (Eg, it is lenient about
234 	 * whitespace, leading "0"s, etc.)
235 	 *
236 	 * Params:
237 	 *     dateString = the date in some plausible format
238 	 *
239 	 * Returns: a new #SoupDate, or %NULL if @date_string
240 	 *     could not be parsed.
241 	 *
242 	 * Throws: ConstructionException GTK+ fails to create the object.
243 	 */
244 	public this(string dateString)
245 	{
246 		auto __p = soup_date_new_from_string(Str.toStringz(dateString));
247 
248 		if(__p is null)
249 		{
250 			throw new ConstructionException("null returned by new_from_string");
251 		}
252 
253 		this(cast(SoupDate*) __p);
254 	}
255 
256 	/**
257 	 * Creates a #SoupDate corresponding to @when
258 	 *
259 	 * Params:
260 	 *     when = a <type>time_t</type>
261 	 *
262 	 * Returns: a new #SoupDate
263 	 *
264 	 * Throws: ConstructionException GTK+ fails to create the object.
265 	 */
266 	public this(uint when)
267 	{
268 		auto __p = soup_date_new_from_time_t(when);
269 
270 		if(__p is null)
271 		{
272 			throw new ConstructionException("null returned by new_from_time_t");
273 		}
274 
275 		this(cast(SoupDate*) __p);
276 	}
277 
278 	/**
279 	 * Copies @date.
280 	 *
281 	 * Since: 2.24
282 	 */
283 	public Date copy()
284 	{
285 		auto __p = soup_date_copy(soupDate);
286 
287 		if(__p is null)
288 		{
289 			return null;
290 		}
291 
292 		return ObjectG.getDObject!(Date)(cast(SoupDate*) __p, true);
293 	}
294 
295 	/**
296 	 * Frees @date.
297 	 *
298 	 * Since: 2.24
299 	 */
300 	public void free()
301 	{
302 		soup_date_free(soupDate);
303 		ownedRef = false;
304 	}
305 
306 	/**
307 	 * Gets @date's day.
308 	 *
309 	 * Returns: @date's day
310 	 *
311 	 * Since: 2.32
312 	 */
313 	public int getDay()
314 	{
315 		return soup_date_get_day(soupDate);
316 	}
317 
318 	/**
319 	 * Gets @date's hour.
320 	 *
321 	 * Returns: @date's hour
322 	 *
323 	 * Since: 2.32
324 	 */
325 	public int getHour()
326 	{
327 		return soup_date_get_hour(soupDate);
328 	}
329 
330 	/**
331 	 * Gets @date's minute.
332 	 *
333 	 * Returns: @date's minute
334 	 *
335 	 * Since: 2.32
336 	 */
337 	public int getMinute()
338 	{
339 		return soup_date_get_minute(soupDate);
340 	}
341 
342 	/**
343 	 * Gets @date's month.
344 	 *
345 	 * Returns: @date's month
346 	 *
347 	 * Since: 2.32
348 	 */
349 	public int getMonth()
350 	{
351 		return soup_date_get_month(soupDate);
352 	}
353 
354 	/**
355 	 * Gets @date's offset from UTC.
356 	 *
357 	 * Returns: @date's offset from UTC. If soup_date_get_utc()
358 	 *     returns %FALSE but soup_date_get_offset() returns 0, that means the
359 	 *     date is a "floating" time with no associated offset information.
360 	 *
361 	 * Since: 2.32
362 	 */
363 	public int getOffset()
364 	{
365 		return soup_date_get_offset(soupDate);
366 	}
367 
368 	/**
369 	 * Gets @date's second.
370 	 *
371 	 * Returns: @date's second
372 	 *
373 	 * Since: 2.32
374 	 */
375 	public int getSecond()
376 	{
377 		return soup_date_get_second(soupDate);
378 	}
379 
380 	/**
381 	 * Gets @date's UTC flag
382 	 *
383 	 * Returns: %TRUE if @date is UTC.
384 	 *
385 	 * Since: 2.32
386 	 */
387 	public int getUtc()
388 	{
389 		return soup_date_get_utc(soupDate);
390 	}
391 
392 	/**
393 	 * Gets @date's year.
394 	 *
395 	 * Returns: @date's year
396 	 *
397 	 * Since: 2.32
398 	 */
399 	public int getYear()
400 	{
401 		return soup_date_get_year(soupDate);
402 	}
403 
404 	/**
405 	 * Determines if @date is in the past.
406 	 *
407 	 * Returns: %TRUE if @date is in the past
408 	 *
409 	 * Since: 2.24
410 	 */
411 	public bool isPast()
412 	{
413 		return soup_date_is_past(soupDate) != 0;
414 	}
415 
416 	/**
417 	 * Converts @date to a string in the format described by @format.
418 	 *
419 	 * Params:
420 	 *     format = the format to generate the date in
421 	 *
422 	 * Returns: @date as a string
423 	 */
424 	public string toString(SoupDateFormat format)
425 	{
426 		auto retStr = soup_date_to_string(soupDate, format);
427 
428 		scope(exit) Str.freeString(retStr);
429 		return Str.toString(retStr);
430 	}
431 
432 	/**
433 	 * Converts @date to a <type>time_t</type>, assumming it to be in
434 	 * UTC.
435 	 *
436 	 * If @date is not representable as a <type>time_t</type>, it will be
437 	 * clamped into range. (In particular, some HTTP cookies have
438 	 * expiration dates after "Y2.038k" (2038-01-19T03:14:07Z).)
439 	 *
440 	 * Returns: @date as a <type>time_t</type>
441 	 */
442 	public uint to_time_t()
443 	{
444 		return soup_date_to_time_t(soupDate);
445 	}
446 
447 	/**
448 	 * Converts @date to a #GTimeVal.
449 	 *
450 	 * Deprecated: Do not use #GTimeVal, as it's not Y2038-safe.
451 	 *
452 	 * Params:
453 	 *     time = a #GTimeVal structure in which to store the converted time.
454 	 *
455 	 * Since: 2.24
456 	 */
457 	public void toTimeval(out TimeVal time)
458 	{
459 		GTimeVal* outtime = sliceNew!GTimeVal();
460 
461 		soup_date_to_timeval(soupDate, outtime);
462 
463 		time = new TimeVal(outtime, true);
464 	}
465 }