Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
217 changes: 156 additions & 61 deletions PLAN.md

Large diffs are not rendered by default.

250 changes: 5 additions & 245 deletions src/DayTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
#include "DayTime.hpp"

///////////////////////////////////
// DayTime (and Declination below)
//
// A class to handle hours, minutes, seconds in a unified manner, allowing
// addition of hours, minutes, seconds, other times and conversion to string.
// DayTime — Arduino-dependent overlay methods
// Pure arithmetic is in core::DayTime (src/core/types/DayTime.hpp/.cpp)

// Parses the RA or DEC from a string that has an optional sign, a two digit degree, a seperator, a two digit minute, a seperator and a two digit second.
// Does not correct for hemisphere (derived class Declination takes care of that)
Expand Down Expand Up @@ -59,139 +57,6 @@ DayTime DayTime::ParseFromMeade(String const &s)
return result;
}

DayTime::DayTime()
{
totalSeconds = 0;
}

DayTime::DayTime(const DayTime &other)
{
totalSeconds = other.totalSeconds;
}

DayTime::DayTime(int h, int m, int s)
{
long sgn = sign(h);
h = abs(h);
totalSeconds = sgn * ((60L * h + m) * 60L + s);
}

DayTime::DayTime(float timeInHours)
{
long sgn = fsign(timeInHours);
timeInHours = fabsf(timeInHours);
totalSeconds = sgn * static_cast<long>(roundf(timeInHours * 60.0f * 60.0f));
}

int DayTime::getHours() const
{
int h, m, s;
getTime(h, m, s);
return h;
}

int DayTime::getMinutes() const
{
int h, m, s;
getTime(h, m, s);
return m;
}

int DayTime::getSeconds() const
{
int h, m, s;
getTime(h, m, s);
return s;
}

float DayTime::getTotalHours() const
{
return 1.0f * totalSeconds / 3600.0f;
}

float DayTime::getTotalMinutes() const
{
return 1.0f * totalSeconds / 60.0f;
}

long DayTime::getTotalSeconds() const
{
return totalSeconds;
}

void DayTime::getTime(int &h, int &m, int &s) const
{
long seconds = labs(totalSeconds);

h = (int) (seconds / 3600L);
seconds = seconds - (h * 3600L);
m = (int) (seconds / 60L);
s = (int) (seconds - (m * 60L));

h *= sign(totalSeconds);
}

void DayTime::set(int h, int m, int s)
{
DayTime dt(h, m, s);
totalSeconds = dt.totalSeconds;
checkHours();
}

void DayTime::set(const DayTime &other)
{
totalSeconds = other.totalSeconds;
checkHours();
}

// Add hours, wrapping days (which are not tracked)
void DayTime::addHours(float deltaHours)
{
totalSeconds += long(deltaHours * 3600L);
checkHours();
}

void DayTime::checkHours()
{
while (totalSeconds >= secondsPerDay)
{
totalSeconds -= secondsPerDay;
}

while (totalSeconds < 0)
{
totalSeconds += secondsPerDay;
}
}

// Add minutes, wrapping hours if needed
void DayTime::addMinutes(int deltaMins)
{
totalSeconds += deltaMins * 60;
checkHours();
}

// Add seconds, wrapping minutes and hours if needed
void DayTime::addSeconds(long deltaSecs)
{
totalSeconds += deltaSecs;
checkHours();
}

// Add another time, wrapping seconds, minutes and hours if needed
void DayTime::addTime(const DayTime &other)
{
totalSeconds += other.totalSeconds;
checkHours();
}

// Subtract another time, wrapping seconds, minutes and hours if needed
void DayTime::subtractTime(const DayTime &other)
{
totalSeconds -= other.totalSeconds;
checkHours();
}

char achBuf[32];

// Convert to a standard string (like 14:45:06)
Expand Down Expand Up @@ -245,112 +110,7 @@ const char *DayTime::ToString() const
snprintf(p, remaining, " (%s)", floatStr.c_str());
return achBuf;
}
void DayTime::printTwoDigits(char *achDegs, int num) const
{
achDegs[0] = '0' + (num / 10);
achDegs[1] = '0' + (num % 10);
achDegs[2] = 0;
}

