Calendar Programming Project in C. Too complicated for this newbie [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
void main()
{
int numdays, month, day, year = 1;
while (year > 0)`enter code here`
{
printf("Enter Month: ");
scanf("%d", &month);
printf("Enter Day: ");
scanf("%d", &day);
printf("Enter Year: ");
scanf("%d", &year);
numdays = ((year - 1) * 365 + ((year - 1) / 4) - ((year - 1) / 100) + ((year - 1) / 400)); // how many days including exceptions
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) //check if leapyear
{
if (month = 1) // January
numdays = numdays;
if (month = 2) // February
numdays = numdays + 31;
if (month = 3) // March
numdays = numdays + 28 + 31 + 1;
if (month = 4) // April
numdays = numdays + 31 + 28 + 31 + 1;
if (month = 5) // May
numdays = numdays + 30 + 31 + 28 + 31 + 1;
if (month = 6) // June
numdays = numdays + 31 + 30 + 31 + 28 + 31 + 1;
if (month = 7) // July
numdays = numdays + 30 + 31 + 30 + 31 + 28 + 31 + 1;
if (month = 8) // August
numdays = numdays + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1;
if (month = 9) // September
numdays = numdays + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1;
if (month = 10) // October
numdays = numdays + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1;
if (month = 11) // November
numdays = numdays + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1;
if (month = 12) // December
numdays = numdays + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1;
}
else
{
if (month = 1) // January
numdays = numdays;
if (month = 2) // February
numdays = numdays + 31;
if (month = 3) // March
numdays = numdays + 28 + 31;
if (month = 4) // April
numdays = numdays + 31 + 28 + 31;
if (month = 5) // May
numdays = numdays + 30 + 31 + 28 + 31;
if (month = 6) // June
numdays = numdays + 31 + 30 + 31 + 28 + 31;
if (month = 7) // July
numdays = numdays + 30 + 31 + 30 + 31 + 28 + 31;
if (month = 8) // August
numdays = numdays + 31 + 30 + 31 + 30 + 31 + 28 + 31;
if (month = 9) // September
numdays = numdays + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
if (month = 10) // October
numdays = numdays + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
if (month = 11) // November
numdays = numdays + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
if (month = 12) // December
numdays = numdays + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
{
printf("%d %d %d\n", month, day, year);
}
int daycode = numdays % 7;
switch (daycode)
{
case 0:
printf("Sunday\n");
break;
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
default: printf("unexpected error (daycode case) daycode = %d", daycode);
break;`enter code here`
}
}
}
}
What I need to do is get the output to read what the day would be on the given date input by the user. However, I don't understand why my month is being locked at 12 for input. Any help is appreciated.

As mentioned in a comment by MrPickles, the main problem is that you are mixing up the assignment operator = with the comparison operator ==. The former (=) sets the variable on the left to whatever is on the right, while == checks if the right hand and left hand sides are equal. Your last if statement is setting month to 12 instead of checking if it's 12.
Another big problem is that your code to print out the solution is inside your else statement, so it doesn't run at all if it's a leap year. Make sure to move it out.
Finally, you never actually use the day of the month in your code. You need to put in something like numdays+=day;.
Other than that, you could make the code much shorter, but I think it's right besides that.

Related

epoch in seconds to MJD conversion in C

