base_constr.cc 2.09 KB
Newer Older
thomas.forbriger's avatar
thomas.forbriger committed
1
2
3
/* this is <base_constr.cc>
 * ----------------------------------------------------------------------------
 *
4
 * $Id: base_constr.cc,v 1.7 2004-01-13 19:50:55 tforb Exp $
thomas.forbriger's avatar
thomas.forbriger committed
5
6
7
8
9
10
11
 *
 * 09/08/2000 by Thomas Forbriger (IfG Stuttgart)
 *
 * TBaseClassTime constructor
 *
 * REVISIONS and CHANGES
 *    09/08/2000   V1.0   Thomas Forbriger
thomas.forbriger's avatar
thomas.forbriger committed
12
 *    06/09/2000   V1.1   added character array initializer
13
14
 *    22/12/2000   V1.2   changed namespace time to libtime (resolved conflict
 *                        with system time library)
thomas.forbriger's avatar
thomas.forbriger committed
15
 *    22/12/2003   V1.3   added function now()
thomas.forbriger's avatar
thomas.forbriger committed
16
 *                        correction: month in struct tm is in (0,11)
thomas.forbriger's avatar
thomas.forbriger committed
17
18
19
20
21
22
 *
 * ============================================================================
 */

#include "libtime++.h"
#include <cstring>
thomas.forbriger's avatar
thomas.forbriger committed
23
#include <ctime>
thomas.forbriger's avatar
thomas.forbriger committed
24
25
#include <iostream>

26
namespace libtime {
thomas.forbriger's avatar
thomas.forbriger committed
27

thomas.forbriger's avatar
thomas.forbriger committed
28
void TBaseClassTime::char_read(char *timestring)
thomas.forbriger's avatar
thomas.forbriger committed
29
{
thomas.forbriger's avatar
thomas.forbriger committed
30
  if (time_kernel::time_read(&Mtime_Ts, timestring)!=EXIT_SUCCESS)
thomas.forbriger's avatar
thomas.forbriger committed
31
  {
thomas.forbriger's avatar
thomas.forbriger committed
32
    std::cerr << "TBaseClassTime could not initialize time structure "
thomas.forbriger's avatar
thomas.forbriger committed
33
              << "from string:\n" << std::string(timestring) << "\n";
thomas.forbriger's avatar
thomas.forbriger committed
34
35
36
37
    std::abort();
  }
}

thomas.forbriger's avatar
thomas.forbriger committed
38
39
40
41
42
43
44
void TBaseClassTime::string_read(const std::string &timestring)
{
  char charstring[TIME_SLEN+2];
  std::strncpy(charstring, timestring.c_str(), TIME_SLEN+1);
  char_read(charstring);
}

thomas.forbriger's avatar
thomas.forbriger committed
45
46
47
48
49
TAbsoluteTime now()
{
  std::time_t nowtime=std::time(NULL);
  std::tm *nowtm=std::localtime(&nowtime);
  int year=nowtm->tm_year+1900;
thomas.forbriger's avatar
thomas.forbriger committed
50
  int month=nowtm->tm_mon+1;
thomas.forbriger's avatar
thomas.forbriger committed
51
52
53
54
55
56
  int day=nowtm->tm_mday;
  int hour=nowtm->tm_hour;
  int minute=nowtm->tm_min; 
  int second=nowtm->tm_sec;
  TAbsoluteTime thetime(year, month, day, hour, minute, second); 
  return(thetime);
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
} // now()

TRelativeTime::TRelativeTime(const double& seconds):
  TBaseClassTime(0, 0, 0, 0, 0, 0, 0) 
{
  double remain=seconds;
  typedef long int li;
  Mtime_Ts.second=li(seconds);
  remain-=double(Mtime_Ts.second);
  remain*=1.e3;
  Mtime_Ts.milsec=li(remain);
  remain-=double(Mtime_Ts.milsec);
  remain*=1.e3;
  Mtime_Ts.micsec=li(remain);
  norm();
} // TRelativeTime::TRelativeTime(const double& seconds)
thomas.forbriger's avatar
thomas.forbriger committed
73
74

}; // namespace libtime
thomas.forbriger's avatar
thomas.forbriger committed
75
76
 
/* ----- END OF base_constr.cc ----- */