pilar
Joined: 30 Jan 2008 Posts: 197
|
Error decomposing Epoch Time value |
Posted: Sun Oct 01, 2023 4:52 pm |
|
|
Hi, I am using the CCS V4.74 and the PIC18F4620, I need to decompose the value of an Epoch Time data into its equivalent years, months, days, hours, minutes and seconds, for this I have this code but it does not correspond to the month or day of the month, could someone tell me what my mistake is?
Here is my code:
Code: | #include <18F4620.h>
#DEVICE ADC=8
#fuses HS,WDT32768,PROTECT,NOLVP,NOBROWNOUT
#use delay(clock=20MHz)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)// RS232 Estándar
#include <stdio.h>
int32 secondsInYear = 31536000; // 365 * 24 * 60 * 60
int32 secondsInMonth = 2592000; // 30 * 24 * 60 * 60
int32 secondsInDay = 86400; // 24 * 60 * 60
int32 secondsInHour = 3600; // 60 * 60
int32 secondsInMinute = 60; // 60
int32 epochTime;
int16 years;
int months;
int days;
int hours;
int minutes;
int seconds;
int main() {
epochTime = 1696199000; // data Epoch time
years = epochTime / secondsInYear;
years = 1970 + years;
epochTime = epochTime % secondsInYear;
months = epochTime / secondsInMonth;
epochTime = epochTime % secondsInMonth;
days = epochTime / secondsInDay;
epochTime = epochTime % secondsInDay;
hours = epochTime / secondsInHour;
epochTime = epochTime % secondsInHour;
minutes = epochTime / secondsInMinute;
seconds = epochTime% secondsInMinute;
printf("Years: %Ld\r\n", years);
printf("Months: %d\r\n", months);
printf("Days: %d\r\n", days);
printf("Hours: %d\r\n", hours);
printf("Minutes: %d\r\n", minutes);
printf("Seconds: %d\r\n", seconds);
} |
Quote: | I am getting:
Years: 2023
Months: 9
Days: 16
Hours: 22
Minutes: 23
Seconds: 20 |
Quote: | when I should get:
Years: 2023
Months: 10
Days: 1
Hours: 22
Minutes: 23
Seconds: 20 |
|
|