I wanted to convert the given time in epoch format to MJD (Modified Julian Day). But not getting exact MJD equivalent. I think missing some offset value.
#include <stdio.h>
#include <time.h>
#include <memory.h>
int get_mjd (int Y, int M, int D)
{
int L = 0, MJD = 0;
if ((M == 1) || (M == 2))
L = 1;
MJD = 14956 + D + (int)((Y - L) * 365.25) +
(int)((M + 1 + L * 12) * 30.60001);
return MJD;
}
int main(){
time_t start;
int mjd;
struct tm start_tm;
start = 1442286867; // Tue, 15 Sep 2015 03:14:27 GMT
memcpy(&start_tm, localtime(&start),sizeof(struct tm));
printf("year:%d\tmonth: %d\tday: %d\n",start_tm.tm_year,start_tm.tm_mon,start_tm.tm_mday);
mjd = get_mjd(start_tm.tm_year,start_tm.tm_mon, start_tm.tm_mday);
printf("mjd: %d\n",mjd);
return 0;
}
Output:
year:115 month: 8 day: 15
mjd: 57249
Expected mjd should is 57280
Please let me know what could be missing here.
If you don't need the date broken down into Gregorian calendar year/month/day values, there's a much simpler way.
double mjd(time_t epoch_time)
{
return epoch_time / 86400.0 + 40587;
}
The value 40587 is the number of days between the MJD epoch (1858-11-17) and the Unix epoch (1970-01-01), and 86400 is the number of seconds in a day.
Ref Modified Julian Day: Days since November 17, 1858
get_mjd() oddly references Y as years from 1900, yet M in the traditional 1 = January, 2 = February, etc.
get_mjd() works for years in the range 1898 to 2099, but fails outside that.
// For years from 1900, limited range
int get_mjd (int Y, int M, int D) {
int L = 0, MJD = 0;
if ((M == 1) || (M == 2))
L = 1;
MJD = 14956 + D + (int)((Y - L) * 365.25) +
(int)((M + 1 + L * 12) * 30.60001);
return MJD;
}
The following works over a much larger range or the Gregorian calendar.
M,D not limited to their usual range and does not need floating point math.
#define DaysPer400Years (365L*400 + 97)
#define DaysPer100Years (365L*100 + 24)
#define DaysPer4Years (365*4 + 1)
#define DaysPer1Year 365
#define MonthsPerYear 12
#define MonthsPer400Years (12*400)
#define MonthMarch 3
#define mjdOffset (678881 /* Epoch Nov 17, 1858 */)
static const short DaysMarch1ToBeginingOfMonth[12] = {
0,
31,
31 + 30,
31 + 30 + 31,
31 + 30 + 31 + 30,
31 + 30 + 31 + 30 + 31,
31 + 30 + 31 + 30 + 31 + 31,
31 + 30 + 31 + 30 + 31 + 31 + 30,
31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31,
31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 31 };
int ymd_to_mjd_x(int year, int month, int day) {
year += month / MonthsPerYear;
month %= MonthsPerYear;
// Adjust for month/year to Mar... Feb
while (month < MonthMarch) {
month += MonthsPerYear; // Months per year
year--;
}
int d = (year / 400) * DaysPer400Years;
int y400 = (int) (year % 400);
d += (y400 / 100) * DaysPer100Years;
int y100 = y400 % 100;
d += (y100 / 4) * DaysPer4Years;
int y4 = y100 % 4;
d += y4 * DaysPer1Year;
d += DaysMarch1ToBeginingOfMonth[month - MonthMarch];
d += day;
// November 17, 1858 == MJD 0
d--;
d -= mjdOffset;
return d;
}
It was yet another off by one error. struct tm returns month with range 0-11.
I needed to +1 to tm_mon before using get_mjd(). Now it works!

How to sum values in array from different columns + EXCEL VBA

