ctime_read.c 2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* this is <ctime_read.c>
 * ----------------------------------------------------------------------------
 *
 * $Id: ctime_read.c,v 1.1 2000-08-06 12:00:39 thof Exp $
 *
 * 06/08/2000 by Thomas Forbriger (IfG Stuttgart)
 *
 * fill a time_Ts structure from a character string
 *
 * REVISIONS and CHANGES
 *    06/08/2000   V1.0   Thomas Forbriger
 *
 * ============================================================================
 */

#include <libtime.h>
#include <string.h>

int time_read(time_Ts *Date, char *String)
{
  double sec;
  char *ptr;
  char cmilmicsec[7];
  long int day, month, milmicsec;
  int i;

  time_clear(Date);
  ptr=String;

  ptr=strpbrk(ptr, "0123456789");
  RETURNERROR((ptr==NULL), "time_read", \
    "year is missing", EXIT_FAILURE)
  Date->year=strtol(ptr, &ptr, 10);

  ptr=strpbrk(ptr, "0123456789");
  RETURNERROR((ptr==NULL), "time_read", \
    "month is missing", EXIT_FAILURE)
  month=strtol(ptr, &ptr, 10);

  ptr=strpbrk(ptr, "0123456789");
  RETURNERROR((ptr==NULL), "time_read", \
    "day is missing", EXIT_FAILURE)
  day=strtol(ptr, &ptr, 10);

  ptr=strpbrk(ptr, "0123456789");
  if (ptr!=NULL) {
    Date->hour=strtol(ptr, &ptr, 10);
    ptr=strpbrk(ptr, "0123456789");
  }
  if (ptr!=NULL) {
    Date->minute=strtol(ptr, &ptr, 10);
    ptr=strpbrk(ptr, "0123456789");
  }
  if (ptr!=NULL) {
    Date->second=strtol(ptr, &ptr, 10);
    ptr=strpbrk(ptr, "0123456789");
  }
  if (ptr!=NULL) {
    for(i=0; i<6; i++) {
      if (ptr!=NULL) {
        if (isdigit(ptr[i])) {
          cmilmicsec[i]=ptr[i];
        } else {
          ptr=NULL;
          cmilmicsec[i]='0';
        }
      } else {
        cmilmicsec[i]='0';
      }
      cmilmicsec[6]='\0';
    }
    milmicsec=strtol(cmilmicsec, &ptr, 10);
    Date->milsec=(long int)(milmicsec/1000);
    Date->micsec=milmicsec-(Date->milsec*1000);
  }

  if (month>0) { time_fullyear(&Date->year); }
  time_setdoy(day, month, Date);

  time_finish(Date);
  time_norm(Date);
  return(EXIT_SUCCESS);
} /* time_read */
 
/* ----- END OF ctime_read.c ----- */