CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Error decomposing Epoch Time value

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
pilar



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

Error decomposing Epoch Time value
PostPosted: Sun Oct 01, 2023 4:52 pm     Reply with quote

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
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Mon Oct 02, 2023 12:08 am     Reply with quote

Problem is you can't just divide like this. Remember years are different
lengths when they are leap years, and months all differ in length.
You have the function to do the conversion in the libraries with the
compiler. localtime in time.c/time.h.
If you want to do the conversion without this the function's code is here:

[url]
https://stackoverflow.com/questions/21593692/convert-unix-timestamp-to-date-without-system-libs
[/url]

You see, it walks through the time value, adjusting for the leap years and
month lengths.

You can prove that this is the problem. Simply do the divisions you
have coded on the Windows calculator.. Guess what this gives for the
month number.... 9. Sad
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group