I need to calculate this in excel vba ,using array loop only :
round 0 round 1
9 28
65 84
28 47
84 103
41 60
66 85
115 134
I need to sum values in round 0 in loop so the sum result (408) must be divided by 7 , if not I WANT to sum one value from the round 1 (in this case 84 instead of 65 ) to the rest of values in round 0 so the sum result can divided by 7 . There will be so many round up to 7 . I need VBA code to accomplish this..
Notes :
round 0 and round 1 all in one two-dimensional array
My Question is : is there a way to sum values from different columns in multi-dimensional array ??
there is an image attached .
I appreciate any help or idea .
Thanks in advance
Excel VBA Array Model:
http://im56.gulfup.com/8rDErI.png
Here an example file contains macro "Question1.xlsm"
http://www.gulfup.com/?TKAAYM
Notes : click the link under the big green down arrow to download the file.
UPDATE :
here another macro to the file "Question1.xlsm" :
Sub A1()
Dim arrTemp1() As Integer
Dim sum1 As Integer
arrblkTable1 = Sheets("Sheet1").Range("blkTable1").Value
ReDim Preserve arrTemp1(0 To 1, 1 To 7)
For a = 0 To 1
sum1 = 0
For c = 1 To 7
arrTemp1(a, c) = arrblkTable1(c, 1) + (a * 19)
text6 = text6 & arrTemp1(a, c) & vbCrLf
Worksheets("TEST3").Cells(a + 1, c).Value = arrTemp1(a, c)
sum1 = sum1 + arrTemp1(a, c)
Next c
If XLMod(sum1, 7) = 0 Then
MsgBox "Yes " & sum1
Else
MsgBox "No " & sum1
End If
Next a
MsgBox text6
End Sub
Function XLMod(a, b)
' This replicates the Excel MOD function
XLMod = a - b * Int(a / b)
End Function
UPDATE : here a new update to the previous macro :
Sub A1()
Dim arrTemp1(), arrTemp2(), arrSUMs() As Integer
Dim sum1 As Integer
arrblkTable1 = Sheets("Sheet1").Range("blkTable1").Value
arrblkTable2 = Sheets("Sheet1").Range("blkTable2").Value
'-------------------------------- arrTemp1 ------------------------------
ReDim Preserve arrTemp1(0 To 1, 1 To 7)
For a = 0 To 1
sum1 = 0
For c = 1 To 7
arrTemp1(a, c) = arrblkTable1(c, 1) + (a * 19)
text6 = text6 & arrTemp1(a, c) & vbCrLf
Worksheets("TEST3").Cells(a + 1, c).Value = arrTemp1(a, c)
sum1 = sum1 + arrTemp1(a, c)
Next c
If XLMod(sum1, 7) = 0 Then
MsgBox "Yes " & sum1
Else
MsgBox "No " & sum1
For c = 1 To 7
sum1 = sum1 - arrTemp1(a, c)
arrTemp1(a, c) = arrblkTable1(c, 1) + ((a + 1) * 19)
sum1 = sum1 + arrTemp1(a, c)
If XLMod(sum1, 7) = 0 Then
MsgBox "Yes " & sum1 & " " & arrTemp1(a, c)
End If
Next c
End If
Next a
For x = 0 To UBound(arrTemp1)
For y = 1 To UBound(arrTemp1)
text7 = text7 & arrTemp1(x, y) & vbCrLf
Next y
Next x
MsgBox text7
End Sub
Function XLMod(a, b)
' This replicates the Excel MOD function
XLMod = a - b * Int(a / b)
End Function
I need now to put each sum1 in one array , how I can do that ??
If I understood it correctly you want something similar to this:
Sub p()
v = Range("A2:A8")
v1 = Sheets("Sheet1").Range("B2:B8")
s = Application.WorksheetFunction.Sum(v)
b = False
Count = 0
For i = 1 To 7
temp = v
temp(i, 1) = v1(i, 1)
s = Application.WorksheetFunction.Sum(temp)
b = s Mod 7 = 0
If b = True Then
Count = Count + 1
End If
Next
MsgBox Count
End Sub
It may help tremendously if you can give more detail about what problem you're trying to solve, instead of focusing on how to solve it this way. There is a possibility that there's another way of doing it that hasn't occurred to you that will be much simpler.
This isn't an answer. Yet. But it will take more space than is allowed in a comment to ensure we've got this right.
For your sample data:
round 0 round 1
9 28
65 84
28 47
84 103
41 60
66 85
115 134
You want to:
Sum all the values in Round 0 (9 + 65 + 28 + 84 + 41 + 66 + 115) = 408
Take that sum (408) mod 7 and see if the result is 0
408 / 7 = 58.28, so (408 mod 7) <> 0
If the result isn't 0 (as in this case)
Start substituting numbers from round 1 for numbers in Round 0
Sum (28 + 65 + 28 + 84 + 41 + 66 + 115) = 427
427 / 7 = 61 (427 mod 7) = 0
This is now your valid result set.
Had the first number in Round 1 been 29
Sum (29 + 65 + 28 + 84 + 41 + 66 + 115) = 428
428 / 7 = 61.14 so (428 mod 7) <> 0
Substitute the next number from round 1 for the next number from round 0
Sum (9 + 84 + 28 + 84 + 41 + 66 + 115) = 427
This is now your valid result set.
Is that the logic you're after?
What happens if you get to the end of round 1 and you don't find a total that (mod 7 = 0)?

