Started a test setup
[AXE.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 std::ostream& operator<<(std::ostream &s, const UTC &t)
88 {
89    s << t.d << " " << t.t;
90
91    return s;
92 }
93
94 std::istream &operator>>(std::istream &s, UTC &t)
95 {
96    s >> t.d >> t.t;
97
98    return s;
99 }
100
101 String UTC::format(const char *fmt)
102 {
103    String  s;
104    char    buf[80];
105    struct  tm  ut;
106
107    ut.tm_sec = t.seconds;
108    ut.tm_min  = t.minutes;
109    ut.tm_hour = t.hours;
110    ut.tm_year = d.year - 1900;
111    ut.tm_mon  = d.month - 1;
112    ut.tm_mday = d.day;
113
114    strftime(buf, 80, fmt, &ut);
115    s = buf;
116
117    return s;
118 }
119