Domanda

Ho del codice che utilizza la funzione Oracle add_months per incrementare una data di X numero di mesi.

Ora devo reimplementare la stessa logica in una funzione C / C ++. Per motivi che non desidero / non devo affrontare, non posso semplicemente inviare una query a Oracle per ottenere la nuova data.

Qualcuno conosce un modo semplice e affidabile per aggiungere un numero X di mesi a un time_t? Di seguito sono riportati alcuni esempi dei tipi di calcoli.

30/01/2009 + 1 mese = 28/02/2009
31/01/2009 + 1 mese = 28/02/2009
27/02/2009 + 1 mese = 27/03/2009
28/02/2009 + 1 mese = 31/03/2009
31/01/2009 + 50 mesi = 31/03/2013

È stato utile?

Soluzione

Il metodo AddMonths_OracleStyle fa ciò di cui hai bisogno.

Forse vorresti sostituire IsLeapYear e GetDaysInMonth con alcuni metodi bibliotecari.

#include <ctime>
#include <assert.h>

bool IsLeapYear(int year) 
{
    if (year % 4 != 0) return false;
    if (year % 400 == 0) return true;
    if (year % 100 == 0) return false;
    return true;
}

int daysInMonths[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int GetDaysInMonth(int year, int month)
{
    assert(month >= 0);
    assert(month < 12);

    int days = daysInMonths[month];

    if (month == 1 && IsLeapYear(year)) // February of a leap year
        days += 1;

    return days;
}

tm AddMonths_OracleStyle(const tm &d, int months)
{
    bool isLastDayInMonth = d.tm_mday == GetDaysInMonth(d.tm_year, d.tm_mon);

    int year = d.tm_year + months / 12;
    int month = d.tm_mon + months % 12;

    if (month > 11)
    {
        year += 1;
        month -= 12;
    }

    int day;

    if (isLastDayInMonth)
        day = GetDaysInMonth(year, month); // Last day of month maps to last day of result month
    else
        day = std::min(d.tm_mday, GetDaysInMonth(year, month));

    tm result = tm();

    result.tm_year = year;
    result.tm_mon = month;
    result.tm_mday = day;

    result.tm_hour = d.tm_hour;
    result.tm_min = d.tm_min;
    result.tm_sec = d.tm_sec;

    return result;
}

time_t AddMonths_OracleStyle(const time_t &date, int months)
{
    tm d = tm();

    localtime_s(&d, &date);

    tm result = AddMonths_OracleStyle(d, months);

    return mktime(&result);
}

Altri suggerimenti

Puoi utilizzare Boost.GregorianDate per questo.

Più specificamente, determina il mese aggiungendo il date_duration , quindi usa end_of_month_day () dagli algoritmi della data

Converti time_t in struct tm , aggiungi X al mese, aggiungi mesi > 12 a anni, riconvertire. tm.tm_mon è un int, l'aggiunta di oltre 32000 mesi non dovrebbe essere un problema.

[modifica] Potresti scoprire che abbinare Oracle è difficile una volta arrivati ??ai casi più difficili, come aggiungere 12 mesi al 29/02/2008. Sia l'01 / 03/2009 che il 28/02/2008 sono ragionevoli.

Davvero nuova risposta a una davvero vecchia domanda!

Utilizzando questa libreria gratuita e open source e un compilatore C ++ 14 (come clang) Ora posso scrivere questo:

#include "date.h"

constexpr
date::year_month_day
add(date::year_month_day ymd, date::months m) noexcept
{
    using namespace date;
    auto was_last = ymd == ymd.year()/ymd.month()/last;
    ymd = ymd + m;
    if (!ymd.ok() || was_last)
        ymd = ymd.year()/ymd.month()/last;
    return ymd;
}

int
main()
{
    using namespace date;
    static_assert(add(30_d/01/2009, months{ 1}) == 28_d/02/2009, "");
    static_assert(add(31_d/01/2009, months{ 1}) == 28_d/02/2009, "");
    static_assert(add(27_d/02/2009, months{ 1}) == 27_d/03/2009, "");
    static_assert(add(28_d/02/2009, months{ 1}) == 31_d/03/2009, "");
    static_assert(add(31_d/01/2009, months{50}) == 31_d/03/2013, "");
}

E si compila.

Nota la notevole somiglianza tra il codice attuale e lo pseudo-codice del PO:

  

30/01/2009 + 1 mese = 28/02/2009
  31/01/2009 + 1 mese = 28/02/2009
  27/02/2009 + 1 mese = 27/03/2009
  28/02/2009 + 1 mese = 31/03/2009
  31/01/2009 + 50 mesi = 31/03/2013

Nota anche che le informazioni in fase di compilazione in portano a informazioni in fase di compilazione in uscita .

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top