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