Changeset 2830 in Sophya for trunk/SophyaLib


Ignore:
Timestamp:
Nov 8, 2005, 5:13:56 PM (20 years ago)
Author:
ansari
Message:

Amelioration classe TimeStamp - prise en charge format YYYY-MM-DDThh:mm:ss
et TimeStamp rendu PPersist - Reza 8/11/2005

Location:
trunk/SophyaLib/BaseTools
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaLib/BaseTools/dvlist.cc

    r2826 r2830  
    496496void        ObjFileIO<DVList>::WriteSelf(POutPersist& s) const
    497497{
     498if (dobj == NULL)
     499  throw NullPtrError("ObjFileIO<DVList>::WriteSelf() dobj=NULL");
     500
    498501char buf[1024];
    499502string sfw;
  • trunk/SophyaLib/BaseTools/sophyainit.cc

    r2774 r2830  
    8181  DObjRegister(FIO_NDataBlock< complex<r_8> >, NDataBlock< complex<r_8> >);
    8282
     83  // Enregistrement des handlers PPF pour les TimeStamp
     84  PPRegister(ObjFileIO<TimeStamp>);
     85  DObjRegister(ObjFileIO<TimeStamp>, TimeStamp);
     86
    8387  // Enregistrement des handlers PPF pour les DVList
    8488  PPRegister(ObjFileIO<DVList>);
     
    133137  DObjRegister(PPFWrapperSTLVector< string >, std::vector<string>);
    134138
     139  PPRegister(PPFWrapperSTLVector< TimeStamp >);
     140  DObjRegister(PPFWrapperSTLVector< TimeStamp >, std::vector<TimeStamp>);
     141
    135142
    136143#if (!defined(__GNUG__) && !defined(__MWERKS__) && !defined(HPUX))
  • trunk/SophyaLib/BaseTools/timestamp.cc

    r2826 r2830  
    7474}
    7575
    76 TimeStamp::TimeStamp(string& date, string& hour)
     76TimeStamp::TimeStamp(string const & date, string const & hour)
    7777{
    7878  SetDate(date);
     
    8686}
    8787
     88TimeStamp::TimeStamp(string& datim)
     89{
     90  Set(datim);
     91}
     92
     93TimeStamp::TimeStamp(const char* datim)
     94{
     95  Set(datim);
     96}
    8897
    8998void TimeStamp::Set(TimeStamp const & ts)
     
    96105{
    97106  if (days >= 0.) {
    98     mDays = trunc(days);
     107    mDays = (int_8)trunc(days);
    99108    mSeconds = (days-trunc(days))*86400.;
    100109  }
    101110  else {
    102111    if ( (trunc(days)-days) > 0.) {
    103       mDays = trunc(days)-1;
     112      mDays = (int_8)trunc(days)-1;
    104113      mSeconds = (days-mDays)*86400.;
    105114    }
    106115    else {
    107       mDays = trunc(days);
     116      mDays = (int_8)trunc(days);
    108117      mSeconds = 0.;
    109118    }
     
    116125    throw ParmError("TimeStamp::Set(int_8, r_8) seconds<0 or seconds>86400.");
    117126  mDays = days;
    118 }
     127  mSeconds = seconds;
     128}
     129
     130void TimeStamp::Set(const char * datim)
     131{
     132  int year, month, day;
     133  int hour, min;
     134  double sec;
     135  sscanf(datim,"%d-%d-%dT%d:%d:%lf", &year, &month, &day,
     136         &hour, &min, &sec);
     137  SetDate(year, month, day);
     138  SetHour(hour, min, sec);
     139}
     140
    119141
    120142void TimeStamp::SetNow()
     
    193215{
    194216  double seconds = mSeconds;
    195   hour = trunc(seconds/3600.);
     217  hour = (int)trunc(seconds/3600.);
    196218  seconds -= hour*3600;
    197   min = trunc(seconds/60.);
     219  min = (int)trunc(seconds/60.);
    198220  while (min >= 60) { hour++; min -= 60; }
    199221  sec = seconds-min*60;
     
    207229
    208230/*!
    209 \param fgday : if false, ignore the date (dd/mm/yy) part
    210 \param fghour : if false, ignore the hour (hh:mm:ss) part
     231\param fmt : String format for the date
     232  - FmtPackedDateTime : YYYY-MM-DDThh:mm:ss.s
     233  - FmtDateOnly : dd/mm/yyyy
     234  - FmtTimeOnly : hh:mm:ss.s
     235  - FmtDateTime : dd/mm/yyyy hh:mm:ss.s UT
    211236*/
    212 string TimeStamp::ToString(bool fgday, bool fghour) const
     237string TimeStamp::ToString(StrFmt fmt) const
    213238{
    214239  char buff[128];
     
    218243  GetDate(aa, mm, jj);
    219244  GetHour(hh, min, sec);
    220   if (!fghour)
    221     sprintf(buff,"%02d/%02d/%02d ", jj,mm,aa);
    222   else if (!fgday)
    223     sprintf(buff,"%02d:%02d:%02.3f ", hh,min,sec);
    224   else
    225     sprintf(buff,"%02d/%02d/%02d %02d:%02d:%02.1f GMT", jj,mm,aa,hh,min,sec);
     245  if (fmt == FmtPackedDateTime)
     246    sprintf(buff,"%04d-%02d-%02dT%02d:%02d:%02.1f", aa,mm,jj, hh,min,sec);
     247  else {
     248    if (fmt == FmtDateOnly)
     249      sprintf(buff,"%02d/%02d/%04d ", jj,mm,aa);
     250    else if (fmt == FmtTimeOnly)
     251      sprintf(buff,"%02d:%02d:%02.3f ", hh,min,sec);
     252    else
     253      sprintf(buff,"%02d/%02d/%04d %02d:%02d:%02.1f UT", jj,mm,aa,hh,min,sec);
     254  }
    226255  return buff;
    227256}
    228257
    229258/*!
    230 \param fgday : if false, ignore the date (dd/mm/yy) part
    231 \param fghour : if false, ignore the hour (hh:mm:ss) part
     259\param fmt : String format for the date
     260  - FmtPackedDateTime : YYYY-MM-DDThh:mm:ss.s
     261  - FmtDateOnly : dd/mm/yyyy
     262  - FmtTimeOnly : hh:mm:ss.s
     263  - FmtDateTime : dd/mm/yyyy hh:mm:ss.s UT
    232264*/
    233 void TimeStamp::Print(ostream& os, bool fgday, bool fghour)  const
    234 {
    235   os << " " << ToString(fgday, fghour) << " ";
     265void TimeStamp::Print(ostream& os, StrFmt fmt)  const
     266{
     267  os << " " << ToString(fmt) << " ";
    236268}
    237269
     
    280312  return t;
    281313}
     314
     315//----------------------------------------------------------
     316// Classe pour la gestion de persistance
     317// ObjFileIO<TimeStamp>
     318//----------------------------------------------------------
     319
     320/* --Methode-- */
     321DECL_TEMP_SPEC  /* equivalent a template <> , pour SGI-CC en particulier */
     322void        ObjFileIO<TimeStamp>::WriteSelf(POutPersist& s) const
     323{
     324  if (dobj == NULL)
     325    throw NullPtrError("ObjFileIO<TimeStamp>::WriteSelf() dobj=NULL");
     326  int_4 ver;
     327  ver = 1;
     328  s.Put(ver);   // Lecture numero de version PPF
     329  s.Put(dobj->DaysPart());
     330  s.Put(dobj->SecondsPart());
     331}
     332
     333/* --Methode-- */
     334DECL_TEMP_SPEC  /* equivalent a template <> , pour SGI-CC en particulier */
     335void        ObjFileIO<TimeStamp>::ReadSelf(PInPersist& s)
     336{
     337  int_4 ver;
     338  s.Get(ver);   // Lecture numero de version PPF
     339  r_8 seconds;
     340  int_8 days;
     341  s.Get(days);
     342  s.Get(seconds);
     343
     344  if (dobj == NULL) dobj = new TimeStamp(days, seconds);
     345  else dobj->Set(days, seconds);
     346}
     347
     348
     349#ifdef __CXX_PRAGMA_TEMPLATES__
     350#pragma define_template ObjFileIO<TimeStamp>
     351#endif
     352
     353#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
     354template class ObjFileIO<TimeStamp>;
     355#endif
  • trunk/SophyaLib/BaseTools/timestamp.h

    r2826 r2830  
    1111
    1212#include "machdefs.h"
     13#include "objfio.h"
    1314#include <string>
    1415#include <iostream>
     
    1718 
    1819//! Class representing date and time
    19 class TimeStamp {
     20class TimeStamp : public AnyDataObj {
    2021public:
    2122  /*  Pour utilisation ulterieure
     
    2627  enum Month {month_January=1, month_February, month_March, month_April, month_May, month_June, month_July,
    2728             month_August, month_September, month_October, month_November, month_December}; 
     29
     30  enum StrFmt { FmtPackedDateTime , FmtDateOnly, FmtTimeOnly, FmtDateTime };
    2831
    2932  //! Default Constructor : current date and time (GMT)
     
    3639  //! Constructor with specification of day,month,year,hour,minutes,seconds
    3740  TimeStamp(int year, int month, int day, int hour, int min, double sec);
    38   //! Constructor with specification of date & time in the format YYYY/MM/DD hh:mm:ss
    39   TimeStamp(string& date, string& hour);
     41  //! Constructor with specification of date & time in the format YYYY/MM/DD hh:mm:ss 
     42  TimeStamp(string const & date, string const & hour);
     43  //! Constructor with specification of date & time in the format YYYY/MM/DD hh:mm:ss 
    4044  TimeStamp(const char* date, const char* hour);
     45  //! Constructor with specification of date & time in the format YYYY-MM-DDThh:mm:ss
     46  TimeStamp(string& datim);
     47  //! Constructor with specification of date & time in the format YYYY-MM-DDThh:mm:ss
     48  TimeStamp(const char* datim);
    4149
    4250  //! Set the value of the time stamp copying from "ts"
     
    4654  //! Sets the value of the time stamp from the number of days and seconds
    4755  void Set(int_8 days, r_8 seconds);
     56  //! Sets the value of the time stamp from a string in the format YYYY-MM-DDThh:mm:ss
     57  void Set(const char * datim);
     58 //! Sets the value of the time stamp from a string in the format YYYY-MM-DDThh:mm:ss
     59  inline void Set(string const & datim)  { Set(datim.c_str()); }
    4860  //! initialize with current date and time (GMT)
    4961  void SetNow();
     
    7890
    7991  //! Return the timestamp in string format
    80   string ToString(bool fgday=true, bool fghour=true) const;
     92  string ToString(StrFmt fmt=FmtPackedDateTime) const;
    8193 
    8294  //! Return the integral number of days since 0 Jan 1901
     
    8698
    8799  //! Prints the date/time in string format on \b cout
    88   inline  void    Print(bool fgday=true, bool fghour=true) const 
    89      { Print(cout, fgday, fghour); } 
     100  inline  void    Print(StrFmt fmt=FmtDateTime) const 
     101     { Print(cout, fmt); } 
    90102  //! Prints the date/time in string format on stream \b os
    91   virtual void    Print(ostream& os, bool fgday=true, bool fghour=true)  const;
     103  virtual void    Print(ostream& os, StrFmt fmt=FmtDateTime)  const;
    92104
    93105  //! Number of days in a given month 
     
    108120  {  ts.Print(s);  return(s);  }
    109121
     122/*! Writes the object in the POutPersist stream \b os */
     123inline POutPersist& operator << (POutPersist& os, TimeStamp & obj)
     124{ ObjFileIO<TimeStamp> fio(&obj);  fio.Write(os);  return(os); }
     125/*! Reads the object from the PInPersist stream \b is */
     126inline PInPersist& operator >> (PInPersist& is, TimeStamp & obj)
     127{ ObjFileIO<TimeStamp> fio(&obj); is.SkipToNextObject(); fio.Read(is); return(is); }
     128
    110129} // namespace SOPHYA
    111130
Note: See TracChangeset for help on using the changeset viewer.