OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib/] [newlib/] [libc/] [time/] [lcltime_r.c] - Blame information for rev 1774

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 39 lampret
/*
2
 * localtime_r.c
3
 * Original Author:     Adapted from tzcode maintained by Arthur David Olson.
4
 *
5
 * Converts the calendar time pointed to by tim_p into a broken-down time
6
 * expressed as local time. Returns a pointer to a structure containing the
7
 * broken-down time.
8
 */
9
 
10
#include <stdlib.h>
11
#include <time.h>
12
 
13
#define SECSPERMIN      60L
14
#define MINSPERHOUR     60L
15
#define HOURSPERDAY     24L
16
#define SECSPERHOUR     (SECSPERMIN * MINSPERHOUR)
17
#define SECSPERDAY      (SECSPERHOUR * HOURSPERDAY)
18
#define DAYSPERWEEK     7
19
#define MONSPERYEAR     12
20
 
21
#define YEAR_BASE       1900
22
#define EPOCH_YEAR      1970
23
#define EPOCH_WDAY      4
24
 
25
#define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
26
 
27
static _CONST int mon_lengths[2][MONSPERYEAR] = {
28
  {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
29
  {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
30
} ;
31
 
32
static _CONST int year_lengths[2] = {
33
  365,
34
  366
35
} ;
36
 
37
struct tm *
38
_DEFUN (localtime_r, (tim_p, res),
39
        _CONST time_t * tim_p _AND
40
        struct tm *res)
41
{
42
  long days, rem;
43
  int y;
44
  int yleap;
45
  _CONST int *ip;
46
 
47
  days = ((long) *tim_p) / SECSPERDAY;
48
  rem = ((long) *tim_p) % SECSPERDAY;
49
  while (rem < 0)
50
    {
51
      rem += SECSPERDAY;
52
      --days;
53
    }
54
  while (rem >= SECSPERDAY)
55
    {
56
      rem -= SECSPERDAY;
57
      ++days;
58
    }
59
 
60
  /* compute hour, min, and sec */
61
  res->tm_hour = (int) (rem / SECSPERHOUR);
62
  rem %= SECSPERHOUR;
63
  res->tm_min = (int) (rem / SECSPERMIN);
64
  res->tm_sec = (int) (rem % SECSPERMIN);
65
 
66
  /* compute day of week */
67
  if ((res->tm_wday = ((EPOCH_WDAY + days) % DAYSPERWEEK)) < 0)
68
    res->tm_wday += DAYSPERWEEK;
69
 
70
  /* compute year & day of year */
71
  y = EPOCH_YEAR;
72
  if (days >= 0)
73
    {
74
      for (;;)
75
        {
76
          yleap = isleap(y);
77
          if (days < year_lengths[yleap])
78
            break;
79
          y++;
80
          days -= year_lengths[yleap];
81
        }
82
    }
83
  else
84
    {
85
      do
86
        {
87
          --y;
88
          yleap = isleap(y);
89
          days += year_lengths[yleap];
90
        } while (days < 0);
91
    }
92
 
93
  res->tm_year = y - YEAR_BASE;
94
  res->tm_yday = days;
95
  ip = mon_lengths[yleap];
96
  for (res->tm_mon = 0; days >= ip[res->tm_mon]; ++res->tm_mon)
97
    days -= ip[res->tm_mon];
98
  res->tm_mday = days + 1;
99
 
100
  /* set daylight saving time flag */
101
  res->tm_isdst = -1;
102
 
103
  return (res);
104
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.