const char *DayTime::formatStringImpl(char *targetBuffer, const char *format, char sgn, long degs, long mins, long secs) const
{
char achDegs[5];
char achMins[3];
char achSecs[3];
const char *f = format;
char *p = targetBuffer;

int i = 0;
if (sgn != '\0')
{
achDegs[0] = sgn;
i++;
}

long absdegs = labs(degs);
if (absdegs >= 100)
{
achDegs[i++] = '0' + min(9L, (absdegs / 100));
absdegs = absdegs % 100;
}

printTwoDigits(achDegs + i, absdegs);
printTwoDigits(achMins, mins);
printTwoDigits(achSecs, secs);

char macro = '\0';
bool inMacro = false;
while (*f)
{
switch (*f)
{
case '{':
{
inMacro = true;
}
break;
case '}':
{
if (inMacro)
{
switch (macro)
{
case '+':
{
*p++ = (degs < 0 ? '-' : '+');
}
break;
case 'd':
{
strcpy(p, achDegs);
p += strlen(achDegs);
}
break;
case 'm':
{
strcpy(p, achMins);
p += 2;
}
break;
case 's':
{
strcpy(p, achSecs);
p += 2;
}
break;
}
inMacro = false;
}
}
break;
default:
{
if (inMacro)
{
macro = *f;
}
else
{
*p++ = *f;
}
}
}
f++;
}

*p = '\0';
return targetBuffer;
}

const char *DayTime::formatString(char *targetBuffer, const char *format, long *pSecs) const
{
long secs = pSecs == nullptr ? totalSeconds : *pSecs;
char sgn = secs < 0 ? '-' : '+';
secs = labs(secs);
long degs = secs / 3600;
secs = secs - degs * 3600;
long mins = secs / 60;
secs = secs - mins * 60;

return formatStringImpl(targetBuffer, format, sgn, degs, mins, secs);
}
// ParseFromMeade, formatString, formatStringImpl, printTwoDigits
// were previously here. formatString/formatStringImpl/printTwoDigits
// are now in core::DayTime (pure).
59 changes: 7 additions & 52 deletions src/DayTime.hpp
Original file line number Diff line number Diff line change
@@ -1,68 +1,23 @@
#pragma once

#include "core/types/DayTime.hpp"

// A class to handle hours, minutes, seconds in a unified manner, allowing
// addition of hours, minutes, seconds, other times and conversion to string.

// Forward declarations
class String;

// DayTime handles a 24-hour time.
class DayTime
/// Thin overlay over core::DayTime that adds Arduino-dependent methods
/// (String-based parsing, formatting, logging).
class DayTime : public core::DayTime
{
protected:
long totalSeconds;

public:
DayTime();

DayTime(const DayTime &other);
DayTime(int h, int m, int s);

// From hours
DayTime(float timeInHours);

int getHours() const;
int getMinutes() const;
int getSeconds() const;
float getTotalHours() const;
float getTotalMinutes() const;
long getTotalSeconds() const;

void getTime(int &h, int &m, int &s) const;
virtual void set(int h, int m, int s);
void set(const DayTime &other);

// Add hours, wrapping days (which are not tracked). Negative or positive.
virtual void addHours(float deltaHours);

// Add minutes, wrapping hours if needed
void addMinutes(int deltaMins);

// Add seconds, wrapping minutes and hours if needed
void addSeconds(long deltaSecs);

// Add time components, wrapping seconds, minutes and hours if needed
void addTime(int deltaHours, int deltaMinutes, int deltaSeconds);

// Add another time, wrapping seconds, minutes and hours if needed
void addTime(const DayTime &other);
// Subtract another time, wrapping seconds, minutes and hours if needed

void subtractTime(const DayTime &other);
// Inherit constructors from core
using core::DayTime::DayTime;

// Convert to a standard string (like 14:45:06)
virtual const char *ToString() const;
virtual const char *formatString(char *targetBuffer, const char *format, long *pSeconds = nullptr) const;

//protected:
virtual void checkHours();

static DayTime ParseFromMeade(String const &s);

protected:
const char *formatStringImpl(char *targetBuffer, const char *format, char sgn, long degs, long mins, long secs) const;
void printTwoDigits(char *achDegs, int num) const;

private:
static long const secondsPerDay = 24L * 3600L; /// Real seconds (not sidereal)
};
Loading