Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <time.h>
- #include <stdbool.h>
- int main1() {
- time_t now = time(NULL);
- struct tm* datum = localtime(&now);
- printf("År:%d", datum->tm_year + 1900);
- if (datum->tm_mon + 1== 10) {
- printf("stjärthål");
- }
- if (datum->tm_wday == 3) {
- printf("Röv");
- }
- else if (datum->tm_wday == 5) {
- printf("Carl");
- }
- struct tm thomasBirthday;
- thomasBirthday.tm_year = 90;
- thomasBirthday.tm_mon = 2;
- thomasBirthday.tm_mday = 31;
- thomasBirthday.tm_hour = 0;
- thomasBirthday.tm_min = 0;
- thomasBirthday.tm_sec = 0;
- thomasBirthday.tm_mon += 8; // plussar på 8 månader
- mktime(&thomasBirthday); // måste lägga till denna för att det ska fixa till
- time_t thomasTime = mktime(&thomasBirthday);
- printf("år: %d",thomasBirthday.tm_year + 1900);
- printf("mån: %d",thomasBirthday.tm_mon + 1);
- printf("dag: %d",thomasBirthday.tm_mday);
- }
- void main2() {
- time_t mytime = time(NULL);
- char* time_str = ctime(&mytime);
- time_str[strlen(time_str) - 1] = '\0';
- printf("Current Time : %s\n", time_str);
- return 0;
- }
- void main3() {
- time_t now = time(NULL);
- struct tm* datum = localtime(&now);
- printf("year:%d\n", datum->tm_year + 1900);
- printf("mon:%d\n", datum->tm_mon + 1);
- printf("day:%d\n", datum->tm_mday);
- printf("hour:%d\n", datum->tm_hour);
- printf("min:%d\n", datum->tm_min);
- printf("sec:%d\n", datum->tm_sec);
- }
- int dayofweek(int d, int m, int y)
- {
- static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
- y -= m < 3;
- return (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7;
- }
- void main4() {
- int day = dayofweek(7, 11, 2016);
- if (day == 1)
- printf("Måndag");
- else if (day == 2)
- printf("tis");
- else if (day == 3)
- printf("ons");
- else if (day == 4)
- printf("tors");
- else if (day == 5)
- printf("fre");
- else if (day == 6)
- printf("loerdag");
- else if (day == 0)
- printf("soendag");
- }
- void main5() {
- time_t now = time(NULL);
- struct tm* date = localtime(&now);
- date->tm_mday = 23;
- date->tm_mon = 9;
- date->tm_year = 119;
- date->tm_mday += 40;
- mktime(date);
- printf("%d\n", date->tm_mday);
- printf("%d\n", date->tm_mon + 1);
- printf("%d\n", date->tm_year + 1900);
- }
- void main() {
- time_t now = time(NULL);
- struct tm *date = localtime(&now);
- date->tm_mday = 23;
- date->tm_mon = 9;
- date->tm_year = 119;
- for (int i = 0; i < 10; i++)
- {
- date->tm_mday += 40;
- mktime(date);
- printf("dag:%d\n", date->tm_mday);
- printf("mon:%d\n", date->tm_mon + 1);
- printf("year:%d\n", date->tm_year + 1900);
- }
- }
- void main8() {
- int month = 9;
- int year = 2020;
- int numberOfDays;
- if (month == 4 || month == 6 || month == 9 || month == 11)
- numberOfDays = 30;
- else if (month == 2)
- {
- bool isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
- if (isLeapYear)
- numberOfDays = 29;
- else
- numberOfDays = 28;
- }
- else
- numberOfDays = 31;
- printf("year :%d mon: %d Have %d numb of days", year, month, numberOfDays);
- }
- time_t GetWhenStoppedWithNapp(struct tm birthday)
- {
- birthday.tm_mday += 100;
- return mktime(&birthday);
- }
- int main()
- {
- time_t t = 1;//1970-01-01--00:00:01
- time_t tiden = time(NULL);
- struct tm* tidStructPtr = localtime(&tiden);
- struct tm* gammalPtr = localtime(&t);
- struct tm kopia1 = *tidStructPtr; // NY kopia av tid struct
- // mktime grejer!!!!
- struct tm stefansBirthday;
- stefansBirthday.tm_year = 1972 - 1900; // MINUS 1900 för time grejen
- stefansBirthday.tm_mon = 7;
- stefansBirthday.tm_mday = 3;
- stefansBirthday.tm_hour = 0;
- stefansBirthday.tm_min = 0;
- stefansBirthday.tm_sec = 0;
- stefansBirthday.tm_mday += 40;
- // VVV sätter mktime till variabel stefansBirthdayTime men går utan också
- time_t stefansBirthdayTime = GetWhenStoppedWithNapp(stefansBirthday);
- mktime(&stefansBirthday); //DETTA går också (utan variabel)
- printf("%d", stefansBirthday.tm_mday);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement