2 * Copyright 2010 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Christophe Huriaux, c.huriaux@gmail.com
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()
29 fDateFormat(B_HTTP_TIME_FORMAT_PREFERRED
)
34 BHttpTime::BHttpTime(BDateTime date
)
37 fDateFormat(B_HTTP_TIME_FORMAT_PREFERRED
)
42 BHttpTime::BHttpTime(const BString
& dateString
)
44 fDateString(dateString
),
46 fDateFormat(B_HTTP_TIME_FORMAT_PREFERRED
)
51 // #pragma mark Date modification
55 BHttpTime::SetString(const BString
& string
)
62 BHttpTime::SetDate(BDateTime date
)
68 // #pragma mark Date conversion
76 if (fDateString
.Length() < 4)
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
;
91 for (i
= 0; i
< sizeof(kDateFormats
) / sizeof(const char*);
93 const char* result
= strptime(fDateString
.String(), kDateFormats
[i
],
96 // Make sure we parsed enough of the date string, not just a small
98 if (result
!= NULL
&& result
> fDateString
.String() + 24) {
104 if (fDateFormat
== B_HTTP_TIME_FORMAT_PARSED
)
107 BTime
time(expireTime
.tm_hour
, expireTime
.tm_min
, expireTime
.tm_sec
);
108 BDate
date(expireTime
.tm_year
+ 1900, expireTime
.tm_mon
+ 1,
110 BDateTime
dateTime(date
, time
);
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];
138 strLength
= strftime(expirationString
, kTimetToStringMaxLength
,
139 kDateFormats
[format
], &expirationTm
);
141 expirationFinal
.SetTo(expirationString
, strLength
);
143 return expirationFinal
;