Universal formula to calculate the number of days in a month taking into account leap years

Does someone know a math formula to calculate the number of days in a month like this one
28 + (x + Math.floor(x/8)) % 2 + 2 % x + 2 * Math.floor(1/x);
but which also takes in to account leap years? It should also take into account that the Gregorian calendar omits 3 leap days every 400 years, which is the length of its leap cycle.
It isn't very hard to add a term (m == 2) * leapyear(yyyy) to the expression to determine the correct number of days in February of a leap year. This C code shows a way to do it:
#include <stdio.h>
#include <stdbool.h>
static inline bool leapyear(int yy)
{
if (yy % 4 != 0) return false;
if (yy % 100 != 0) return true;
if (yy % 400 != 0) return false;
return true;
}
static inline int old_dim(int mm)
{
return (28 + (mm + (mm/8)) % 2 + 2 % mm + 2 * (1/mm));
}
static inline int new_dim(int mm, int yyyy)
{
return (28 + (mm + (mm/8)) % 2 + 2 % mm + 2 * (1/mm) + ((mm == 2) * leapyear(yyyy)));
}
int main(void)
{
/*28 + (x + Math.floor(x/8)) % 2 + 2 % x + 2 * Math.floor(1/x);*/
for (int mm = 1; mm <= 12; mm++)
printf("mm = %2d, DIM = %2d\n", mm, old_dim(mm));
for (int yyyy = 1900; yyyy < 2101; yyyy += 5)
{
for (int mm = 1; mm <= 12; mm++)
printf("yyyy = %4d, mm = %2d: DIM = %2d\n", yyyy, mm, new_dim(mm, yyyy));
}
return 0;
}
The output for mm = 2 (filtered from the full output) is:
yyyy = 1900, mm = 2: DIM = 28
yyyy = 1905, mm = 2: DIM = 28
yyyy = 1910, mm = 2: DIM = 28
yyyy = 1915, mm = 2: DIM = 28
yyyy = 1920, mm = 2: DIM = 29
yyyy = 1925, mm = 2: DIM = 28
yyyy = 1930, mm = 2: DIM = 28
yyyy = 1935, mm = 2: DIM = 28
yyyy = 1940, mm = 2: DIM = 29
yyyy = 1945, mm = 2: DIM = 28
yyyy = 1950, mm = 2: DIM = 28
yyyy = 1955, mm = 2: DIM = 28
yyyy = 1960, mm = 2: DIM = 29
yyyy = 1965, mm = 2: DIM = 28
yyyy = 1970, mm = 2: DIM = 28
yyyy = 1975, mm = 2: DIM = 28
yyyy = 1980, mm = 2: DIM = 29
yyyy = 1985, mm = 2: DIM = 28
yyyy = 1990, mm = 2: DIM = 28
yyyy = 1995, mm = 2: DIM = 28
yyyy = 2000, mm = 2: DIM = 29
yyyy = 2005, mm = 2: DIM = 28
yyyy = 2010, mm = 2: DIM = 28
yyyy = 2015, mm = 2: DIM = 28
yyyy = 2020, mm = 2: DIM = 29
yyyy = 2025, mm = 2: DIM = 28
yyyy = 2030, mm = 2: DIM = 28
yyyy = 2035, mm = 2: DIM = 28
yyyy = 2040, mm = 2: DIM = 29
yyyy = 2045, mm = 2: DIM = 28
yyyy = 2050, mm = 2: DIM = 28
yyyy = 2055, mm = 2: DIM = 28
yyyy = 2060, mm = 2: DIM = 29
yyyy = 2065, mm = 2: DIM = 28
yyyy = 2070, mm = 2: DIM = 28
yyyy = 2075, mm = 2: DIM = 28
yyyy = 2080, mm = 2: DIM = 29
yyyy = 2085, mm = 2: DIM = 28
yyyy = 2090, mm = 2: DIM = 28
yyyy = 2095, mm = 2: DIM = 28
yyyy = 2100, mm = 2: DIM = 28
This correctly considers 1900 and 2100 as non-leap years, but 2000 as a leap year.
yyyy = 1900, mm = 1: DIM = 31
yyyy = 1900, mm = 2: DIM = 28
yyyy = 1900, mm = 3: DIM = 31
yyyy = 1900, mm = 4: DIM = 30
yyyy = 1900, mm = 5: DIM = 31
yyyy = 1900, mm = 6: DIM = 30
yyyy = 1900, mm = 7: DIM = 31
yyyy = 1900, mm = 8: DIM = 31
yyyy = 1900, mm = 9: DIM = 30
yyyy = 1900, mm = 10: DIM = 31
yyyy = 1900, mm = 11: DIM = 30
yyyy = 1900, mm = 12: DIM = 31
…
yyyy = 2000, mm = 1: DIM = 31
yyyy = 2000, mm = 2: DIM = 29
yyyy = 2000, mm = 3: DIM = 31
yyyy = 2000, mm = 4: DIM = 30
yyyy = 2000, mm = 5: DIM = 31
yyyy = 2000, mm = 6: DIM = 30
yyyy = 2000, mm = 7: DIM = 31
yyyy = 2000, mm = 8: DIM = 31
yyyy = 2000, mm = 9: DIM = 30
yyyy = 2000, mm = 10: DIM = 31
yyyy = 2000, mm = 11: DIM = 30
yyyy = 2000, mm = 12: DIM = 31

