Berechnen der zeit anhand eines zeit und datum strings
-
Sorry, keine direkte Antwort auf Deinen Code.
Vielleicht aber trotzdem für Dich von Interesse.
Zum Umrechnen aus der "normalen" Darstellung Tag Monat Jahr in eine linare Darstellung (Anzahl Tage seit einem Datum) hilft das Stichwort: Julianisches Datum. Erklärt wird das ganz ordentlich (mit Formeln) bei Wikipedia.
Routinen findet man dafür auch schon fertig, z.B. in der gnulib
http://savannah.gnu.org/projects/gnulib
Module getDate.cDie Differenz in Tagen ist dann einfach die Differenz der beiden julianischen Datumswerte. Die Zeit bildet man einfach als Bruchteil von 24 Stunden ab (z.B. 0.5 ist 12h Mittag).
Um die Zahl der Sekunden zu berechnen, multiplizierst Du die Differenz mit 24 * 60 * 60 (Anzahl Stunden pro Tages * Anzahl Minuten pro Stunde * Anzahl Sekunden pro Minute) und hast dann die Differenz in Sekunden.Beispiel (auf Sekunden gerundet):
16.03.1983 22:48.00 = 2445410.45
10.01.1984 17:31.11 = 2445710.23
Differenz: 299,78 = 299 Tage und 0,78 von 24 Stunden = 18:43:12
In Sekunden 299,78 * 24 * 60 * 60 = 18648714 Sekunden
-
Ich würde Datum und Uhrzeit so nach Sekunden seit 1.1.1900 umrechnen
unsigned long time_to_sec( int hour, int minute, int sec ) { return sec + 60*minute + 60*60*hour; } unsigned long sec_of_month( int month, int year ) { return days_of_month(month, year) * 24 * 60* 60; } unsigned long sec_of_year( int year ) { return days_of_year( year ) * 24 * 60* 60; } unsigned long time_date_to_sec ( int year, int month, int day, int hour, int minute, int sec ) { unsigned long ret = time_to_sec( hour, minute, sec ); for( int y = 1900; y < year; ++y ) // ganze jahre dieser Tag ret += sec_of_year( y ); for( int m = 1; m < month; ++m ) // ganze monate ret += sec_of_month( m, year ); ret += ( day-1 )* 24 * 60* 60; // restliche Tage im Monat return ret; }
Vorsicht ungetestet.
Die fehlenden funktionen sind in meinem ersten posting.
Kurt
-
danke werde ich mir gleich mal anschauen und auch danke an dir "niemand" schau ich mir auch gleich an. habe meine lösung noch erweitert. könnt ihr euch die bitte mal anschauen? sollte bis auf den konstanten monat (kommentiert) eigentlich beinahe stimmen oder was vergesse ich noch zu berücksichtigen? was ist falsch?
[cpp]
int act_day = 25;
int act_month = 9;
int act_year = 2005;int act_hour = 23;
int act_minute = 50;
int act_second = 5;int to_day = 26;
int to_month = 9;
int to_year = 2005;int to_hour = 2;
int to_minute = 50;
int to_second = 5;int total_diffdays = 0;
int total_diffhours = 0;
int total_diffminutes = 0;
int total_diffseconds = 0;int seconds = 0;
while(true)
{
if(act_day < to_day && act_month <= to_month && act_year <= to_year)
{
if(act_second < 59)
{
act_second++;
if(total_diffseconds == 59)
{
total_diffminutes++;
total_diffseconds = 0;
}
else
{
total_diffseconds++;
}
}
else
{
act_second = 0;
act_minute++;
if(total_diffminutes == 59)
{
total_diffhours++;
total_diffminutes = 0;
}
else
{
total_diffminutes++;
}
if(act_minute == 59)
{
act_minute = 0;
act_hour++;
if(total_diffhours == 23)
{
total_diffdays++;
total_diffhours = 0;
}
else
{
total_diffhours++;
}
}
if(act_hour == 23)
{
if(act_day == 31) //momentan konstant muss aber stimmen!!! (ob 29, 30, 31)
{
if(act_month == 12)
{
act_day = 1;
act_month = 1;
act_year++;
}
else
{
act_month++;
act_day = 1;
}
}
else
{
act_day++;
}
}
}
}
else
{
total_diffdays *= 24 * 60 * 60;
total_diffhours *= 60 * 60;
total_diffminutes *= 60;
seconds = total_diffdays + total_diffhours + total_diffminutes + total_diffseconds;
break;
}
}
[cpp]
-
sorry!
int act_day = 25; int act_month = 9; int act_year = 2005; int act_hour = 23; int act_minute = 50; int act_second = 5; int to_day = 26; int to_month = 9; int to_year = 2005; int to_hour = 2; int to_minute = 50; int to_second = 5; int total_diffdays = 0; int total_diffhours = 0; int total_diffminutes = 0; int total_diffseconds = 0; int seconds = 0; while(true) { if(act_day < to_day && act_month <= to_month && act_year <= to_year) { if(act_second < 59) { act_second++; if(total_diffseconds == 59) { total_diffminutes++; total_diffseconds = 0; } else { total_diffseconds++; } } else { act_second = 0; act_minute++; if(total_diffminutes == 59) { total_diffhours++; total_diffminutes = 0; } else { total_diffminutes++; } if(act_minute == 59) { act_minute = 0; act_hour++; if(total_diffhours == 23) { total_diffdays++; total_diffhours = 0; } else { total_diffhours++; } } if(act_hour == 23) { if(act_day == 31) //momentan konstant muss aber stimmen!!! (ob 29, 30, 31) { if(act_month == 12) { act_day = 1; act_month = 1; act_year++; } else { act_month++; act_day = 1; } } else { act_day++; } } } } else { total_diffdays *= 24 * 60 * 60; total_diffhours *= 60 * 60; total_diffminutes *= 60; seconds = total_diffdays + total_diffhours + total_diffminutes + total_diffseconds; break; } }
-
hm komisch das ergebnisn ist 114
im code ist die differenz 3 stunden. müsste also ergebniss: 10800 sein.
-
hier der entgültige code, mit deinen beiden funktionen um den tag des monats zu holen.
//--------------------------------------------------------------------------- bool is_leap_year(const int year) { if((year % 400) == 0) { return true; } else { if((year % 100) == 0) { return false; } else { if((year % 4) == 0) { return true; } } } return false; } //--------------------------------------------------------------------------- short days_of_month(const int month, const int year) { int arrDaysinMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if(month == 2) { if(is_leap_year(year)) { return 29; } else { return 28; } } if((month >= 1) && (month <= 12)) { return arrDaysinMonth[month]; } else { return 0; } } //--------------------------------------------------------------------------- int CalculateSeconds() { int act_day = 25; int act_month = 9; int act_year = 2005; int act_hour = 23; int act_minute = 50; int act_second = 5; int to_day = 26; int to_month = 9; int to_year = 2005; int to_hour = 2; int to_minute = 50; int to_second = 5; int total_diffdays = 0; int total_diffhours = 0; int total_diffminutes = 0; int total_diffseconds = 0; int seconds = 0; while(true) { if(act_day < to_day && act_month <= to_month && act_year <= to_year) { if(act_second < 59) { act_second++; if(total_diffseconds == 59) { total_diffminutes++; total_diffseconds = 0; } else { total_diffseconds++; } } else { act_second = 0; act_minute++; if(total_diffminutes == 59) { total_diffhours++; total_diffminutes = 0; } else { total_diffminutes++; } if(act_minute == 59) { act_minute = 0; act_hour++; if(total_diffhours == 23) { total_diffdays++; total_diffhours = 0; } else { total_diffhours++; } } if(act_hour == 23) { if(act_day == days_of_month(act_month, act_year)) { if(act_month == 12) { act_day = 1; act_month = 1; act_year++; } else { act_month++; act_day = 1; } } else { act_day++; } } } } else { total_diffdays *= 24 * 60 * 60; total_diffhours *= 60 * 60; total_diffminutes *= 60; seconds = total_diffdays + total_diffhours + total_diffminutes + total_diffseconds; return seconds; } } } //---------------------------------------------------------------------------
aber eben, komischerweise erhalte ich 114 als ergebniss.
-
also jetzt bin ich meiner meinung nach auf dem richtigen weg. die schleife beendet nun genau dann wann sie soll. nähmlich wenn datum und die zeit zur berechnung erreicht wurde. jetzt stimmt nur der wert noch nicht. also alle total_ inkrementationen sind wohl noch teils an falscher stelle. jetzt muss ich jedoch schlafen
wenn jemand lust hat mir den code zu korrigieren, hier ist er:
int CalculateSeconds() { int act_day = 25; int act_month = 9; int act_year = 2005; int act_hour = 23; int act_minute = 50; int act_second = 5; int to_day = 26; int to_month = 9; int to_year = 2005; int to_hour = 2; int to_minute = 50; int to_second = 5; int total_diffdays = 0; int total_diffhours = 0; int total_diffminutes = 0; int total_diffseconds = 0; int seconds = 0; while(true) { if(act_day == to_day && act_month == to_month && act_year == to_year && act_hour == to_hour && act_minute == to_minute && act_second == to_second) { total_diffdays *= 24 * 60 * 60; total_diffhours *= 60 * 60; total_diffminutes *= 60; seconds = total_diffdays + total_diffhours + total_diffminutes + total_diffseconds; return seconds; } else { if(act_second < 59) { act_second++; if(total_diffseconds < 59) { total_diffseconds++; } else { total_diffseconds = 0; total_diffminutes++; } } else { act_second = 0; if(act_minute == 59) { act_minute = 0; if(act_hour == 23) { act_hour = 0; if(act_day == days_of_month(act_month, act_year)) { if(act_month == 12) { act_day = 1; act_month = 1; act_year++; act_hour = 0; act_minute = 0; act_second = 0; } else { act_month++; act_day = 1; act_hour = 0; act_minute = 0; act_second = 0; } } else { act_day++; } } else { act_hour++; if(total_diffhours < 23) { total_diffhours++; } else { total_diffseconds = 0; total_diffminutes = 0; total_diffhours = 0; total_diffdays++; } } } else { act_minute++; if(total_diffminutes < 59) { total_diffminutes++; } else { total_diffseconds = 0; total_diffminutes = 0; total_diffhours++; } } } } } }
-
ich konnts nicht sein lassen. nach scharfem überlegen wurde mir klar das ich ja nur seconds jedesmal in der else-anweisung inkrementieren muss. wenn die allererste if-anweisung bzw. die bedingung dann erfüllt wurd bricht die schleife ab und seconds wurde so oft mal inkrementiert wie die differenz eben schlussendlich ist.
habs also nun hinbekommen. danke ZuK für deine unterstützung! hier noch mein fertiger code. ist dieser ok so?
int CalculateSeconds() { int act_day = 25; int act_month = 9; int act_year = 2005; int act_hour = 23; int act_minute = 50; int act_second = 5; int to_day = 26; int to_month = 9; int to_year = 2005; int to_hour = 2; int to_minute = 50; int to_second = 5; int seconds = 0; while(true) { if(act_day == to_day && act_month == to_month && act_year == to_year && act_hour == to_hour && act_minute == to_minute && act_second == to_second) { return seconds; } else { seconds++; if(act_second < 59) { act_second++; } else { act_second = 0; if(act_minute == 59) { act_minute = 0; if(act_hour == 23) { act_hour = 0; if(act_day == days_of_month(act_month, act_year)) { if(act_month == 12) { act_day = 1; act_month = 1; act_year++; act_hour = 0; act_minute = 0; act_second = 0; } else { act_month++; act_day = 1; act_hour = 0; act_minute = 0; act_second = 0; } } else { act_day++; } } else { act_hour++; } } else { act_minute++; } } } } }
-
Die Funktion ist eindeutig zu lang.
-
splitter schrieb:
Die Funktion ist eindeutig zu lang.
Und benutzt "Magic Numbers"
-
Natürlich sollte das so funktionieren.
Diese Version ist aber nicht sehr flexibel. Wenn du jetzt eine Funktion brauchst um zb eine bestimmte Zeitspanne zu einem Datum zu addieren oder zu subtrahieren dann musst du diese extra schreiben.
Wenn du aber prinzipiell jedes Datum+ Uhrzeit in eine Anzahl von Sekunden seit einem bestimmten Anfangszeitpunkt umrechnest dann kannst du, wenn du zB die Differenz zwichen zwei Zeitpunkten brauchst, ganz einfach subtrahieren.Willst du das Datum plus eine Zeitspanne errechnen genügt eine Addition.
Dann brauchst du nur noch eine Funktion um von diesem seriellen Datum wieder in einen Datum-Uhrzeitstring zurückzurechnen. Du hast also nur 2 komplexere Funktionen und kannst damit alle möglichen Datumsberechnungen abdecken.
Das ganze würde ich dann noch in zwei Klassen date und timespan kapseln.
Kurt
-
Also wenn Du mich fragst:
Wenn Du die Differenz von zwei Daten in Sekunden haben willst, würde ich die
Funktionen in time.h benutzen.
Sind die einzelnen Komponenten der Daten z.B. 18.09.2005 14:00:00
als integer Werte gegeben: int stunde = 14;
usw....
dann ist die Berechnung der Differenz in Sekunden ein Dreizeiler.Gruß,
proggingmania
-
#include <time.h> #include <iostream> using namespace std; void main() { struct tm heute_14Uhr = { 0, 0, 14, 18, 8, 2005 - 1900}; struct tm morgen_14Uhr = { 0, 0, 14, 19, 8, 2005 - 1900}; cout<< "Differenz in Sekunden: "<< difftime(mktime(&morgen_14Uhr),mktime(&heute_14Uhr)) << endl; }
-
Wie wäre es mit boost::date_time ??
MfG Spacelord