Problems with my unix-epoch time converter - c

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)

Related

return the day of the week with day, month, year and first of January provided?

I have the C99 as follow:
int dayOfWeek(int day, int month, int year, int firstJan);
The first parameter, day, provides the day of interest − range from 1 to 31 (inclusive). The second parameter, month, provides the month of interest − range from 1 to 12 (inclusive). The third parameter, year, provides the year of interest − any integer value of 1970 or greater. The fourth parameter, firstJan, indicates the day of the week on which the first of January falls in the provided year.
The function will return the day of the week on which the indicated date falls. For example, the call:
dayOfWeek(13, 11, 2017, 0);
will return the integer 1 (representing Monday).
How can I approach the solution? Its permitted values are 0 (representing Sunday), 1 (representing Monday), and so on, up to 6 (representing Saturday). Code has been edit:
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int dayOfweek(int day, int month, int year, int firstJan)
5 {
6 int mth[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
7 int mth_leap[] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335};
8
9 if(year <1970 || month < 1 || month > 12 || day < 1 || day > 31 || firstJan < 0 || firstJan > 6 ){
10 printf("invalid input");
11 //return -1;
12 }
13
14 if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)){
15 day = mth_leap[month - 1] + day ;
16 }else{
17 day = mth[month - 1] + day;
18 }
19
20 int dow = (day - firstJan + 7)%7;
21 printf("Day of week is %i.\n", dow);
22 //return 1;
23
24 }
Day of the week is found simply with mktime().
The mktime function
On successful completion, the values of the tm_wday and tm_yday components of the structure are set appropriately, and the other components are set to represent the specified calendar time,
#include <time.h>
int dayOfWeek(int day, int month, int year, int /*firstJan*/) {
// struct tm members domain are: years from 1900, and months since January
// Important to set tm_isdst = -1 to let the function determine dst setting.
struct tm ymd = { .tm_year - 1900, .tm_mon = month - 1, .tm_mday = day, .tm_isdst = -1);
time_t t = mktime(&ymd); // this will fill in .tm_wday
if (t == -1) return -1; // Failed to find a valid calender time (and day-of-the-week)
return ymd.tm_wday;
}
How can I approach the solution?
Yet OP has a function that provides the day-of-the-week for Jan 1st.
Some pseudo code for that approach:
int dayOfWeek(int day, int month, int year, int firstJan) {
days_since_jan1 = table[month] + day;
if (month > Feb and isleap(year)) days_since_jan1++;
dow = (days_since_jan1 - firstJan + 7)%7
}

Calculation of countdown timer

