Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / zotnet / tws / phoon / deltime.c
1 /* deltime.c - subtract date/times
2
3 ver  date   who remarks
4 --- ------- --- -------------------------------------------------------------
5 01B 15nov86 JP  Modified to use twsubtract().
6 01A 08nov86 JP  Written.
7
8 Copyright (C) 1986 by Jef Poskanzer.  Permission to use, copy,
9 modify, and distribute this software and its documentation for any
10 purpose and without fee is hereby granted, provided that this copyright
11 notice appear in all copies and in all supporting documentation.  No
12 representation is made about the suitability of this software for any
13 purpose.  It is provided "as is" without express or implied warranty.
14
15 */
16
17 static char copyright[] = "\nCopyright (C) 1986 by Jef Poskanzer.\n";
18
19
20 #include "tws.h"
21 #include <stdio.h>
22
23 #define SECSPERMINUTE 60
24 #define SECSPERHOUR (60 * SECSPERMINUTE)
25 #define SECSPERDAY (24 * SECSPERHOUR)
26
27 main( argc, argv )
28 int argc;
29 char *argv[];
30     {
31     struct tws tws1, tws2, *twp;
32     long delta, days, hours, minutes, secs;
33     char *illdt = "illegal date/time: %s\n";
34
35     if ( argc == 2 )
36         {
37         twp = dparsetime( argv[1] );
38         if ( twp == NULL || twp -> tw_flags & TW_JUNK )
39             {
40             fprintf( stderr, illdt, argv[1] );
41             exit( 1 );
42             }
43         twscopy( &tws1, twp );
44         twscopy( &tws2, dtwstime( ) );
45         }
46     else if ( argc == 3 )
47         {
48         twp = dparsetime( argv[1] );
49         if ( twp == NULL || twp -> tw_flags & TW_JUNK )
50             {
51             fprintf( stderr, illdt, argv[1] );
52             exit( 1 );
53             }
54         twscopy( &tws1, twp );
55         twp = dparsetime( argv[2] );
56         if ( twp == NULL || twp -> tw_flags & TW_JUNK )
57             {
58             fprintf( stderr, illdt, argv[2] );
59             exit( 1 );
60             }
61         twscopy( &tws2, twp );
62         }
63     else
64         {
65         fprintf( stderr, "usage:  %s  <time>  [ <time2> ]\n", argv[0] );
66         exit( 1 );
67         }
68     
69     delta = twsubtract( &tws2, &tws1 );
70     if ( delta < 0 )
71         {
72         printf( "-" );
73         delta = -delta;
74         }
75
76     days = delta / SECSPERDAY;
77     delta = delta - days * SECSPERDAY;
78     hours = delta / SECSPERHOUR;
79     delta = delta - hours * SECSPERHOUR;
80     minutes = delta / SECSPERMINUTE;
81     delta = delta - minutes * SECSPERMINUTE;
82     secs = delta;
83
84     printf( "%ld %2ld:%02ld:%02ld\n", days, hours, minutes, secs );
85
86     exit( 0 );
87     }