1 | /* This is part of the Model Coupling Environmental Library or MCEL |
---|
2 | If you have any questions mail matt at matt@mail.bettencourt.info |
---|
3 | it is covered under the LGPL http://www.gnu.org/licenses/lgpl.html |
---|
4 | |
---|
5 | This code was developed with DoD HPCMO CHSSI and PET funding |
---|
6 | http://www.hpcmo.hpc.mil/ */ |
---|
7 | #include <stdio.h> |
---|
8 | #include <time.h> |
---|
9 | #include <string.h> |
---|
10 | |
---|
11 | |
---|
12 | #define INIT2000 \ |
---|
13 | t1.tm_sec=-1; \ |
---|
14 | t1.tm_min=-1; \ |
---|
15 | t1.tm_hour=-1; \ |
---|
16 | t1.tm_mday=-1; \ |
---|
17 | t1.tm_mon=-1; \ |
---|
18 | t1.tm_year=-1; \ |
---|
19 | t1.tm_wday=-1; \ |
---|
20 | t1.tm_yday=-1; \ |
---|
21 | t1.tm_isdst=0; \ |
---|
22 | t1.tm_year=100;\ |
---|
23 | t1.tm_mon=0;\ |
---|
24 | t1.tm_mday=1;\ |
---|
25 | t1.tm_hour = 0;\ |
---|
26 | t1.tm_min = 0;\ |
---|
27 | t1.tm_sec = 0; |
---|
28 | |
---|
29 | #ifdef LOWERCASE |
---|
30 | # ifdef UNDERSCORE |
---|
31 | # define YYMMDDHH2SECS yymmddhh2secs_ |
---|
32 | # define YYMMDDHHMMSS2SECS yymmddhhmmss2secs_ |
---|
33 | # define YYYYMMDDHHMMSS2SECS yyyymmddhhmmss2secs_ |
---|
34 | # define SECS2YYYYMMDDHHMMSS secs2yyyymmddhhmmss_ |
---|
35 | # else |
---|
36 | # define YYMMDDHH2SECS yymmddhh2secs |
---|
37 | # define YYMMDDHHMMSS2SECS yymmddhhmmss2secs |
---|
38 | # define YYYYMMDDHHMMSS2SECS yyyymmddhhmmss2secs |
---|
39 | # define SECS2YYYYMMDDHHMMSS secs2yyyymmddhhmmss |
---|
40 | # endif |
---|
41 | #else |
---|
42 | /* Do nothing */ |
---|
43 | #endif |
---|
44 | |
---|
45 | void YYMMDDHH2SECS(char* date, double* time); |
---|
46 | void YYMMDDHHMMSS2SECS(char* date, double* time); |
---|
47 | void YYYYMMDDHHMMSS2SECS(char* date, double* time); |
---|
48 | void SECS2YYYYMMDDHHMMSS(char* date, double* time); |
---|
49 | |
---|
50 | |
---|
51 | void YYMMDDHH2SECS(char* date, double* time) { |
---|
52 | |
---|
53 | char date2[12]; |
---|
54 | strncpy(date2,date,8); |
---|
55 | strncat(date2,"0000",4); |
---|
56 | |
---|
57 | YYMMDDHHMMSS2SECS(date2, time); |
---|
58 | |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | void YYMMDDHHMMSS2SECS(char* date, double* time) { |
---|
63 | |
---|
64 | |
---|
65 | char date2[14]; |
---|
66 | int year; |
---|
67 | sscanf(date,"%2d",&year); |
---|
68 | |
---|
69 | if ( year < 50 ) |
---|
70 | sprintf(date2,"20"); |
---|
71 | else |
---|
72 | sprintf(date2,"19"); |
---|
73 | strncat(date2,date,12); |
---|
74 | YYYYMMDDHHMMSS2SECS(date2,time); |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | void YYYYMMDDHHMMSS2SECS(char* date, double* time) { |
---|
79 | |
---|
80 | struct tm t1,t2; |
---|
81 | time_t tt1,tt2; |
---|
82 | |
---|
83 | INIT2000; |
---|
84 | t2=t1; |
---|
85 | sscanf(date,"%4d%2d%2d%2d%2d%2d",&t2.tm_year,&t2.tm_mon,&t2.tm_mday, |
---|
86 | &t2.tm_hour,&t2.tm_min,&t2.tm_sec); |
---|
87 | /* Correct year, month */ |
---|
88 | t2.tm_year -= 1900; |
---|
89 | t2.tm_mon -= 1; |
---|
90 | |
---|
91 | |
---|
92 | tt1 = mktime(&t1); |
---|
93 | tt2 = mktime(&t2); |
---|
94 | |
---|
95 | *time = difftime(tt2,tt1); |
---|
96 | |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | void SECS2YYYYMMDDHHMMSS(char* date, double* time) { |
---|
101 | |
---|
102 | struct tm t1,t2; |
---|
103 | time_t tt1,tt2; |
---|
104 | INIT2000; |
---|
105 | tt1 = mktime(&t1); |
---|
106 | tt2 = tt1+(unsigned long int )*time; |
---|
107 | t2=t1; |
---|
108 | localtime_r(&tt2,&t2); |
---|
109 | if (t2.tm_isdst) |
---|
110 | tt2 -= 3600; |
---|
111 | localtime_r(&tt2,&t2); |
---|
112 | |
---|
113 | sprintf(date,"%04d%02d%02d%02d%02d%02d", |
---|
114 | 1900+t2.tm_year, |
---|
115 | t2.tm_mon+1, |
---|
116 | t2.tm_mday, |
---|
117 | t2.tm_hour, |
---|
118 | t2.tm_min, |
---|
119 | t2.tm_sec); |
---|
120 | } |
---|