📄 aiptime.cpp
字号:
long aipTime::seconds_since (const aipTime& x) const {
long nsec = (long)difftime (m_time, x.tt_time());
return nsec;
}
//----------------------------------------------------------------------
// return the number of minutes since x
long aipTime::minutes_since (const aipTime& x) const {
return (seconds_since(x) / 60);
}
//----------------------------------------------------------------------
// return the number of hours since x
long aipTime::hours_since (const aipTime& x) const {
return (seconds_since(x) / (60 * 60));
}
//----------------------------------------------------------------------
// return the number of days since x
long aipTime::days_since (const aipTime& x) const {
return (seconds_since(x) / (60 * 60 * 24));
}
//----------------------------------------------------------------------
// set the whole days, hours, minutes and seconds since x
void aipTime::time_since (const aipTime& x,
long *pdays, long *phrs,
long *pmins, long *psecs) const {
long nsec = seconds_since(x);
const long sec_per_min = 60;
const long sec_per_hr = 60 * 60;
const long sec_per_day = 60 * 60 * 24;
long days = 0;
if (nsec >= sec_per_day) {
days = nsec / sec_per_day;
nsec -= (days * sec_per_day);
}
long hrs = 0;
if (nsec >= sec_per_hr) {
hrs = nsec / sec_per_hr;
nsec -= (hrs * sec_per_hr);
}
long mins = 0;
if (nsec >= sec_per_min) {
mins = nsec / sec_per_min;
nsec -= (mins * sec_per_min);
}
if (pdays) *pdays = days;
if (phrs) *phrs = hrs;
if (pmins) *pmins = mins;
if (psecs) *psecs = nsec;
}
//----------------------------------------------------------------------
// return the difference in hours after x
long aipTime::hours_after (const aipTime& x) const {
long yr, mon, day, hr;
get (&yr, &mon, &day, &hr);
aipTime a(yr, mon, day, hr, 0, 0);
x.get (&yr, &mon, &day, &hr);
aipTime b(yr, mon, day, hr, 0, 0);
return a.hours_since(b);
}
//----------------------------------------------------------------------
// return the difference in days after x
long aipTime::days_after (const aipTime& x) const {
long yr, mon, day;
get (&yr, &mon, &day);
aipTime a(yr, mon, day, 0, 0, 0);
x.get (&yr, &mon, &day);
aipTime b(yr, mon, day, 0, 0, 0);
return a.days_since(b);
}
//----------------------------------------------------------------------
// get components for non-null pointers - return zero on failure
int aipTime::get (long *a_year, long *a_mon, long *a_day,
long *a_hour, long *a_min, long *a_sec,
char *s_mon, char *s_weekday) const {
if (!is_valid()) {
if (a_year) *a_year = 0;
if (a_mon) *a_mon = 0;
if (a_day) *a_day = 0;
if (a_hour) *a_hour = 0;
if (a_min) *a_min = 0;
if (a_sec) *a_sec = 0;
if (s_mon) s_mon[0] = '\0';
if (s_weekday) s_weekday[0] = '\0';
return 0;
}
struct tm *ts = localtime (&m_time);
if (a_year) *a_year = ts->tm_year + 1900;
if (a_mon) *a_mon = ts->tm_mon + 1;
if (a_day) *a_day = ts->tm_mday;
if (a_hour) *a_hour = ts->tm_hour;
if (a_min) *a_min = ts->tm_min;
if (a_sec) *a_sec = ts->tm_sec;
if (s_mon) aip_strncpy(s_mon, aip_Month_Abbrv[ts->tm_mon], 3);
if (s_weekday) aip_strncpy(s_weekday,
aip_Day_Abbrv[ts->tm_wday], 3);
return 1;
}
//----------------------------------------------------------------------
// write the date to dstr - ex: "17/Jan/2008"
void aipTime::dd_mon_yyyy (char *dstr) const {
if (!dstr) return;
long dyear, dmon, dday;
if ( ! get(&dyear,&dmon,&dday) ) { *dstr='\0'; return; }
const char *smon = aip_str_month(dmon);
sprintf (dstr, "%02ld/%s/%ld", dday, smon, dyear);
}
//----------------------------------------------------------------------
// write the date to dstr - ex: "17/Jan/2008_16:30"
void aipTime::dd_mon_yyyy_hh_mm (char *dstr) const {
if (!dstr) return;
long dyear, dmon, dday, dhour, dmin;
if ( ! get(&dyear,&dmon,&dday,&dhour,&dmin) ) { *dstr='\0'; return; }
const char *smon = aip_str_month(dmon);
sprintf (dstr, "%02ld/%s/%ld_%02ld:%02ld",
dday, smon, dyear, dhour, dmin);
}
//----------------------------------------------------------------------
// write the date to dstr - ex: "01/17/08"
void aipTime::dd_mm_yy (char *dstr) const {
if (!dstr) return;
long dyear, dmon, dday;
if ( ! get(&dyear,&dmon,&dday) ) { *dstr='\0'; return; }
sprintf (dstr, "%02ld/%02ld/%02ld", dday, dmon, dyear%100);
}
//----------------------------------------------------------------------
// write the date to dstr - ex: "01/17/2008"
void aipTime::dd_mm_yyyy (char *dstr) const {
if (!dstr) return;
long dyear, dmon, dday;
if ( ! get(&dyear,&dmon,&dday) ) { *dstr='\0'; return; }
sprintf (dstr, "%02ld/%02ld/%4ld", dday, dmon, dyear);
}
//----------------------------------------------------------------------
// write the date to dstr (insert-format) - ex: "2008-01-17"
void aipTime::yyyy_mm_dd (char *dstr) const {
if (!dstr) return;
long dyear, dmon, dday;
if ( ! get(&dyear,&dmon,&dday) ) { *dstr='\0'; return; }
sprintf (dstr, "%04ld-%02ld-%2ld", dyear, dmon, dday);
}
//----------------------------------------------------------------------
// return the date as a long
long aipTime::yyyymmdd () const {
long dyear, dmon, dday;
if ( ! get(&dyear,&dmon,&dday) ) return 0;
return ( dyear*10000 + dmon*100 + dday );
}
//----------------------------------------------------------------------
// return the hhmm as a long
long aipTime::hhmm () const {
long dyear, dmon, dday, dhour, dmin;
if ( ! get(&dyear,&dmon,&dday,&dhour,&dmin) ) return 0;
return ( dhour*100 + dmin );
}
//======================================================================
// aipTimeOfDay
//
//----------------------------------------------------------------------
// set from components
void aipTimeOfDay::set (long a_hour, long a_min) {
m_hour = a_hour;
m_minute = a_min;
if (m_hour < 0 || m_hour > 23 ||
m_minute < 0 || m_minute > 59) reset();
}
//----------------------------------------------------------------------
// set from another instance of aipTimeOfDay
void aipTimeOfDay::set (const aipTimeOfDay& x) {
set (x.hour(), x.minute());
if (!is_valid()) reset();
}
//----------------------------------------------------------------------
// set from a string - ex: "16:30" or "4:30 pm"
void aipTimeOfDay::set (const char *x) {
long hr, min;
char ampm[2]; // only care whether first char is 'p' or 'P'
if (x && *x) x = aip_get_val(x, 2, &hr);
if (x && *x) x = aip_get_val(x, 2, &min);
if (x && *x) x = aip_get_str(x, 1, ampm);
if (x) {
if (*ampm == 'p' || *ampm == 'P') { // pm
if (hr <= 12) hr += 12; // convert to 24 hour clock
}
}
if (hr == 24) hr = 0;
set (hr, min);
}
//----------------------------------------------------------------------
// compare functions
int aipTimeOfDay::eq (const aipTimeOfDay& x) const {
return m_minute == x.minute() && m_hour == x.hour();
}
int aipTimeOfDay::ne (const aipTimeOfDay& x) const {
return !eq(x);
}
int aipTimeOfDay::lt (const aipTimeOfDay& x) const {
if (m_hour < x.hour()) return 1;
if (m_hour == x.hour()) {
if ( m_minute < x.minute()) return 1;
}
return 0;
}
int aipTimeOfDay::gt (const aipTimeOfDay& x) const {
return !lt(x) && !eq(x);
}
int aipTimeOfDay::le (const aipTimeOfDay& x) const {
return lt(x) || eq(x);
}
int aipTimeOfDay::ge (const aipTimeOfDay& x) const {
return !lt(x);
}
//----------------------------------------------------------------------
// add hours to this time-of-day
void aipTimeOfDay::add_hours (long num) {
m_hour += num;
if (m_hour >= 24) m_hour %= 24;
}
//----------------------------------------------------------------------
// add minutes to this time-of-day
void aipTimeOfDay::add_minutes (long num) {
m_minute += num;
if (m_minute >= 60) {
long nhour = m_minute / 60;
m_minute %= 60;
if (nhour > 0) add_hours(nhour);
}
}
//----------------------------------------------------------------------
// return the number of hours since x
long aipTimeOfDay::hours_since (const aipTimeOfDay& x) {
long nhour = m_hour - x.hour();
return nhour;
}
//----------------------------------------------------------------------
// return the number of minutes since x
long aipTimeOfDay::minutes_since (const aipTimeOfDay& x) {
long nmin = (m_minute - x.minute()) +
(m_hour - x.hour()) * 60;
return nmin;
}
//----------------------------------------------------------------------
// write the time-of-day to the pointer on a 24 hour clock
//
// ex: "08:30" "16:45" writes 6 chars (with null)
void aipTimeOfDay::hh_mm (char *dstr) const {
if (!dstr) return;
sprintf (dstr, "%02d:%02d", m_hour, m_minute);
}
//----------------------------------------------------------------------
// write the time-of-day with seconds to the pointer on a 24 hour clock
//
// ex: "08:30:00" "16:45:00 writes 9 chars (with null)
void aipTimeOfDay::hh_mm_ss (char *dstr) const {
if (!dstr) return;
sprintf (dstr, "%02d:%02d:00", m_hour, m_minute);
}
//----------------------------------------------------------------------
// write the time-of-day to the pointer on a 12 hour clock
//
// ex: "08:30 am" "04:45 pm" writes 9 chars (with null)
void aipTimeOfDay::hh_mm_12 (char *dstr) const {
if (!dstr) return;
int hr = m_hour;
char ampm[3];
if (hr > 12) {
strcpy (ampm, "pm");
hr -= 12;
} else {
strcpy (ampm, "am");
}
sprintf (dstr, "%02d:%02d %s", hr, m_minute, ampm);
}
//----------------------------------------------------------------------
//======================================================================
// License
//
// Permission is hereby granted, free of charge, to any
// person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to
// whom the Software is furnished to do so, subject to the
// following conditions:
//
// The copyright notice and this license shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
//
//**********************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -