~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/calendar.cc

Merge Stewart's dead code removal

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2008 Sun Microsystems
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
24
24
 * Common functions for dealing with calendrical calculations
25
25
 */
26
26
 
27
 
#include "config.h"
28
 
 
29
 
#if TIME_WITH_SYS_TIME
30
 
# include <sys/time.h>
31
 
# include <time.h>
32
 
#else
33
 
# if HAVE_SYS_TIME_H
34
 
#  include <sys/time.h>
35
 
# else
36
 
#  include <time.h>
37
 
# endif
38
 
#endif
39
 
#include <cstdlib>
40
 
 
 
27
#include "drizzled/global.h"
41
28
#include "drizzled/calendar.h"
42
29
 
43
 
namespace drizzled
44
 
{
45
 
 
46
30
/** Static arrays for number of days in a month and their "day ends" */
47
31
static const uint32_t __leap_days_in_month[12]=       {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
48
32
static const uint32_t __normal_days_in_month[12]=     {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
53
37
 * Private utility macro for enabling a switch between
54
38
 * Gregorian and Julian leap year date arrays.
55
39
 */
56
 
inline static const uint32_t* days_in_month(uint32_t y, enum calendar c) 
57
 
{
58
 
  if (is_leap_year(y, c))
59
 
    return __leap_days_in_month;
60
 
  else
61
 
    return __normal_days_in_month;
62
 
}
63
 
 
64
 
inline static const uint32_t* days_to_end_month(uint32_t y, enum calendar c) 
65
 
{
66
 
  if (is_leap_year(y, c))
67
 
    return __leap_days_to_end_month;
68
 
  else
69
 
    return __normal_days_to_end_month;
70
 
}
 
40
#define __DAYS_IN_MONTH(y, c) (const uint32_t *) (IS_LEAP_YEAR((y),(c)) ? __leap_days_in_month : __normal_days_in_month)
 
41
#define __DAYS_TO_END_MONTH(y, c) (const uint32_t *) (IS_LEAP_YEAR((y),(c)) ? __leap_days_to_end_month : __normal_days_to_end_month)
71
42
 
72
43
 
73
44
/**
302
273
    return (day <= __normal_days_in_month[month - 1]);
303
274
  else
304
275
  {
305
 
    const uint32_t *p_months= days_in_month(year, (enum calendar) GREGORIAN);
 
276
    const uint32_t *p_months= __DAYS_IN_MONTH(year, (enum calendar) GREGORIAN);
306
277
    return (day <= p_months[1]);
307
278
  }
308
279
}
316
287
 */
317
288
uint32_t days_in_gregorian_year_month(uint32_t year, uint32_t month)
318
289
{
319
 
  const uint32_t *p_months= days_in_month(year, GREGORIAN);
 
290
  const uint32_t *p_months= __DAYS_IN_MONTH(year, GREGORIAN);
320
291
  return p_months[month - 1];
321
292
}
322
293
 
333
304
 * @param Minute
334
305
 * @param Second
335
306
 */
336
 
bool in_unix_epoch_range(uint32_t year,
337
 
                         uint32_t month,
338
 
                         uint32_t day,
339
 
                         uint32_t hour,
340
 
                         uint32_t minute,
341
 
                         uint32_t second)
 
307
bool in_unix_epoch_range(uint32_t year
 
308
                       , uint32_t month
 
309
                       , uint32_t day
 
310
                       , uint32_t hour
 
311
                       , uint32_t minute
 
312
                       , uint32_t second)
342
313
{
343
314
  if (month == 0 || day == 0)
344
315
    return false;
345
 
 
346
316
  if (year < UNIX_EPOCH_MAX_YEARS
347
317
      && year >= UNIX_EPOCH_MIN_YEARS)
348
318
    return true;
349
 
 
350
319
  if (year < UNIX_EPOCH_MIN_YEARS)
351
320
    return false;
352
 
 
353
321
  if (year == UNIX_EPOCH_MAX_YEARS)
354
322
  {
355
323
    if (month > 1)
356
 
    {
357
324
      return false;
358
 
    }
359
325
    if (day > 19)
360
 
    {
361
326
      return false;
362
 
    }
363
327
    else if (day < 19)
364
 
    {
365
328
      return true;
366
 
    }
367
329
    else
368
330
    {
369
331
      /* We are on the final day of UNIX Epoch */
436
398
 */
437
399
uint32_t iso_week_number_from_gregorian_date(uint32_t year
438
400
                                           , uint32_t month
439
 
                                           , uint32_t day)
 
401
                                           , uint32_t day
 
402
                                           , uint32_t *year_out)
440
403
{
441
404
  struct tm broken_time;
442
405
 
 
406
  if (year_out != NULL)
 
407
    *year_out= year;
 
408
 
443
409
  broken_time.tm_year= year;
444
410
  broken_time.tm_mon= month - 1; /* struct tm has non-ordinal months */
445
411
  broken_time.tm_mday= day;
459
425
 
460
426
  uint32_t week_number= (uint32_t) atoi(result);
461
427
 
 
428
  /* 
 
429
   * ISO8601:1988 states that if the first week in January
 
430
   * does not contain 4 days, then the resulting week number
 
431
   * shall be 52 or 53, depending on the number of days in the
 
432
   * previous year.  In this case, we adjust the outbound
 
433
   * year parameter down a year.
 
434
   */
 
435
  if (year_out != NULL)
 
436
    if (week_number == 53 || week_number == 52)
 
437
      if (month == 1)
 
438
        *year_out--;
 
439
 
462
440
  return week_number;
463
441
}
464
442
 
501
479
 
502
480
  return (years * 100) + (months % 12) + 1;
503
481
}
504
 
 
505
 
} /* namespace drizzled */