Day of Week function not working as intended in Atmega8

I have a C function that finds the Day of the week if given the complete date. This function works perfectly when I compile it on my laptop using gcc.
But when I compile the function for the Atmega8 using avr-gcc it gives the wrong answer. Could anyone help me figure out why?
Here's the C function
unsigned char *getDay(int year, int month, int day) {
static unsigned char *weekdayname[] = {"MON", "TUE",
"WED", "THU", "FRI", "SAT", "SUN"};
size_t JND = \
day \
+ ((153 * (month + 12 * ((14 - month) / 12) - 3) + 2) / 5) \
+ (365 * (year + 4800 - ((14 - month) / 12))) \
+ ((year + 4800 - ((14 - month) / 12)) / 4) \
- ((year + 4800 - ((14 - month) / 12)) / 100) \
+ ((year + 4800 - ((14 - month) / 12)) / 400) \
- 32045;
return weekdayname[JND % 7];
}
For example, when I enter the date 01/01/2015 into the function on a laptop the function gives me the correct day of the week (Thursday) but on the atmega8 it gives me Monday.
Update:
The function sujithvm gave works! :D
But I still have no clue why the original function doesn't work on the avr. I tried uint32_t and int32_t. However, it looks like the day is always off by 3. Adding three to JND gives the correct day. That's a bit strange.
Try using this code
unsigned char *getDay(int y, int m, int d) {
static unsigned char *weekdayname[] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
int weekday = (d += m < 3 ? y-- : y - 2 , 23 * m / 9 + d + 4 + y / 4 - y / 100 + y / 400) % 7;
return weekdayname[weekday];
}

Problems with my unix-epoch time converter

