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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [uC-libc/] [time/] [tm_conv.c] - Blame information for rev 1778

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

Line No. Rev Author Line
1 199 simons
 
2
#if 0
3
#include <time.h>
4
 
5
/* This is a translation from ALGOL in Collected Algorithms of CACM. */
6
/* Copied from Algorithm 199, Author: Robert G. Tantzen */
7
 
8
void
9
__tm_conv(tmbuf, timep, offset)
10
struct tm *tmbuf;
11
time_t *timep;
12
time_t offset;
13
{
14
static int   moffset[] =
15
   {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
16
 
17
   long s;
18
   long  j, d, m, y;
19
 
20
   offset += *timep;
21
 
22
   tmbuf->tm_isdst = 0;          /* Someone else can set this */
23
 
24
   j = offset / 86400L + 719469;
25
   s = offset % 86400L;
26
 
27
   if( s < 0 ) { s += 86400L; j--; }
28
 
29
   tmbuf->tm_sec = s % 60;
30
   tmbuf->tm_min = (s / 60) % 60;
31
   tmbuf->tm_hour = s / 3600;
32
 
33
   tmbuf->tm_wday = (j+2) % 7;
34
 
35
   /*
36
    * Julian date converter. Takes a julian date (the number of days since
37
    * some distant epoch or other), and fills tmbuf.
38
    */
39
 
40
   y = (4L * j - 1L) / 146097L;
41
   j = 4L * j - 1L - 146097L * y;
42
   d = j / 4L;
43
   j = (4L * d + 3L) / 1461L;
44
   d = 4L * d + 3L - 1461L * j;
45
   d = (d + 4L) / 4L;
46
   m = (5L * d - 3L) / 153L;
47
   d = 5L * d - 3 - 153L * m;
48
   d = (d + 5L) / 5L;
49
   y = 100L * y + j;
50
   if (m < 10)
51
      m += 2;
52
   else
53
   {
54
      m -= 10;
55
      ++y;
56
   }
57
 
58
   tmbuf->tm_year = y - 1900;
59
   tmbuf->tm_mon = m;
60
   tmbuf->tm_mday = d;
61
 
62
   tmbuf->tm_yday = d + moffset[m];
63
   if (m > 1 && ((y) % 4 == 0 && ((y) % 100 != 0 || (y) % 400 == 0)))
64
      tmbuf->tm_yday++;
65
}
66
 
67
#else
68
 
69
/* This is adapted from glibc */
70
/* Copyright (C) 1991, 1993 Free Software Foundation, Inc */
71
 
72
#define SECS_PER_HOUR 3600L
73
#define SECS_PER_DAY  86400L
74
 
75
#include <time.h>
76
 
77
static const unsigned short int __mon_lengths[2][12] =
78
  {
79
    /* Normal years.  */
80
    { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
81
    /* Leap years.  */
82
    { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
83
  };
84
 
85
 
86
void
87
__tm_conv(tmbuf, t, offset)
88
struct tm *tmbuf;
89
time_t *t;
90
time_t offset;
91
{
92
  long days, rem;
93
  register int y;
94
  register unsigned short int *ip;
95
 
96
  days = *t / SECS_PER_DAY;
97
  rem = *t % SECS_PER_DAY;
98
  rem += offset;
99
  while (rem < 0)
100
    {
101
      rem += SECS_PER_DAY;
102
      --days;
103
    }
104
  while (rem >= SECS_PER_DAY)
105
    {
106
      rem -= SECS_PER_DAY;
107
      ++days;
108
    }
109
  tmbuf->tm_hour = rem / SECS_PER_HOUR;
110
  rem %= SECS_PER_HOUR;
111
  tmbuf->tm_min = rem / 60;
112
  tmbuf->tm_sec = rem % 60;
113
  /* January 1, 1970 was a Thursday.  */
114
  tmbuf->tm_wday = (4 + days) % 7;
115
  if (tmbuf->tm_wday < 0)
116
    tmbuf->tm_wday += 7;
117
  y = 1970;
118
  while (days >= (rem = __isleap(y) ? 366 : 365))
119
    {
120
      ++y;
121
      days -= rem;
122
    }
123
  while (days < 0)
124
    {
125
      --y;
126
      days += __isleap(y) ? 366 : 365;
127
    }
128
  tmbuf->tm_year = y - 1900;
129
  tmbuf->tm_yday = days;
130
  ip = __mon_lengths[__isleap(y)];
131
  for (y = 0; days >= ip[y]; ++y)
132
    days -= ip[y];
133
  tmbuf->tm_mon = y;
134
  tmbuf->tm_mday = days + 1;
135
  tmbuf->tm_isdst = -1;
136
}
137
 
138
#endif

powered by: WebSVN 2.1.0

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