Add parameter localtime in today() and now()
[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(bool local)
72 {
73    long      clock;
74    struct tm   *tp;
75    date        d;
76    hour        t;
77
78    time(&clock);
79    if (local)
80    {
81       tp = localtime(&clock);
82    }
83    else
84    {
85       tp = gmtime(&clock);
86    }
87    
88    t = hour(tp->tm_hour, tp->tm_min, tp->tm_sec);
89    d = date(tp->tm_mday, tp->tm_mon+1, tp->tm_year+1900);
90
91    return UTC(d, t);
92 }
93
94 UTC operator+(UTC &t1, UTC &t2)
95 {
96    UTC t = t1;
97
98    t.d += t2.d;
99    t.t += t2.t;
100
101    return t;
102 }
103
104 UTC UTC::operator+=(UTC u)
105 {
106    d += u.d;
107    t += u.t;
108
109    return *this;
110 }
111
112 std::ostream& operator<<(std::ostream &s, const UTC &t)
113 {
114    s << t.d << " " << t.t;
115
116    return s;
117 }
118
119 std::istream &operator>>(std::istream &s, UTC &t)
120 {
121    s >> t.d >> t.t;
122
123    return s;
124 }
125
126 String UTC::format(const char *fmt)
127 {
128    String  s;
129    char    buf[80];
130    struct  tm  ut;
131
132    ut.tm_sec = t.seconds;
133    ut.tm_min  = t.minutes;
134    ut.tm_hour = t.hours;
135    ut.tm_year = d.year - 1900;
136    ut.tm_mon  = d.month - 1;
137    ut.tm_mday = d.day;
138
139    strftime(buf, 80, fmt, &ut);
140    s = buf;
141
142    return s;
143 }
144