summaryrefslogtreecommitdiffstats
path: root/sources/time.cpp
blob: d8622eafe3487ca96cf76155f98727ed49896ea1 (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//-*- mode: c++; indent-tabs-mode: t; coding: utf-8; show-trailing-whitespace: t -*-

// file time.cpp

#include "time.hpp"

extern long int spycom::nKWTSpy;

namespace date_time {

  std::clock_t nCPUTime;

  bool date44(std::string *a)
  {
    // The purpose of subroutine  DATE44  is to interrogate the
    // installation calendar, and return the current date through the
    // argument of the subroutine.   Eight BCD characters are allowed,
    // with the first (left) four characters to be placed in a(1) ,
    // and the final (right) four placed in a(2) .    A statement like
    //          write (lunit6, 4041)  a
    //     4041 format ( 1x, 2a4 )
    // Thus outputs the current date as first the month, then the day,
    // and finally the year, separated by slashes (MM/DD/YY) .
    // subroutine  DATE44  is of course installation dependent.
    // European (or generally non-United-States, perhaps) users of this
    // program may want to reverse the order of appearance of the month
    // and the day, in conformity with established european usage.
    // Installation-dependent  EMTP  module written for the  DEC
    // VAX-11/780.    'IDATE'  is a  DEC  system subroutine which
    // returns the month, day, and year (of century) as three integer*2
    // numerical values.
    bool bReturnValue = false;
    int year, decade;
    std::stringstream ss;
    std::time_t sCurrentTime;
    std::tm *pLocalTime;

    if(a) {
      sCurrentTime = std::time(NULL);
      pLocalTime = std::localtime(&sCurrentTime);
      if(pLocalTime) {
	ss << std::setfill('0') << std::setw(2) << (1 + pLocalTime -> tm_mon);
	ss << ".";
	ss << std::setfill('0') << std::setw(2) << pLocalTime -> tm_mday;
	ss << ".";
	year = pLocalTime -> tm_year + 1900;
	decade = 100 * (((float) year) / 100.0 - (float) floor (year / 100.0));
	ss << std::setfill('0') << std::setw(2) << decade;
	a[ 0 ] = ss.str().substr(0, 4);
	a[ 1 ] = ss.str().substr(4, 8);
	bReturnValue = true;
      }
    }
    return bReturnValue;
  }

  bool time44(std::string *a)
  {
    // The purpose of subroutine  TIME44  is to interrogate the
    // installation clock, and return the wall-clock time through the
    // argument of the subroutine.   Eight BCD characters are allowed,
    // with the first (left) four characters to be placed in a(1) ,
    // and the final (right) four placed in a(2) .    A statement like
    //          write (lunit6, 4041)  a
    //     4041 format ( 1x, 2a4 )
    // Thus outputs the wall-clock time as first hours, then minutes,
    // and finally seconds, separated by periods (HH.MM.SS) .
    // subroutine  TIME44  is of course installation dependent.
    // Installation-dependent  EMTP  module written for the  DEC
    // VAX-11/780.    'TIME'  is a  DEC  system subroutine which
    // returns the wall-clock time as an 8-byte character string.
    // This is just what the EMTP needs, except that we want periods
    // rather than colons, and of course we require  2a4  format.
    bool bReturnValue = false;
    std::stringstream ss;
    std::time_t sCurrentTime;
    std::tm *pLocalTime;

    if(a) {
      sCurrentTime = std::time(NULL);
      pLocalTime = std::localtime(&sCurrentTime);
      if(pLocalTime) {
	ss << std::setfill('0') << std::setw(2) << pLocalTime -> tm_hour;
	ss << ".";
	ss << std::setfill('0') << std::setw(2) << pLocalTime -> tm_min;
	ss << ".";
	ss << std::setfill('0') << std::setw(2) << pLocalTime -> tm_sec;
	a[ 0 ] = ss.str().substr(0, 4);
	a[ 1 ] = ss.str().substr(4, 8);
	bReturnValue = true;
      }
    }
    return bReturnValue;
  }

  void settym(void)
  {
    // VAX-11  Installation-dependent EMTP module.
    // Called only by VAX "RUNTYM"; destroy for other computers.
    // Include  '[SCOTT]COMMUK.FOR' --- share with "RUNTYM" in-line:
    date_time::nCPUTime = std::clock ();
  }

  void runtym(double &d1, double &d2)
  {
    // This subroutine returns with the current job-execution time, as
    // broken down into two categories ....
    //        d1 = central processor job time, in seconds
    //        d2 = peripheral processor (or input/output) job time,
    //             in seconds.
    // If two such figures are not available ont the user's computre,
    // 'd2'  should be set to zero so that case-summary statistics
    // will not print out garbage values.   Such is the only use of
    // the values gotten by calling this subroutine ---- for the case-
    // summary printout.   Hence if one wants to convert time into
    // dollars, or some other measure of job effort, it is easily done.
    // Include  '[SCOTT]COMMUK.FOR' --- share with "SETTYM" in-line:
    std::clock_t nNowCPUTime;
    //
    nNowCPUTime = std::clock ();
    d1 = std::difftime (date_time::nCPUTime, nNowCPUTime) / CLOCKS_PER_SEC;
    d2 = 0.0;
  }

  // subroutine tdelay.

  void tdelay(int &d8)
  {
    // Module of interactive EMTP only, which services "emtspy".
    // If no interactive use, this module can be deleted.
    // VAX-11   module designed to stall for  d8  seconds.
    // Present use of disk writes is temporary only, and should
    // later be replaced by a less-wasteful, true hibernation.
    clock_t sNow = clock();
    //
    int nDelay;                                 // integer number of seconds for hibernation
    //
    nDelay = d8 * CLOCKS_PER_SEC;
    while((clock() - sNow) < nDelay) {          // loop once for each second of delay
      utilities::quiter();                      // check for user-keyed interrupt signal
      if (spycom::nKWTSpy == 0) {               // no user abort of alarm
	;
      } else {
	spycom::nKWTSpy = 0;                    // reset interrupt indicator as we begin service
	d8 = -7654;                             // argument flag remembering abort (for honker)
	break;                                  // jump out of time-delay loop, to return
      }
    }
  }

}

// end of file time.cpp