Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include <stdbool.h>
  5.  
  6.  
  7. int main1() {
  8.  
  9. time_t now = time(NULL);
  10.  
  11. struct tm* datum = localtime(&now);
  12. printf("År:%d", datum->tm_year + 1900);
  13. if (datum->tm_mon + 1== 10) {
  14. printf("stjärthål");
  15.  
  16.  
  17. }
  18. if (datum->tm_wday == 3) {
  19.  
  20. printf("Röv");
  21. }
  22. else if (datum->tm_wday == 5) {
  23. printf("Carl");
  24. }
  25.  
  26. struct tm thomasBirthday;
  27. thomasBirthday.tm_year = 90;
  28. thomasBirthday.tm_mon = 2;
  29. thomasBirthday.tm_mday = 31;
  30. thomasBirthday.tm_hour = 0;
  31. thomasBirthday.tm_min = 0;
  32. thomasBirthday.tm_sec = 0;
  33.  
  34.  
  35. thomasBirthday.tm_mon += 8; // plussar på 8 månader
  36. mktime(&thomasBirthday); // måste lägga till denna för att det ska fixa till
  37. time_t thomasTime = mktime(&thomasBirthday);
  38.  
  39.  
  40. printf("år: %d",thomasBirthday.tm_year + 1900);
  41. printf("mån: %d",thomasBirthday.tm_mon + 1);
  42. printf("dag: %d",thomasBirthday.tm_mday);
  43.  
  44. }
  45.  
  46.  
  47. void main2() {
  48.  
  49.  
  50. time_t mytime = time(NULL);
  51. char* time_str = ctime(&mytime);
  52. time_str[strlen(time_str) - 1] = '\0';
  53. printf("Current Time : %s\n", time_str);
  54.  
  55.  
  56.  
  57. return 0;
  58.  
  59. }
  60.  
  61.  
  62. void main3() {
  63.  
  64. time_t now = time(NULL);
  65.  
  66. struct tm* datum = localtime(&now);
  67.  
  68.  
  69.  
  70. printf("year:%d\n", datum->tm_year + 1900);
  71. printf("mon:%d\n", datum->tm_mon + 1);
  72. printf("day:%d\n", datum->tm_mday);
  73. printf("hour:%d\n", datum->tm_hour);
  74. printf("min:%d\n", datum->tm_min);
  75. printf("sec:%d\n", datum->tm_sec);
  76.  
  77. }
  78.  
  79. int dayofweek(int d, int m, int y)
  80. {
  81. static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
  82. y -= m < 3;
  83. return (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7;
  84. }
  85.  
  86. void main4() {
  87.  
  88.  
  89. int day = dayofweek(7, 11, 2016);
  90. if (day == 1)
  91. printf("Måndag");
  92. else if (day == 2)
  93. printf("tis");
  94. else if (day == 3)
  95. printf("ons");
  96. else if (day == 4)
  97. printf("tors");
  98. else if (day == 5)
  99. printf("fre");
  100. else if (day == 6)
  101. printf("loerdag");
  102. else if (day == 0)
  103. printf("soendag");
  104.  
  105.  
  106.  
  107. }
  108.  
  109.  
  110. void main5() {
  111.  
  112. time_t now = time(NULL);
  113.  
  114. struct tm* date = localtime(&now);
  115.  
  116.  
  117.  
  118.  
  119. date->tm_mday = 23;
  120. date->tm_mon = 9;
  121. date->tm_year = 119;
  122. date->tm_mday += 40;
  123.  
  124. mktime(date);
  125.  
  126. printf("%d\n", date->tm_mday);
  127. printf("%d\n", date->tm_mon + 1);
  128. printf("%d\n", date->tm_year + 1900);
  129.  
  130. }
  131.  
  132. void main() {
  133. time_t now = time(NULL);
  134.  
  135. struct tm *date = localtime(&now);
  136.  
  137.  
  138.  
  139.  
  140. date->tm_mday = 23;
  141. date->tm_mon = 9;
  142. date->tm_year = 119;
  143.  
  144.  
  145.  
  146. for (int i = 0; i < 10; i++)
  147. {
  148. date->tm_mday += 40;
  149.  
  150. mktime(date);
  151. printf("dag:%d\n", date->tm_mday);
  152. printf("mon:%d\n", date->tm_mon + 1);
  153. printf("year:%d\n", date->tm_year + 1900);
  154. }
  155.  
  156. }
  157. void main8() {
  158. int month = 9;
  159. int year = 2020;
  160. int numberOfDays;
  161. if (month == 4 || month == 6 || month == 9 || month == 11)
  162. numberOfDays = 30;
  163. else if (month == 2)
  164. {
  165. bool isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
  166. if (isLeapYear)
  167. numberOfDays = 29;
  168. else
  169. numberOfDays = 28;
  170. }
  171. else
  172. numberOfDays = 31;
  173.  
  174. printf("year :%d mon: %d Have %d numb of days", year, month, numberOfDays);
  175. }
  176.  
  177.  
  178. time_t GetWhenStoppedWithNapp(struct tm birthday)
  179. {
  180. birthday.tm_mday += 100;
  181. return mktime(&birthday);
  182. }
  183.  
  184. int main()
  185. {
  186. time_t t = 1;//1970-01-01--00:00:01
  187. time_t tiden = time(NULL);
  188.  
  189. struct tm* tidStructPtr = localtime(&tiden);
  190. struct tm* gammalPtr = localtime(&t);
  191. struct tm kopia1 = *tidStructPtr; // NY kopia av tid struct
  192.  
  193. // mktime grejer!!!!
  194. struct tm stefansBirthday;
  195. stefansBirthday.tm_year = 1972 - 1900; // MINUS 1900 för time grejen
  196. stefansBirthday.tm_mon = 7;
  197. stefansBirthday.tm_mday = 3;
  198. stefansBirthday.tm_hour = 0;
  199. stefansBirthday.tm_min = 0;
  200. stefansBirthday.tm_sec = 0;
  201.  
  202. stefansBirthday.tm_mday += 40;
  203. // VVV sätter mktime till variabel stefansBirthdayTime men går utan också
  204. time_t stefansBirthdayTime = GetWhenStoppedWithNapp(stefansBirthday);
  205.  
  206.  
  207. mktime(&stefansBirthday); //DETTA går också (utan variabel)
  208.  
  209. printf("%d", stefansBirthday.tm_mday);
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement