ReFRACtor
fp_time.cc
Go to the documentation of this file.
1 #include "fp_time.h"
2 #include <cmath>
3 
4 using namespace FullPhysics;
5 using namespace boost::posix_time;
6 using namespace boost::gregorian;
7 
8 //-----------------------------------------------------------------------
10 //-----------------------------------------------------------------------
11 
12 Time::Time(const boost::posix_time::ptime& t)
13 {
14  const ptime epoch(date(1970,1,1), time_duration(0,0,0,0));
15  unix_time_ = (t - epoch).ticks() /
16  ((double) time_duration::ticks_per_second());
17 }
18 
19 //-----------------------------------------------------------------------
21 //-----------------------------------------------------------------------
22 
23 Time::operator boost::posix_time::ptime() const
24 {
25  return ptime(date(1970,1,1),
26  time_duration(0,0,0,
27  floor(unix_time() * time_duration::ticks_per_second() + 0.5)));
28 }
29 
30 
31 //-----------------------------------------------------------------------
35 //-----------------------------------------------------------------------
36 
37 double Time::frac_day_of_year() const
38 {
39  ptime pt(*this);
40  return pt.date().day_of_year() + pt.time_of_day().total_microseconds() /
41  (24 * 60 * 60 * 1e6);
42 }
43 
44 //-----------------------------------------------------------------------
47 //-----------------------------------------------------------------------
48 
49 double Time::frac_year() const
50 {
51  ptime pt(*this);
52  // Fractional part of the year. Do it this way to account for leap years
53  // Use total_microseconds for greater precision
54  double frac = double(( pt - ptime(date(pt.date().year(), 1, 1)) ).total_microseconds()) /
55  double(( ptime(date(pt.date().year(), 12, 31)) - ptime(date(pt.date().year(), 1, 1)) ).total_microseconds());
56  return pt.date().year() + frac;
57 
58 }
59 
60 //-----------------------------------------------------------------------
62 //-----------------------------------------------------------------------
63 
64 Time Time::parse_time(const std::string& Time_string)
65 {
66  std::string tstring(Time_string);
67 
68  // boost::posix_time::time_from_string can't directly read CCSDS
69  // format strings, but it can read something very close to
70  // this. Massage the string into a format that time_from_string can
71  // read.
72  if(tstring.find('T') != std::string::npos)
73  tstring[tstring.find('T')] = ' ';
74  tstring = tstring.substr(0, tstring.find('Z'));
75  return Time(boost::posix_time::time_from_string(tstring));
76 }
77 
78 //-----------------------------------------------------------------------
80 //-----------------------------------------------------------------------
81 
82 std::string Time::to_string() const
83 {
84  std::string t = to_iso_extended_string(ptime(*this));
85  // ptime doesn't write exactly CCSDS format. Massage this to make it
86  // the right format.
87  return t + "Z";
88 }
Time()
Default constructor.
Definition: fp_time.h:40
static Time parse_time(const std::string &Time_string)
Parse CCSDS format time (e.g., "1996-07-03T04:13:57.987654Z")
Definition: fp_time.cc:64
double frac_year() const
Calculate the fractional year.
Definition: fp_time.cc:49
std::string to_string() const
Convert to CCSDS format.
Definition: fp_time.cc:82
double frac_day_of_year() const
Calculate the fractional day of the year.
Definition: fp_time.cc:37
This is a simple time class.
Definition: fp_time.h:29
Contains classes to abstract away details in various Spurr Radiative Transfer software.
Definition: doxygen_python.h:1

Copyright © 2017, California Institute of Technology.
ALL RIGHTS RESERVED.
U.S. Government Sponsorship acknowledged.
Generated Fri Aug 24 2018 15:44:11