Add class xml
[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 #include <time.h>
40
41 #include "date.h"
42 #include "parsedate.h"
43
44 UTC::UTC(String s)
45 {
46    struct parseddate *pd;
47
48    if (~s == 0)
49    {
50       t.hours   = 0;
51       t.minutes = 0;
52       t.seconds = 0;
53       d.year   = 0;
54       d.month  = 0;
55       d.day    = 0;
56    }
57    else
58    {
59       pd = parsedate(s);
60       t.hours = pd->hour;
61       t.minutes = pd->minute;
62       t.seconds = pd->second;
63       d.year = pd->year;
64       d.month = pd->month;
65       d.day   = pd->day;
66    }
67 }
68
69 UTC Now(bool local)
70 {
71    long      clock;
72    struct tm   *tp;
73    date        d;
74    hour        t;
75
76    time(&clock);
77    if (local)
78    {
79       tp = localtime(&clock);
80    }
81    else
82    {
83       tp = gmtime(&clock);
84    }
85    
86    t = hour(tp->tm_hour, tp->tm_min, tp->tm_sec);
87    d = date(tp->tm_mday, tp->tm_mon+1, tp->tm_year+1900);
88
89    return UTC(d, t);
90 }
91
92 UTC operator+(UTC &t1, UTC &t2)
93 {
94    UTC t = t1;
95
96    t.d += t2.d;
97    t.t += t2.t;
98
99    return t;
100 }
101
102 UTC UTC::operator+=(UTC u)
103 {
104    d += u.d;
105    t += u.t;
106
107    return *this;
108 }
109
110 std::ostream& operator<<(std::ostream &s, const UTC &t)
111 {
112    s << t.d << " " << t.t;
113
114    return s;
115 }
116
117 std::istream &operator>>(std::istream &s, UTC &t)
118 {
119    s >> t.d >> t.t;
120
121    return s;
122 }
123
124 String UTC::format(const char *fmt)
125 {
126    String  s;
127    char    buf[80];
128    struct  tm  ut;
129
130    ut.tm_sec = t.seconds;
131    ut.tm_min  = t.minutes;
132    ut.tm_hour = t.hours;
133    ut.tm_year = d.year - 1900;
134    ut.tm_mon  = d.month - 1;
135    ut.tm_mday = d.day;
136
137    strftime(buf, 80, fmt, &ut);
138    s = buf;
139
140    return s;
141 }
142