libtime++: Date and time calculation
ctime_read.c
Go to the documentation of this file.
1 /* this is <ctime_read.c>
2  * ----------------------------------------------------------------------------
3  *
4  * Copyright 2000 by Thomas Forbriger (IfG Stuttgart)
5  *
6  * ----
7  * libtime is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  * ----
21  *
22  * fill a time_Ts structure from a character string
23  *
24  * REVISIONS and CHANGES
25  * 06/08/2000 V1.0 Thomas Forbriger
26  * 09/08/2000 V1.1 correct handling of relative dates
27  * 14/12/2007 V1.2 use the Fortran integer type defined in libtime.h
28  * see there
29  *
30  * ============================================================================
31  */
32 
33 #include <libtime.h>
34 #include <string.h>
35 #include <stdio.h>
36 
37 int time_read(time_Ts *Date, const char *String)
38 {
39  double sec;
40  const char *ptr;
41  char cmilmicsec[7];
42  integer day, month, milmicsec;
43  int i;
44 
45  time_clear(Date);
46  ptr=String;
47 
48  ptr=strpbrk(ptr, "0123456789");
49  RETURNERROR((ptr==NULL), "time_read", \
50  "year is missing", EXIT_FAILURE)
51  Date->year=strtol(ptr, &ptr, 10);
52 
53  ptr=strpbrk(ptr, "0123456789");
54  RETURNERROR((ptr==NULL), "time_read", \
55  "month is missing", EXIT_FAILURE)
56  month=strtol(ptr, &ptr, 10);
57 
58  ptr=strpbrk(ptr, "0123456789");
59  RETURNERROR((ptr==NULL), "time_read", \
60  "day is missing", EXIT_FAILURE)
61  day=strtol(ptr, &ptr, 10);
62 
63  ptr=strpbrk(ptr, "0123456789");
64  if (ptr!=NULL) {
65  Date->hour=strtol(ptr, &ptr, 10);
66  ptr=strpbrk(ptr, "0123456789");
67  }
68  if (ptr!=NULL) {
69  Date->minute=strtol(ptr, &ptr, 10);
70  ptr=strpbrk(ptr, "0123456789");
71  }
72  if (ptr!=NULL) {
73  Date->second=strtol(ptr, &ptr, 10);
74  ptr=strpbrk(ptr, "0123456789");
75  }
76  if (ptr!=NULL) {
77  for(i=0; i<6; i++) {
78  if (ptr!=NULL) {
79  if (isdigit(ptr[i])) {
80  cmilmicsec[i]=ptr[i];
81  } else {
82  ptr=NULL;
83  cmilmicsec[i]='0';
84  }
85  } else {
86  cmilmicsec[i]='0';
87  }
88  cmilmicsec[6]='\0';
89  }
90  milmicsec=strtol(cmilmicsec, &ptr, 10);
91  Date->milsec=(integer)(milmicsec/1000);
92  Date->micsec=milmicsec-(Date->milsec*1000);
93  }
94 
95  if ((month>0 && Date->year>0) || month>0)
96  {
97  time_fullyear(&Date->year);
98  time_setdoy(day, month, Date);
99  }
100  else
101  {
102  Date->doy=day;
103  }
104 
105  time_norm(Date);
106  return(EXIT_SUCCESS);
107 } /* time_read */
108 
109 /* ----- END OF ctime_read.c ----- */
#define RETURNERROR(EXPR, SUB, STR, CODE)
Definition: libtime.h:96
integer milsec
Definition: libtime.h:127
integer minute
Definition: libtime.h:125
integer hour
Definition: libtime.h:124
integer doy
Definition: libtime.h:123
void time_setdoy(timeint day, timeint month, time_Ts *Pdate)
Definition: ctime_setdoy.c:36
void time_clear(time_Ts *Pdate)
Definition: ctime_clear.c:33
void time_norm(time_Ts *Pdate)
Definition: ctime_norm.c:33
void time_fullyear(timeint *year)
integer second
Definition: libtime.h:126
long int integer
Definition: libtime.h:64
integer year
Definition: libtime.h:122
#define EXIT_SUCCESS
Definition: libtime.h:86
int time_read(time_Ts *Date, const char *String)
Definition: ctime_read.c:37
#define EXIT_FAILURE
Definition: libtime.h:91
integer micsec
Definition: libtime.h:128