Show weekday for given year

I have been given an assignment which I have big troubles with. The assignment is:
"If I ask you “When is the weekday of the 2nd of August in 1429?”, you can answer immediately with your program to my question.

The initial value for the 1st of January in (the year) 1 will be sought by inducing, for example the crucifixion of Jesus is said to be held on Friday, the 3rd of April, AD 33."

How can I solve this?
Don't forget to incorporate the change from the Julian calendar to the Gregorian calendar in your program.
1) You should know weekday of some day. (Best if you use Sunday)
2) Find, how many days between yor reference date and date you want to calculate weekday for.
3) Caculate weekday:
1
2
3
4
5
//dist is the difference between needed and reference date.
int day_of_week = ((dist % 7) + 7) % 7;
//Due to processors giving mathematically incorrect result with modulus of negative numbers we use this bulky formula. 

//Now we have weekday in day_of_veek variable with 0 being Sunday and 6 being Saturday. 


Also <chrono> library have native functions to determine day of week.
I can not take credit for this code, I wish I had the original link, but afraid it is lost.

I saved it as a great example of using time_t. It should give you examples that may be useful.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>      // Needed for printf()
#include <time.h>       // Needed for time data structures and functions

// Adjust for Daylight savings as needed
// Sunday, March 31 & Sunday, October 27 2013
#define PST (-8)
#define MST (-7)
#define CST (-6)
#define EST (-5)
#define UTC (0)
#define BST (+1)
#define CCT (+8)

int leap_year();

void TimeDump()
{
  time_t timer;                // Define the timer
  struct tm *tblock;           // Define a structure for time block

  // Get time of day
  timer = time(NULL);

  // Converts date/time to a structure
  tblock = localtime(&timer);

  // Output ASCII data/time
  printf("Local time is: %s", asctime(tblock));

  // Output members of tm structure
  printf("DST          = %d \n", tblock->tm_isdst);
  printf("Julian date  = %d \n", tblock->tm_yday);
  printf("Week day     = %d \n", tblock->tm_wday);
  printf("Year         = %d \n", tblock->tm_year +1900); // based on 1900
  printf("Month        = %d \n", tblock->tm_mon +1); //month (0 to 11)
  printf("Month day    = %d \n", tblock->tm_mday);  // Starts Monday
  printf("Hour         = %d \n", tblock->tm_hour);
  printf("Minutes      = %d \n", tblock->tm_min);
  printf("Seconds      = %d \n", tblock->tm_sec);
  
}

// Show Time Zones
int TimeZone ()
{
  time_t rawtime;
  struct tm * ptm;

  time ( &rawtime );

  ptm = gmtime ( &rawtime );
  printf ("\n");
  printf ("Location        UTC   \t :  Time\n");
  printf ("Pacific Time   (UTC-7)\t :  %2d:%02d\n", (ptm->tm_hour+PST)%24, ptm->tm_min);
  printf ("Mountain Time  (UTC-6)\t :  %2d:%02d\n", (ptm->tm_hour+MST)%24, ptm->tm_min);
  printf ("Central Time   (UTC-5)\t :  %2d:%02d\n", (ptm->tm_hour+CST)%24, ptm->tm_min);
  printf ("Eastern Time   (UTC-4)\t :  %2d:%02d\n", (ptm->tm_hour+EST)%24, ptm->tm_min);
  printf ("\n");
  printf ("Universal Time (UTC0) \t :  %2d:%02d\n", (ptm->tm_hour+UTC)%24, ptm->tm_min);
  printf ("\n");
  printf ("London England (UTC+0)\t :  %2d:%02d\n", (ptm->tm_hour+BST)%24, ptm->tm_min); 
  printf ("Beijing China  (UTC+8)\t :  %2d:%02d\n", (ptm->tm_hour+CCT)%24, ptm->tm_min);
}

int main(void)
{
    TimeDump();
    TimeZone ();
    return 0;
}
Topic archived. No new replies allowed.