I wrote a simple function to fill three variables with the current year, month, and day.
However, for some reason it is not working correctly, and I can't seem to find the problem.
void getDate(int *year, int *month, int *date)
{
int epochTime,
monthLength,
functionYear,
functionMonth,
functionDate;
functionYear = 1970;
functionMonth = 1;
functionDate = 1;
epochTime = time(NULL);
while (epochTime > 1 * 365 * 24 * 60 * 60)
{
epochTime -= 1 * 365 * 24 * 60 * 60;
functionYear++;
}
monthLength = findMonthLength(functionYear, functionMonth, false);
while (epochTime > 1 * monthLength * 24 * 60 * 60)
{
printf("%d\n", epochTime);
epochTime -= 1 * monthLength * 24 * 60 * 60;
functionMonth++;
monthLength = findMonthLength(functionYear, functionMonth, false);
printf("functionMonth = %d\n", functionMonth);
}
while (epochTime > 1 * 24 * 60 * 60)
{
printf("%d\n", epochTime);
epochTime -= 1 * 24 * 60 * 60;
functionDate++;
printf("functionDate = %d\n", functionDate);
}
*year = functionYear;
*month = functionMonth;
*date = functionDate;
}
findMonthLength() returns an integer value which the length of the month it is sent. 1 = January, etc. It uses the year to test if it is a leap year.
It is currently April 3, 2013; however, my function finds April 15, and I can't seem to find where my problem is.
EDIT:
I got it. My first problem was that while I remembered to check for leap years when finding the months, I forgot about that when finding each year, which put me several days off.
My second problem was that I didn't convert to the local time zone from UTC
One problem could in this section:
while (epochTime > 1 * 365 * 24 * 60 * 60)
{
epochTime -= 1 * 365 * 24 * 60 * 60;
functionYear++;
}
Each iteration of this loop, a time in seconds corresponding to one normal year is subtracted. This does not account for leap years, where you need to subtract a time corresponding to 366 days.
For that section, you may want:
int yearLength = findYearLength(functionYear + 1);
while (epochTime > 1 * yearLength * 24 * 60 * 60)
{
epochTime -= 1 * yearLength * 24 * 60 * 60;
functionYear++;
yearLength = findYearLength(functionYear + 1);
}
with findYearLength(int year) being a function that returns the length in days of a given year.
One minor issue is that leap seconds are not accounted for. As only 35 of these have been added, that can be safely ignored in a calculation for a given day.
Why not use gmtime() or localtime() and be done with it? They return a structure with everything you need in it.
I made the complete solution based on yours, in Python. I hope it will help someone, someday. Cheers!
Use: getDate(unixtime)
def leapYear(year):
#returns True if the year is a leap year, return False if it isn't
if year % 400 == 0:
return True
elif year % 100 == 0:
return False
elif year % 4 == 0:
return True
else:
return False
def findMonthLength(year, month):
#returns an integer value with the length of the month it is sent.
#1 = January, etc. It uses the year to test if it is a leap year.
months1 = [0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
months2 = [0,31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if (leapYear(year)==True):
return months2[month]
else:
return months1[month]
def findYearLength(year):
#returns an integer value with the length of the year it is sent.
#It uses the year to test if it is a leap year.
if leapYear(year)==True:
return 366
else:
return 365
def getDate(epoch):
SECONDS_PER_YEAR = 0
SECONDS_PER_MONTH = 0
SECONDS_PER_DAY = 24 * 60 * 60
SECONDS_PER_HOUR = 60*60
SECONDS_PER_MIN = 60
epochTime = epoch
monthLength = 0
yearLength = 0
year = 1970
month = 1
day = 1
hour = 0
minu = 0
seg = 0
#Years
yearLength = findYearLength(year)
SECONDS_PER_YEAR = yearLength * SECONDS_PER_DAY
while (epochTime >= SECONDS_PER_YEAR):
epochTime -= SECONDS_PER_YEAR
year += 1
yearLength = findYearLength(year)
SECONDS_PER_YEAR = yearLength * SECONDS_PER_DAY
#Months
monthLength = findMonthLength(year, month)
SECONDS_PER_MONTH = monthLength * SECONDS_PER_DAY
while (epochTime >= SECONDS_PER_MONTH):
epochTime -= SECONDS_PER_MONTH;
month += 1
monthLength = findMonthLength(year, month)
SECONDS_PER_MONTH = monthLength * SECONDS_PER_DAY
#Days
while (epochTime >= SECONDS_PER_DAY):
epochTime -= SECONDS_PER_DAY;
day += 1
#Hours
while (epochTime >= SECONDS_PER_HOUR):
epochTime -= SECONDS_PER_HOUR;
hour += 1
#Minutes
while (epochTime >= SECONDS_PER_MIN):
epochTime -= SECONDS_PER_MIN;
minu += 1
#Seconds
seg = epochTime
print ("%d-%d-%d %d:%d:%d") % (year, month, day, hour, minu, seg)

Resources