Ignore:
Timestamp:
02/14/04 11:48:43 (21 years ago)
Author:
tbretz
Message:
*** empty log message ***
Location:
trunk/MagicSoft/Mars/mbase
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Mars/mbase/MTime.cc

    r3138 r3148  
    2929// A generalized MARS time stamp.
    3030//
     31//
     32// We do not use floating point values here, because of several reasons:
     33//  - having the times stored in integers only is more accurate and
     34//    more reliable in comparison conditions
     35//  - storing only integers gives similar bit-pattern for similar times
     36//    which makes compression (eg gzip algorithm in TFile) more
     37//    successfull
     38//
     39// Note, that there are many conversion function converting the day time
     40// into a readable string. Also a direct interface to SQL time strings
     41// is available.
     42//
     43// If you are using MTime containers as axis lables in root histograms
     44// use GetAxisTime(). Make sure that you use the correct TimeFormat
     45// on your TAxis (see GetAxisTime())
     46//
     47//
    3148// WARNING: Be carefull changing this class. It is also used in the
    3249//          MAGIC drive software cosy!
     
    3451// Remarke: If you encounter strange behaviour, check the casting.
    3552//          Note, that on Linux machines ULong_t and UInt_t is the same.
     53//
    3654//
    3755// Version 1:
     
    5270
    5371#include <iomanip>
     72
     73#include <time.h>     // struct tm
    5474#include <sys/time.h> // struct timeval
    5575
     
    6585const UInt_t MTime::kHour = 3600000;         // [ms] one hour
    6686const UInt_t MTime::kDay  = MTime::kHour*24; // [ms] one day
     87
     88// --------------------------------------------------------------------------
     89//
     90// Constructor. Calls SetMjd(d) for d>0 in all other cases the time
     91// is set to the current UTC time.
     92//
     93MTime::MTime(Double_t d)
     94{
     95    Init(0, 0);
     96    if (d<=0)
     97        Now();
     98    else
     99        SetMjd(d);
     100}
    67101
    68102// --------------------------------------------------------------------------
     
    192226
    193227    Set(mjd, tm, ms*1000);
     228}
     229
     230// --------------------------------------------------------------------------
     231//
     232// Return contents as a TString of the form:
     233//   "dd.mm.yyyy hh:mm:ss.fff"
     234//
     235Bool_t MTime::SetString(const char *str)
     236{
     237    if (!str)
     238        return kFALSE;
     239
     240    UInt_t y, mon, d, h, m, s, ms;
     241    const Int_t n = sscanf(str, "%02u.%02u.%04u %02u:%02u:%02u.%03u",
     242                           &d, &mon, &y, &h, &m, &s, &ms);
     243
     244    return n==7 ? Set(y, mon, d, h, m, s, ms) : kFALSE;
     245}
     246
     247// --------------------------------------------------------------------------
     248//
     249// Return contents as a TString of the form:
     250//   "yyyy-mm-dd hh:mm:ss"
     251//
     252Bool_t MTime::SetSqlDateTime(const char *str)
     253{
     254    if (!str)
     255        return kFALSE;
     256
     257    UInt_t y, mon, d, h, m, s;
     258    const Int_t n = sscanf(str, "%04u-%02u-%02u %02u:%02u:%02u",
     259                           &y, &mon, &d, &h, &m, &s);
     260
     261    return n==6 ? Set(y, mon, d, h, m, s) : kFALSE;
     262}
     263
     264// --------------------------------------------------------------------------
     265//
     266// Return contents as a TString of the form:
     267//   "yyyymmddhhmmss"
     268//
     269Bool_t MTime::SetSqlTimeStamp(const char *str)
     270{
     271    if (!str)
     272        return kFALSE;
     273
     274    UInt_t y, mon, d, h, m, s;
     275    const Int_t n = sscanf(str, "%04u%02u%02u%02u%02u%02u",
     276                           &y, &mon, &d, &h, &m, &s);
     277
     278    return n==6 ? Set(y, mon, d, h, m, s) : kFALSE;
    194279}
    195280
     
    255340//
    256341// Return contents as a TString of the form:
    257 //   "yyyy/mm/dd hh:mm:ss.fff"
     342//   "dd.mm.yyyy hh:mm:ss.fff"
    258343//
    259344TString MTime::GetString() const
     
    265350    GetTime(h, m, s, ms);
    266351
    267     return TString(Form("%4d/%02d/%02d %02d:%02d:%02d.%03d", y, mon, d, h, m, s, ms));
     352    return TString(Form("%02d.%02d.%04d %02d:%02d:%02d.%03d", d, mon, y, h, m, s, ms));
     353}
     354
     355// --------------------------------------------------------------------------
     356//
     357// Return contents as a string format'd with strftime:
     358// Here is a short summary of the most important formats. For more
     359// information see the man page (or any other description) of
     360// strftime...
     361//
     362//  %a  The abbreviated weekday name according to the current locale.
     363//  %A  The full weekday name according to the current locale.
     364//  %b  The abbreviated month name according to the current locale.
     365//  %B  The full month name according to the current locale.
     366//  %c  The preferred date and time representation for the current locale.
     367//  %d  The day of the month as a decimal number (range  01 to 31).
     368//  %e  Like %d, the day of the month as a decimal number,
     369//      but a leading zero is replaced by a space.
     370//  %H  The hour as a decimal number using a 24-hour clock (range 00 to 23)
     371//  %k  The hour (24-hour clock) as a decimal number (range 0 to 23);
     372//      single digits are preceded by a blank.
     373//  %m  The month as a decimal number (range 01 to 12).
     374//  %M  The minute as a decimal number (range 00 to 59).
     375//  %R  The time in 24-hour notation (%H:%M).  For a
     376//      version including the seconds, see %T below.
     377//  %S  The second as a decimal number (range 00 to 61).
     378//  %T  The time in 24-hour notation (%H:%M:%S).
     379//  %x  The preferred date representation for the current
     380//      locale without the time.
     381//  %X  The preferred time representation for the current
     382//      locale without the date.
     383//  %y  The year as a decimal number without a century (range 00 to 99).
     384//  %Y  The year as a decimal number including the century.
     385//  %+  The date and time in date(1) format.
     386//
     387// The default is: Tuesday 16.February 2004 12:17:22
     388//
     389// The maximum size of the return string is 128 (incl. NULL)
     390//
     391TString MTime::GetStringFmt(const char *fmt) const
     392{
     393    if (!fmt)
     394        fmt = "%A %e.%B %Y %H:%M:%S";
     395
     396    UShort_t y, ms;
     397    Byte_t mon, d, h, m, s;
     398
     399    GetDate(y, mon, d);
     400    GetTime(h, m, s, ms);
     401
     402    struct tm time;
     403    time.tm_sec   = s;
     404    time.tm_min   = m;
     405    time.tm_hour  = h;
     406    time.tm_mday  = d;
     407    time.tm_mon   = mon-1;
     408    time.tm_year  = y-1900;
     409    time.tm_isdst = 0;
     410
     411    // recalculate tm_yday and tm_wday
     412    mktime(&time);
     413
     414    char ret[128];
     415    return TString(strftime(ret, 127, fmt, &time) ? ret : "");
     416}
     417
     418// --------------------------------------------------------------------------
     419//
     420// Return contents as a TString of the form:
     421//   "yyyy-mm-dd hh:mm:ss"
     422//
     423TString MTime::GetSqlDateTime() const
     424{
     425    return GetStringFmt("%Y-%m-%d %H:%M:%S");
     426}
     427
     428// --------------------------------------------------------------------------
     429//
     430// Return contents as a TString of the form:
     431//   "yyyymmddhhmmss"
     432//
     433TString MTime::GetSqlTimeStamp() const
     434{
     435    return GetStringFmt("%Y%m%d%H%M%S");
    268436}
    269437
     
    275443TString MTime::GetFileName() const
    276444{
    277     UShort_t y;
    278     Byte_t mon, d, h, m, s;
    279 
    280     GetDate(y, mon, d);
    281     GetTime(h, m, s);
    282 
    283     return TString(Form("%04d%02d%02d_%02d%02d%02d", y, mon, d, h, m, s));
     445    return GetStringFmt("%Y%m%d_%H%M%S");
    284446}
    285447
  • trunk/MagicSoft/Mars/mbase/MTime.h

    r3138 r3148  
    5656        Set(tm);
    5757    }
     58    MTime(Double_t d);
    5859    MTime(const MTime& t) : fMjd(t.fMjd), fTime(t.fTime), fNanoSec(t.fNanoSec)
    5960    {
     
    7778    Bool_t   Set(UShort_t y, Byte_t m, Byte_t d, Byte_t h=13, Byte_t min=0, Byte_t s=0, UShort_t ms=0, UInt_t ns=0);
    7879    void     Set(const struct timeval &tv);
     80    Bool_t   SetString(const char *str);
     81    Bool_t   SetSqlDateTime(const char *str);
     82    Bool_t   SetSqlTimeStamp(const char *str);
    7983    void     SetCT1Time(UInt_t mjd, UInt_t t1, UInt_t t0);
    8084    Bool_t   UpdMagicTime(Byte_t h, Byte_t m, Byte_t s, UInt_t ns);
     
    8286    Double_t GetMjd() const;
    8387    TString  GetString() const;
     88    TString  GetStringFmt(const char *fmt=0) const;
     89    TString  GetSqlDateTime() const;
     90    TString  GetSqlTimeStamp() const;
    8491    TString  GetFileName() const;
    8592    void     GetDate(UShort_t &y, Byte_t &m, Byte_t &d) const;
Note: See TracChangeset for help on using the changeset viewer.