BPicture: Fix archive constructor.
[haiku.git] / src / kits / network / libnetapi / HttpTime.cpp
blobc42ab5ff04b743944461bd486f994ed9a9ad544b
1 /*
2 * Copyright 2010 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Christophe Huriaux, c.huriaux@gmail.com
7 */
9 #include <HttpTime.h>
11 #include <new>
13 #include <cstdio>
16 static const char* kDateFormats[] = {
17 "%a, %d %b %Y %H:%M:%S",
18 "%a, %d-%b-%Y %H:%M:%S",
19 "%A, %d-%b-%y %H:%M:%S",
20 "%a %d %b %H:%M:%S %Y"
23 using namespace BPrivate;
26 BHttpTime::BHttpTime()
28 fDate(0),
29 fDateFormat(B_HTTP_TIME_FORMAT_PREFERRED)
34 BHttpTime::BHttpTime(BDateTime date)
36 fDate(date),
37 fDateFormat(B_HTTP_TIME_FORMAT_PREFERRED)
42 BHttpTime::BHttpTime(const BString& dateString)
44 fDateString(dateString),
45 fDate(0),
46 fDateFormat(B_HTTP_TIME_FORMAT_PREFERRED)
51 // #pragma mark Date modification
54 void
55 BHttpTime::SetString(const BString& string)
57 fDateString = string;
61 void
62 BHttpTime::SetDate(BDateTime date)
64 fDate = date;
68 // #pragma mark Date conversion
71 BDateTime
72 BHttpTime::Parse()
74 struct tm expireTime;
76 if (fDateString.Length() < 4)
77 return 0;
79 expireTime.tm_sec = 0;
80 expireTime.tm_min = 0;
81 expireTime.tm_hour = 0;
82 expireTime.tm_mday = 0;
83 expireTime.tm_mon = 0;
84 expireTime.tm_year = 0;
85 expireTime.tm_wday = 0;
86 expireTime.tm_yday = 0;
87 expireTime.tm_isdst = 0;
89 fDateFormat = B_HTTP_TIME_FORMAT_PARSED;
90 unsigned int i;
91 for (i = 0; i < sizeof(kDateFormats) / sizeof(const char*);
92 i++) {
93 const char* result = strptime(fDateString.String(), kDateFormats[i],
94 &expireTime);
96 // Make sure we parsed enough of the date string, not just a small
97 // part of it.
98 if (result != NULL && result > fDateString.String() + 24) {
99 fDateFormat = i;
100 break;
104 if (fDateFormat == B_HTTP_TIME_FORMAT_PARSED)
105 return 0;
107 BTime time(expireTime.tm_hour, expireTime.tm_min, expireTime.tm_sec);
108 BDate date(expireTime.tm_year + 1900, expireTime.tm_mon + 1,
109 expireTime.tm_mday);
110 BDateTime dateTime(date, time);
111 return dateTime;
115 BString
116 BHttpTime::ToString(int8 format)
118 BString expirationFinal;
119 struct tm expirationTm;
120 expirationTm.tm_sec = fDate.Time().Second();
121 expirationTm.tm_min = fDate.Time().Minute();
122 expirationTm.tm_hour = fDate.Time().Hour();
123 expirationTm.tm_mday = fDate.Date().Day();
124 expirationTm.tm_mon = fDate.Date().Month() - 1;
125 expirationTm.tm_year = fDate.Date().Year() - 1900;
126 expirationTm.tm_wday = 0;
127 expirationTm.tm_yday = 0;
128 expirationTm.tm_isdst = 0;
130 if (format == B_HTTP_TIME_FORMAT_PARSED)
131 format = fDateFormat;
133 if (format != B_HTTP_TIME_FORMAT_PARSED) {
134 static const uint16 kTimetToStringMaxLength = 128;
135 char expirationString[kTimetToStringMaxLength + 1];
136 size_t strLength;
138 strLength = strftime(expirationString, kTimetToStringMaxLength,
139 kDateFormats[format], &expirationTm);
141 expirationFinal.SetTo(expirationString, strLength);
143 return expirationFinal;