Added UTC add operations.
[libacl.git] / src / utc.cpp
1 /**************************************************************************
2 **  (c) Copyright 1999, Andromeda Technology & Automation
3 ***************************************************************************
4 ** MODULE INFORMATION *
5 ***********************
6 **      FILE NAME      : utc.cpp
7 **      SYSTEM NAME    : AXE - Andromeda X-windows Encapsulation
8 **      VERSION NUMBER : $Revision: 1.1 $
9 **
10 **  DESCRIPTION      :  
11 **
12 **  EXPORTED OBJECTS : 
13 **  LOCAL    OBJECTS : 
14 **  MODULES  USED    :
15 ***************************************************************************
16 **  ADMINISTRATIVE INFORMATION *
17 ********************************
18 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
19 **      CREATION DATE   : Sep 16, 2002
20 **      LAST UPDATE     : Sep 16, 2002
21 **************************************************************************/
22
23 /*****************************
24    $Log: utc.cpp,v $
25    Revision 1.1  2002-09-28 06:58:45  arjen
26    Bugfix: conversion of an empty string to a date or hour object
27    now makes the values of such an object 0 (null) instead of giving
28    a segmentation fault.
29    The class UTC combines the date and hour classes. The most basic
30    functions of the UTC class are now implemented.
31    These include constructors and conversion to and from String objects.
32    New functions: date::proper(), hour::proper() and UTC::proper().
33    Return true if the object holds a proper clock time and/or calendar
34    date; false if at least one value is out of range.
35
36
37 *****************************/
38
39 static const char *RCSID = "$Id: utc.cpp,v 1.1 2002-09-28 06:58:45 arjen Exp $";
40
41 #include <time.h>
42
43 #include "date.h"
44 #include "parsedate.h"
45
46 UTC::UTC(String s)
47 {
48    struct parseddate *pd;
49
50    if (~s == 0)
51    {
52       t.hours   = 0;
53       t.minutes = 0;
54       t.seconds = 0;
55       d.year   = 0;
56       d.month  = 0;
57       d.day    = 0;
58    }
59    else
60    {
61       pd = parsedate(s);
62       t.hours = pd->hour;
63       t.minutes = pd->minute;
64       t.seconds = pd->second;
65       d.year = pd->year;
66       d.month = pd->month;
67       d.day   = pd->day;
68    }
69 }
70
71 UTC Now()
72 {
73    long      clock;
74    struct tm   *tp;
75    date        d;
76    hour        t;
77
78    time(&clock);
79    tp = localtime(&clock);
80    
81    t = hour(tp->tm_hour, tp->tm_min, tp->tm_sec);
82    d = date(tp->tm_mday, tp->tm_mon+1, tp->tm_year+1900);
83
84    return UTC(d, t);
85 }
86
87 UTC operator+(UTC &t1, UTC &t2)
88 {
89    UTC t = t1;
90
91    t.d += t2.d;
92    t.t += t2.t;
93
94    return t;
95 }
96
97 UTC UTC::operator+=(UTC u)
98 {
99    d += u.d;
100    t += u.t;
101
102    return *this;
103 }
104
105 std::ostream& operator<<(std::ostream &s, const UTC &t)
106 {
107    s << t.d << " " << t.t;
108
109    return s;
110 }
111
112 std::istream &operator>>(std::istream &s, UTC &t)
113 {
114    s >> t.d >> t.t;
115
116    return s;
117 }
118
119 String UTC::format(const char *fmt)
120 {
121    String  s;
122    char    buf[80];
123    struct  tm  ut;
124
125    ut.tm_sec = t.seconds;
126    ut.tm_min  = t.minutes;
127    ut.tm_hour = t.hours;
128    ut.tm_year = d.year - 1900;
129    ut.tm_mon  = d.month - 1;
130    ut.tm_mday = d.day;
131
132    strftime(buf, 80, fmt, &ut);
133    s = buf;
134
135    return s;
136 }
137