1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "calDateTime.h"
#include "calBaseCID.h"
#include "nsServiceManagerUtils.h"
#include "nsIClassInfoImpl.h"
#include "calIErrors.h"
#include "calDuration.h"
#include "jsapi.h"
#include "jsfriendapi.h"
#include "jswrapper.h"
#include "prprf.h"
extern "C" {
#include "ical.h"
}
#define CAL_ATTR_SET_PRE NS_ENSURE_FALSE(mImmutable, NS_ERROR_OBJECT_IS_IMMUTABLE)
#define CAL_ATTR_SET_POST Normalize()
#include "calAttributeHelpers.h"
NS_IMPL_CLASSINFO(calDateTime, nullptr, 0, CAL_DATETIME_CID)
NS_IMPL_ISUPPORTS_CI(calDateTime, calIDateTime, calIDateTimeLibical)
calDateTime::calDateTime()
: mImmutable(false)
{
Reset();
}
calDateTime::calDateTime(icaltimetype const* atimeptr, calITimezone *tz)
: mImmutable(false)
{
FromIcalTime(atimeptr, tz);
}
NS_IMETHODIMP
calDateTime::GetIsMutable(bool *aResult)
{
NS_ENSURE_ARG_POINTER(aResult);
*aResult = !mImmutable;
return NS_OK;
}
NS_IMETHODIMP
calDateTime::MakeImmutable()
{
mImmutable = true;
return NS_OK;
}
NS_IMETHODIMP
calDateTime::Clone(calIDateTime **aResult)
{
NS_ENSURE_ARG_POINTER(aResult);
icaltimetype itt;
ToIcalTime(&itt);
calDateTime * const cdt = new calDateTime(&itt, mTimezone);
CAL_ENSURE_MEMORY(cdt);
NS_ADDREF(*aResult = cdt);
return NS_OK;
}
NS_IMETHODIMP
calDateTime::ResetTo(int16_t year,
int16_t month,
int16_t day,
int16_t hour,
int16_t minute,
int16_t second,
calITimezone * tz)
{
NS_ENSURE_FALSE(mImmutable, NS_ERROR_OBJECT_IS_IMMUTABLE);
NS_ENSURE_ARG_POINTER(tz);
mYear = year;
mMonth = month;
mDay = day;
mHour = hour;
mMinute = minute;
mSecond = second;
mIsDate = false;
mTimezone = tz;
Normalize();
return NS_OK;
}
NS_IMETHODIMP
calDateTime::Reset()
{
NS_ENSURE_FALSE(mImmutable, NS_ERROR_OBJECT_IS_IMMUTABLE);
mYear = 1970;
mMonth = 0;
mDay = 1;
mHour = 0;
mMinute = 0;
mSecond = 0;
mWeekday = 4;
mYearday = 1;
mIsDate = false;
mTimezone = nullptr;
mNativeTime = 0;
mIsValid = true;
return NS_OK;
}
CAL_VALUETYPE_ATTR(calDateTime, int16_t, Year)
CAL_VALUETYPE_ATTR(calDateTime, int16_t, Month)
CAL_VALUETYPE_ATTR(calDateTime, int16_t, Day)
CAL_VALUETYPE_ATTR(calDateTime, int16_t, Hour)
CAL_VALUETYPE_ATTR(calDateTime, int16_t, Minute)
CAL_VALUETYPE_ATTR(calDateTime, int16_t, Second)
CAL_VALUETYPE_ATTR(calDateTime, bool, IsDate)
CAL_VALUETYPE_ATTR_GETTER(calDateTime, bool, IsValid)
CAL_VALUETYPE_ATTR_GETTER(calDateTime, PRTime, NativeTime)
CAL_VALUETYPE_ATTR_GETTER(calDateTime, int16_t, Weekday)
CAL_VALUETYPE_ATTR_GETTER(calDateTime, int16_t, Yearday)
NS_IMETHODIMP
calDateTime::GetTimezone(calITimezone **aResult)
{
NS_ENSURE_ARG_POINTER(aResult);
ensureTimezone();
NS_IF_ADDREF(*aResult = mTimezone);
return NS_OK;
}
NS_IMETHODIMP
calDateTime::SetTimezone(calITimezone *aValue)
{
NS_ENSURE_FALSE(mImmutable, NS_ERROR_OBJECT_IS_IMMUTABLE);
NS_ENSURE_ARG_POINTER(aValue);
mTimezone = aValue;
CAL_ATTR_SET_POST;
return NS_OK;
}
NS_IMETHODIMP
calDateTime::GetTimezoneOffset(int32_t *aResult)
{
NS_ENSURE_ARG_POINTER(aResult);
icaltimetype icalt;
ToIcalTime(&icalt);
int dst;
*aResult = icaltimezone_get_utc_offset(const_cast<icaltimezone *>(icalt.zone), &icalt, &dst);
return NS_OK;
}
NS_IMETHODIMP
calDateTime::SetNativeTime(PRTime aNativeTime)
{
icaltimetype icalt;
PRTimeToIcaltime(aNativeTime, false, icaltimezone_get_utc_timezone(), &icalt);
nsCOMPtr<calITimezone> ctz = cal::UTC();
FromIcalTime(&icalt, ctz);
return NS_OK;
}
NS_IMETHODIMP
calDateTime::AddDuration(calIDuration *aDuration)
{
NS_ENSURE_FALSE(mImmutable, NS_ERROR_OBJECT_IS_IMMUTABLE);
NS_ENSURE_ARG_POINTER(aDuration);
ensureTimezone();
nsresult rv;
nsCOMPtr<calIDurationLibical> icaldur = do_QueryInterface(aDuration, &rv);
NS_ENSURE_SUCCESS(rv, rv);
icaldurationtype idt;
icaldur->ToIcalDuration(&idt);
icaltimetype itt;
ToIcalTime(&itt);
icaltimetype const newitt = icaltime_add(itt, idt);
FromIcalTime(&newitt, mTimezone);
return NS_OK;
}
NS_IMETHODIMP
calDateTime::SubtractDate(calIDateTime *aDate, calIDuration **aDuration)
{
NS_ENSURE_ARG_POINTER(aDate);
NS_ENSURE_ARG_POINTER(aDuration);
// same as icaltime_subtract(), but minding timezones:
PRTime t2t;
aDate->GetNativeTime(&t2t);
// for a duration, need to convert the difference in microseconds (prtime)
// to seconds (libical), so divide by one million.
icaldurationtype const idt = icaldurationtype_from_int(
static_cast<int>((mNativeTime - t2t) / int64_t(PR_USEC_PER_SEC)));
calDuration * const dur = new calDuration(&idt);
CAL_ENSURE_MEMORY(dur);
NS_ADDREF(*aDuration = dur);
return NS_OK;
}
NS_IMETHODIMP
calDateTime::ToString(nsACString & aResult)
{
nsAutoCString tzid;
char buffer[256];
ensureTimezone();
mTimezone->GetTzid(tzid);
uint32_t const length = PR_snprintf(
buffer, sizeof(buffer), "%04hd/%02hd/%02hd %02hd:%02hd:%02hd %s isDate=%01hd nativeTime=%lld",
mYear, mMonth + 1, mDay, mHour, mMinute, mSecond,
tzid.get(), static_cast<int16_t>(mIsDate), mNativeTime);
if (length != static_cast<uint32_t>(-1))
aResult.Assign(buffer, length);
return NS_OK;
}
NS_IMETHODIMP
calDateTime::GetInTimezone(calITimezone * aTimezone, calIDateTime ** aResult)
{
NS_ENSURE_ARG_POINTER(aTimezone);
NS_ENSURE_ARG_POINTER(aResult);
if (mIsDate) {
// if it's a date, we really just want to make a copy of this
// and set the timezone.
nsresult rv = Clone(aResult);
if (NS_SUCCEEDED(rv)) {
rv = (*aResult)->SetTimezone(aTimezone);
}
return rv;
} else {
icaltimetype icalt;
ToIcalTime(&icalt);
icaltimezone * tz = cal::getIcalTimezone(aTimezone);
if (icalt.zone == tz) {
return Clone(aResult);
}
/* If there's a zone, we need to convert; otherwise, we just
* assign, since this item is floating */
if (icalt.zone && tz) {
icaltimezone_convert_time(&icalt, const_cast<icaltimezone *>(icalt.zone), tz);
}
icalt.zone = tz;
icalt.is_utc = (tz && tz == icaltimezone_get_utc_timezone());
calDateTime * cdt = new calDateTime(&icalt, aTimezone);
CAL_ENSURE_MEMORY(cdt);
NS_ADDREF (*aResult = cdt);
return NS_OK;
}
}
NS_IMETHODIMP
calDateTime::GetStartOfWeek(calIDateTime ** aResult)
{
NS_ENSURE_ARG_POINTER(aResult);
ensureTimezone();
icaltimetype icalt;
ToIcalTime(&icalt);
int day_of_week = icaltime_day_of_week(icalt);
if (day_of_week > 1)
icaltime_adjust(&icalt, - (day_of_week - 1), 0, 0, 0);
icalt.is_date = 1;
calDateTime * const cdt = new calDateTime(&icalt, mTimezone);
CAL_ENSURE_MEMORY(cdt);
NS_ADDREF(*aResult = cdt);
return NS_OK;
}
NS_IMETHODIMP
calDateTime::GetEndOfWeek(calIDateTime ** aResult)
{
NS_ENSURE_ARG_POINTER(aResult);
ensureTimezone();
icaltimetype icalt;
ToIcalTime(&icalt);
int day_of_week = icaltime_day_of_week(icalt);
if (day_of_week < 7)
icaltime_adjust(&icalt, 7 - day_of_week, 0, 0, 0);
icalt.is_date = 1;
calDateTime * const cdt = new calDateTime(&icalt, mTimezone);
CAL_ENSURE_MEMORY(cdt);
NS_ADDREF(*aResult = cdt);
return NS_OK;
}
NS_IMETHODIMP
calDateTime::GetStartOfMonth(calIDateTime ** aResult)
{
NS_ENSURE_ARG_POINTER(aResult);
ensureTimezone();
icaltimetype icalt;
ToIcalTime(&icalt);
icalt.day = 1;
icalt.is_date = 1;
calDateTime * const cdt = new calDateTime(&icalt, mTimezone);
CAL_ENSURE_MEMORY(cdt);
NS_ADDREF(*aResult = cdt);
return NS_OK;
}
NS_IMETHODIMP
calDateTime::GetEndOfMonth(calIDateTime ** aResult)
{
NS_ENSURE_ARG_POINTER(aResult);
ensureTimezone();
icaltimetype icalt;
ToIcalTime(&icalt);
icalt.day = icaltime_days_in_month(icalt.month, icalt.year);
icalt.is_date = 1;
calDateTime * const cdt = new calDateTime(&icalt, mTimezone);
CAL_ENSURE_MEMORY(cdt);
NS_ADDREF(*aResult = cdt);
return NS_OK;
}
NS_IMETHODIMP
calDateTime::GetStartOfYear(calIDateTime ** aResult)
{
NS_ENSURE_ARG_POINTER(aResult);
ensureTimezone();
icaltimetype icalt;
ToIcalTime(&icalt);
icalt.month = 1;
icalt.day = 1;
icalt.is_date = 1;
calDateTime * const cdt = new calDateTime(&icalt, mTimezone);
CAL_ENSURE_MEMORY(cdt);
NS_ADDREF(*aResult = cdt);
return NS_OK;
}
NS_IMETHODIMP
calDateTime::GetEndOfYear(calIDateTime ** aResult)
{
NS_ENSURE_ARG_POINTER(aResult);
ensureTimezone();
icaltimetype icalt;
ToIcalTime(&icalt);
icalt.month = 12;
icalt.day = 31;
icalt.is_date = 1;
calDateTime * const cdt = new calDateTime(&icalt, mTimezone);
CAL_ENSURE_MEMORY(cdt);
NS_ADDREF(*aResult = cdt);
return NS_OK;
}
NS_IMETHODIMP
calDateTime::GetIcalString(nsACString& aResult)
{
icaltimetype t;
ToIcalTime(&t);
// note that ics is owned by libical, so we don't need to free
char const * const ics = icaltime_as_ical_string(t);
CAL_ENSURE_MEMORY(ics);
aResult.Assign(ics);
return NS_OK;
}
NS_IMETHODIMP
calDateTime::SetIcalString(nsACString const& aIcalString)
{
NS_ENSURE_FALSE(mImmutable, NS_ERROR_OBJECT_IS_IMMUTABLE);
icaltimetype icalt;
icalt = icaltime_from_string(PromiseFlatCString(aIcalString).get());
if (icaltime_is_null_time(icalt)) {
return static_cast<nsresult>(calIErrors::ICS_ERROR_BASE + icalerrno);
}
FromIcalTime(&icalt, nullptr);
return NS_OK;
}
/**
** utility/protected methods
**/
// internal Normalize():
void calDateTime::Normalize()
{
icaltimetype icalt;
ensureTimezone();
ToIcalTime(&icalt);
FromIcalTime(&icalt, mTimezone);
}
void
calDateTime::ensureTimezone()
{
if (mTimezone == nullptr) {
mTimezone = cal::UTC();
}
}
NS_IMETHODIMP_(void)
calDateTime::ToIcalTime(struct icaltimetype * icalt)
{
ensureTimezone();
icalt->year = mYear;
icalt->month = mMonth + 1;
icalt->day = mDay;
icalt->hour = mHour;
icalt->minute = mMinute;
icalt->second = mSecond;
icalt->is_date = mIsDate ? 1 : 0;
icalt->is_daylight = 0;
icaltimezone * tz = cal::getIcalTimezone(mTimezone);
icalt->zone = tz;
icalt->is_utc = (tz && tz == icaltimezone_get_utc_timezone());
icalt->is_daylight = 0;
// xxx todo: discuss/investigate is_daylight
// if (tz) {
// icaltimezone_get_utc_offset(tz, icalt, &icalt->is_daylight);
// }
}
void calDateTime::FromIcalTime(icaltimetype const* icalt, calITimezone * tz)
{
icaltimetype t = *icalt;
mIsValid = (icaltime_is_null_time(t) ||
icaltime_is_valid_time(t) ? true : false);
mIsDate = t.is_date ? true : false;
if (mIsDate) {
t.hour = 0;
t.minute = 0;
t.second = 0;
}
if (mIsValid) {
t = icaltime_normalize(t);
}
mYear = static_cast<int16_t>(t.year);
mMonth = static_cast<int16_t>(t.month - 1);
mDay = static_cast<int16_t>(t.day);
mHour = static_cast<int16_t>(t.hour);
mMinute = static_cast<int16_t>(t.minute);
mSecond = static_cast<int16_t>(t.second);
if (tz) {
mTimezone = tz;
} else {
mTimezone = cal::detectTimezone(t, nullptr);
}
#if defined(DEBUG)
if (mTimezone) {
if (t.is_utc) {
nsCOMPtr<calITimezone> ctz = cal::UTC();
NS_ASSERTION(SameCOMIdentity(mTimezone, ctz), "UTC mismatch!");
} else if (!t.zone) {
nsAutoCString tzid;
mTimezone->GetTzid(tzid);
if (tzid.EqualsLiteral("floating")) {
nsCOMPtr<calITimezone> ctz = cal::floating();
NS_ASSERTION(SameCOMIdentity(mTimezone, ctz), "floating mismatch!");
}
} else {
nsAutoCString tzid;
mTimezone->GetTzid(tzid);
NS_ASSERTION(tzid.Equals(icaltimezone_get_tzid(const_cast<icaltimezone *>(t.zone))),
"tzid mismatch!");
}
}
#endif
mWeekday = static_cast<int16_t>(icaltime_day_of_week(t) - 1);
mYearday = static_cast<int16_t>(icaltime_day_of_year(t));
// mNativeTime: not moving the existing date to UTC,
// but merely representing it a UTC-based way.
t.is_date = 0;
mNativeTime = IcaltimeToPRTime(&t, icaltimezone_get_utc_timezone());
}
PRTime calDateTime::IcaltimeToPRTime(icaltimetype const* icalt, icaltimezone const* tz)
{
icaltimetype tt;
PRExplodedTime et;
/* If the time is the special null time, return 0. */
if (icaltime_is_null_time(*icalt)) {
return 0;
}
if (tz) {
// use libical for timezone conversion, as it can handle all ics
// timezones. having nspr do it is much harder.
tt = icaltime_convert_to_zone(*icalt, const_cast<icaltimezone *>(tz));
} else {
tt = *icalt;
}
/* Empty the destination */
memset(&et, 0, sizeof(struct PRExplodedTime));
/* Fill the fields */
if (icaltime_is_date(tt)) {
et.tm_sec = et.tm_min = et.tm_hour = 0;
} else {
et.tm_sec = tt.second;
et.tm_min = tt.minute;
et.tm_hour = tt.hour;
}
et.tm_mday = static_cast<int16_t>(tt.day);
et.tm_month = static_cast<int16_t>(tt.month-1);
et.tm_year = static_cast<int16_t>(tt.year);
return PR_ImplodeTime(&et);
}
void calDateTime::PRTimeToIcaltime(PRTime time, bool isdate,
icaltimezone const* tz,
icaltimetype * icalt)
{
PRExplodedTime et;
PR_ExplodeTime(time, PR_GMTParameters, &et);
icalt->year = et.tm_year;
icalt->month = et.tm_month + 1;
icalt->day = et.tm_mday;
if (isdate) {
icalt->hour = 0;
icalt->minute = 0;
icalt->second = 0;
icalt->is_date = 1;
} else {
icalt->hour = et.tm_hour;
icalt->minute = et.tm_min;
icalt->second = et.tm_sec;
icalt->is_date = 0;
}
icalt->zone = tz;
icalt->is_utc = ((tz && tz == icaltimezone_get_utc_timezone()) ? 1 : 0);
icalt->is_daylight = 0;
// xxx todo: discuss/investigate is_daylight
// if (tz) {
// icaltimezone_get_utc_offset(tz, icalt, &icalt->is_daylight);
// }
}
NS_IMETHODIMP
calDateTime::Compare(calIDateTime * aOther, int32_t * aResult)
{
NS_ENSURE_ARG_POINTER(aOther);
NS_ENSURE_ARG_POINTER(aResult);
nsresult rv;
nsCOMPtr<calIDateTimeLibical> icalother = do_QueryInterface(aOther, &rv);
NS_ENSURE_SUCCESS(rv, rv);
bool otherIsDate = false;
aOther->GetIsDate(&otherIsDate);
icaltimetype a, b;
ToIcalTime(&a);
icalother->ToIcalTime(&b);
// If either this or aOther is floating, both objects are treated
// as floating for the comparison.
if (!a.zone || !b.zone) {
a.zone = nullptr;
a.is_utc = 0;
b.zone = nullptr;
b.is_utc = 0;
}
if (mIsDate || otherIsDate) {
*aResult = icaltime_compare_date_only_tz(a, b, cal::getIcalTimezone(mTimezone));
} else {
*aResult = icaltime_compare(a, b);
}
return NS_OK;
}
|