// $Source: /usr/local/SimSoft/SoftShop/SimTool/SimTime.h,v $
// $Revision: 1.19 $
// $Author: baum $
// $Date: 2001/11/29 15:52:23 $
//
// Login name of the user who locked the revision
// $Locker:  $
// Tag name used to checkout the file
// $Name: Release-20 $
// State assigned to revision
// $State: Exp $
//
//
// $Log: SimTime.h,v $
// Revision 1.19  2001/11/29 15:52:23  baum
// -In Kalender.cpp und Kalender.h Handsteuerung eingebaut
//


#ifndef _SIMTIME_H
#define _SIMTIME_H   1


#include <stdlib.h>
#include <time.h>

#include <iostream.h>

#ifdef WIN32
#include <strstrea.h>
#else
#include <strstream.h>
#endif


#ifdef _SIMTIME_CPP
#  undef _impexp
#  define _impexp __declspec(dllexport)
#elif defined BUILD_SIMTOOL_DLL
#  undef _impexp
#  define _impexp 
#else
#  undef _impexp
#  define _impexp __declspec(dllimport)
#endif

#if defined __GNUG__ || defined __hpux
#  undef _impexp
#  define _impexp
#endif


// these defines help to maintain readability (+ security)
#define WEEKDAYNAMEFW	7
#define MONTHNAMEFW	12


class _impexp SimTime
  {
  public:
    // constructors
    SimTime ();
    SimTime (const SimTime&);

    SimTime (int secs, int fracs);
    SimTime (double secs);
    SimTime (int day, int shift, int hour,
	     int minutes = 0, int seconds = 0, int fracs = 0);

    // destructor
    ~SimTime ();

    // assignment operator
    SimTime& operator = (const SimTime&);

    // comparison operators
    bool operator == (const SimTime&) const;
    bool operator != (const SimTime&) const;

    // relational operators
    bool operator <  (const SimTime&) const;
    bool operator <= (const SimTime&) const;
    bool operator >  (const SimTime&) const;
    bool operator >= (const SimTime&) const;

    // arithmetic operators
    SimTime  operator +  (const SimTime&) const;
    SimTime& operator += (const SimTime&);

    SimTime  operator -  (const SimTime&) const;
    SimTime& operator -= (const SimTime&);

    SimTime  operator *  (const double&) const;
    SimTime& operator *= (const double&);

    SimTime  operator /  (const double&) const;
    SimTime& operator /= (const double&);

    // inversion operator (unary -)
    SimTime operator - () const;

    // extracting parts of a SimTime
    int  Fracs () const;
    int  Seconds () const;
    int  Minutes () const;
    int  Hours () const;
    bool isAM () const;
    int  AMorPMHour () const;
    int  Shifts () const;
    int  Days () const;
    int  WeekDay () const;	// 1 ... DaysInWeek (Monday ... ?Sunday?)
    int  Week () const;		// 0 ... 53
    int  MonthDay () const;	// 1 ... 31
    int  Month () const;	// 1 ... 12
    int  YearDay () const;	// 1 ... 366
    int  Year () const;		// 1970 ...

    // conversion to other bases
    double inFracs () const;
    double inSeconds () const;
    double inMinutes () const;
    double inHours () const;
    double inShifts () const;
    double inDays () const;

    // making SimTimes from bases other than seconds
    static SimTime convertMinutes (double minutes);
    static SimTime convertHours   (double hours);

    // the following functions allow interaction with the system's time & date
    static SimTime convertSysClock ();
    static SimTime converttime_t (time_t);
    static SimTime convertstructtm (const struct tm&);
    static SimTime convertSysDate (int mday, int month, int year,
				   int hour =0, int minute =0, int second =0);

    // read a formatted SimTime from a stream
    istream& read (istream&, istrstream& format);
    istream& read (istream&, const char* format);

    // insert a formatted SimTime into a stream
    ostream& print (ostream&, istrstream& format) const;
    ostream& print (ostream&, const char* format) const;

    // write a SimTime into s; much like the C-stdlib-function strftime (...)
    size_t strftime (char* s, size_t maxlen, const char* format) const;

    // yielding and/or modifying the Standard-Formats
    static const char* setInputFormat (const char* newFormat);
    static const char* InputFormat ();
    static const char* setOutputFormat (const char* newFormat);
    static const char* OutputFormat ();

    // yielding the statics controlling external representation
    static int FracsPerSecond ();
    static int SecondsPerMinute ();
    static int SecondsPerHour ();
    static int SecondsPerShift ();
    static int SecondsPerDay ();
    static int DaysPerWeek ();

    // setting things up
    static void setup ();

    // cleaning things up
    static void cleanup ();

    static bool isInitialized ();

  private:
    // adjusting after modification of data members
    SimTime& adjust ();

    // these are used as a base for others
    static int SpecializedDays (int);
    int NormalizedDays () const;
    int YearsSince1970 () const;

    // data members
    int ObjSec;
    int ObjFrac;

    // static data to control the external representation
    static int ClassFracsPerSec;		// 1 ... 1 000 000 000
    static int ClassSecsPerMinute;		// 1 ... 86400
    static int ClassSecsPerHour;		// 1 ... 86400
    static int ClassSecsPerShift;		// 1 ... 86400
    static int ClassSecsPerDay;		// 1 ... 86400
    static int ClassHoursPerHalfDay;
    static int ClassDaysInWeek;		// 1 ... 7
    static int ClassDayNoOf5Jan70;
    static char* ClassInFormat;
    static char* ClassOutFormat;
    static char* ClassWeekDayName [WEEKDAYNAMEFW];
    static int ClassWeekDayNameAbbrLen;	// 1 ... 3
    static char* ClassMonthName [MONTHNAMEFW];
    static int ClassMonthNameAbbrLen;	// 1 ... 3
    static int ClassFracLen;
    static bool ClassIsInitialized;
  };	// class _impexp SimTime


// SimTime-output to a stream (simply calls print (...))
_impexp ostream& operator << (ostream&, const SimTime&);

// SimTime-input from a stream (simply calls read (...))
_impexp istream& operator >> (istream&, SimTime&);


#endif	// !defined _SIMTIME_H

//	$Filename: C:\Software\SoftShop\SimTool\SimTime.h $	ends
