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