xref: /curl/docs/libcurl/curl_getdate.md (revision 5a488251)
1---
2c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
3SPDX-License-Identifier: curl
4Title: curl_getdate
5Section: 3
6Source: libcurl
7See-also:
8  - CURLOPT_TIMECONDITION (3)
9  - CURLOPT_TIMEVALUE (3)
10  - curl_easy_escape (3)
11  - curl_easy_unescape (3)
12Protocol:
13  - All
14Added-in: 7.1
15---
16
17# NAME
18
19curl_getdate - convert date string to number of seconds
20
21# SYNOPSIS
22
23~~~c
24#include <curl/curl.h>
25
26time_t curl_getdate(const char *datestring, const time_t *now);
27~~~
28
29# DESCRIPTION
30
31curl_getdate(3) returns the number of seconds since the Epoch, January
321st 1970 00:00:00 in the UTC time zone, for the date and time that the
33*datestring* parameter specifies. The *now* parameter is not used,
34pass a NULL there.
35
36This function works with valid dates and does not always detect and reject
37wrong dates, such as February 30.
38
39# PARSING DATES AND TIMES
40
41A "date" is a string containing several items separated by whitespace. The
42order of the items is immaterial. A date string may contain many flavors of
43items:
44
45## calendar date items
46
47Can be specified several ways. Month names can only be three-letter English
48abbreviations, numbers can be zero-prefixed and the year may use 2 or 4
49digits. Examples: 06 Nov 1994, 06-Nov-94 and Nov-94 6.
50
51If the year appears to be below 100 (two-digit), any year after 70 is assumed
52to be 1900 + the given year. All others are 2000 + the given year.
53
54## time of the day items
55
56This string specifies the time on a given day. You must specify it with 6
57digits with two colons: HH:MM:SS. If there is no time given in a provided date
58string, 00:00:00 is assumed. Example: 18:19:21.
59
60## time zone items
61
62Specifies international time zone. There are a few acronyms supported, but in
63general you should instead use the specific relative time compared to
64UTC. Supported formats include: -1200, MST, +0100.
65
66## day of the week items
67
68Specifies a day of the week. Days of the week may be spelled out in full
69(using English): 'Sunday', 'Monday', etc or they may be abbreviated to their
70first three letters. This is usually not info that adds anything.
71
72## pure numbers
73
74If a decimal number of the form YYYYMMDD appears, then YYYY is read as the
75year, MM as the month number and DD as the day of the month, for the specified
76calendar date.
77
78# %PROTOCOLS%
79
80# EXAMPLE
81
82~~~c
83int main(void)
84{
85  time_t t;
86  t = curl_getdate("Sun, 06 Nov 1994 08:49:37 GMT", NULL);
87  t = curl_getdate("Sunday, 06-Nov-94 08:49:37 GMT", NULL);
88  t = curl_getdate("Sun Nov  6 08:49:37 1994", NULL);
89  t = curl_getdate("06 Nov 1994 08:49:37 GMT", NULL);
90  t = curl_getdate("06-Nov-94 08:49:37 GMT", NULL);
91  t = curl_getdate("Nov  6 08:49:37 1994", NULL);
92  t = curl_getdate("06 Nov 1994 08:49:37", NULL);
93  t = curl_getdate("06-Nov-94 08:49:37", NULL);
94  t = curl_getdate("1994 Nov 6 08:49:37", NULL);
95  t = curl_getdate("GMT 08:49:37 06-Nov-94 Sunday", NULL);
96  t = curl_getdate("94 6 Nov 08:49:37", NULL);
97  t = curl_getdate("1994 Nov 6", NULL);
98  t = curl_getdate("06-Nov-94", NULL);
99  t = curl_getdate("Sun Nov 6 94", NULL);
100  t = curl_getdate("1994.Nov.6", NULL);
101  t = curl_getdate("Sun/Nov/6/94/GMT", NULL);
102  t = curl_getdate("Sun, 06 Nov 1994 08:49:37 CET", NULL);
103  t = curl_getdate("06 Nov 1994 08:49:37 EST", NULL);
104  t = curl_getdate("Sun, 12 Sep 2004 15:05:58 -0700", NULL);
105  t = curl_getdate("Sat, 11 Sep 2004 21:32:11 +0200", NULL);
106  t = curl_getdate("20040912 15:05:58 -0700", NULL);
107  t = curl_getdate("20040911 +0200", NULL);
108}
109~~~
110
111# STANDARDS
112
113This parser handles date formats specified in RFC 822 (including the update in
114RFC 1123) using time zone name or time zone delta and RFC 850 (obsoleted by
115RFC 1036) and ANSI C's *asctime()* format.
116
117These formats are the only ones RFC 7231 says HTTP applications may use.
118
119# %AVAILABILITY%
120
121# RETURN VALUE
122
123This function returns -1 when it fails to parse the date string. Otherwise it
124returns the number of seconds as described.
125
126On systems with a signed 32-bit time_t: if the year is larger than 2037 or
127less than 1903, this function returns -1.
128
129On systems with an unsigned 32-bit time_t: if the year is larger than 2106 or
130less than 1970, this function returns -1.
131
132On systems with 64-bit time_t: if the year is less than 1583, this function
133returns -1. (The Gregorian calendar was first introduced 1582 so no "real"
134dates in this way of doing dates existed before then.)
135