How numerators and denominators of days, hours and minutes are calculated in this code, why modulus is calculated in numerator?
var countDownDate = new Date("Sep 5, 2018 15:37:25").getTime();
var x = setInterval(function() {
var now = new Date().getTime();
var distance = countDownDate - now;
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
document.getElementById("demo").innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
Let me explain it line by line:
var countDownDate = new Date("Sep 5, 2018 15:37:25").getTime();
In the above line, you are getting the milliseconds for the date Sep 5, 2018 15:37:25 from Jan 1, 1970 (which is the reference date being used by getTime()
var now = new Date().getTime();
var distance = countDownDate - now;
The above two lines are simple. now gets the current time in milliseconds and distance is the difference between the two times (also in milliseconds)
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
The total number of seconds in a day is 60 * 60 * 24 and if we want to get the milliseconds, we need to multiply it by 1000 so the number 1000 * 60 * 60 * 24 is the total number of milliseconds in a day. Dividing the difference (distance) by this number and discarding the values after the decimal, we get the number of days.
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
The above line is a little tricker as there are two operations. The first operation (%) is used to basically discard the part of the difference representing days (% returns the remainder of the division so the days portion of the difference is taken out.
In the next step (division), 1000 * 60 * 60 is the total number of milliseconds in an hour. So dividing the remainder of the difference by this number will give us the number of hours (and like before we discard the numbers after decimal)
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
This is similar to how hours are calculated. The first operation (%) takes out the hours portion from difference and the division (1000*60) returns the minutes (as 1000 * 60 is the number of milliseconds in a minute)
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
Here the first operation (%) takes out the minutes part and the second operation (division) returns the number of seconds.
Note: You might have noticed that in every operation the original distance is used but the code still works fine. Let me give you an example (I am using difference instead of distance as this name makes more sense).
difference = 93234543
days = Math.floor(89234543 / (1000 * 60 * 60 * 24))
=> days = 1
hours = Math.floor((89234543 % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
(result of modulus operation is 6834543, and division is )
=> hours = 1
This is a very important operation to understand:
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
distance(difference) / (1000 * 60 * 60) returns 25 (hours). As you can see we have already got 1 day and 1 hour (25 hours) so distance % (1000 * 60 * 60) wipes out all of these 25 hours and then the division calculates the minutes and so on.

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!

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];
}

Is there something wrong with my date code?

Description: given a date after START_GREGORIAN_CALENDAR, this function returns the number of days until the next Thursday. For example, for 16 March 2011 (2011,3,16), the function will return 1, and for 17 March 2011 (2011,3,17), the function will return 7.
int daysToNextThursday (int year, int month, int day) {
int Thursday;
Thursday = 7;
return (Thursday - day);
}
The code compiles correctly, but when I input a date, e.g. 16 3 2011, I do not get the right answer. Note this is apart of a larger amount of code that I have written, which works perfectly.
Any ideas?
Yes, I have an idea. My idea is that you go back and rethink the algorithm you've selected for determining that a day is Thursday. It's dead wrong :-)
Now, like a broken clock that's right twice a day, you may find input parameters that give you the correct answer but they'll be the exception rather than the rule.
If you want to figure out when next Thursday is from a given date, C provides date and time functions for exactly that purpose:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
static char *textday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
int main (int argc, char *argv[]) {
int year, month, day, today, thursday;
struct tm *mytm;
time_t mytime;
// Get all arguments (minimal error checks).
if (argc != 3) {
printf ("Usage: next_thursday <year> <month>\n");
return -1;
}
year = atoi (argv[1]);
month = atoi (argv[2]);
// Do first fourteen days of the month.
for (day = 1; day <= 14; day++) {
Up until there, it's just getting parameters and starting the loop. The meat of the calculation is below, setting up a useful struct tm and then forcing our year, month and day into it. Then mktime will fill in the tm_wday (day of week) field for us and we can use that to figure out the number of days till Thursday.
// Make the tm structure based on date (and midday).
mytime = time (0);
mytm = localtime (&mytime);
mytm->tm_year = year - 1900;
mytm->tm_mon = month - 1;
mytm->tm_mday = day;
mytm->tm_hour = 12;
mytime = mktime (mytm);
// Output filled in fields and days till next Thursday.
today = mytm->tm_wday;
thursday = (11 - today) % 7;
if (thursday == 0)
thursday = 7;
printf ("%04d-%02d-%02d, weekday = %d (%s), days till Thu = %d\n",
mytm->tm_year + 1900, mytm->tm_mon + 1, mytm->tm_mday,
today, textday[today], thursday);
}
return 0;
}
Note that the thursday calculation is a bit of modulus trickery - it's simply used to give us the number of days based on the following table:
today thursday
------- --------
0 (sun) 4
1 (mon) 3
2 (tue) 2
3 (wed) 1
4 (thu) 7
5 (fri) 6
6 (sat) 5
If you want a more readable solution, you can use:
if (today < 4) thursday = 4 - today;
else thursday = 11 - today;
This program outputs the following for 2011-03:
2011-03-01, weekday = 2 (Tue), days till Thu = 2
2011-03-02, weekday = 3 (Wed), days till Thu = 1
2011-03-03, weekday = 4 (Thu), days till Thu = 7
2011-03-04, weekday = 5 (Fri), days till Thu = 6
2011-03-05, weekday = 6 (Sat), days till Thu = 5
2011-03-06, weekday = 0 (Sun), days till Thu = 4
2011-03-07, weekday = 1 (Mon), days till Thu = 3
2011-03-08, weekday = 2 (Tue), days till Thu = 2
2011-03-09, weekday = 3 (Wed), days till Thu = 1
2011-03-10, weekday = 4 (Thu), days till Thu = 7
2011-03-11, weekday = 5 (Fri), days till Thu = 6
2011-03-12, weekday = 6 (Sat), days till Thu = 5
2011-03-13, weekday = 0 (Sun), days till Thu = 4
2011-03-14, weekday = 1 (Mon), days till Thu = 3

Resources