Uzyskanie numeru dnia i miesiąca w typie int

0

Mam pytanie, czy można uzyskać numer dnia i miesiąca jako int. Na razie napisałem coś takiego i niestety nie działa (dzień jest poprawny, ale miesiąc nie).

#include <stdio.h>
#include <time.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
struct data czas(void);

struct data {
    int dzien;
    int miesiac;
};

int main(void) {
    struct data cos= czas();
    printf("%d %d", cos.dzien, cos.miesiac);

    return 0;
}

struct data czas(void) {
    time_t czas = time(NULL);
    char* date_string = ctime(&czas);
    char dzien[3];
    int counter;    
    char month[4];
    char months[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Sep", "Oct", "Nov", "Dec"};    
    int returned;
    int month_num = 0;
    struct data date;

    for(counter = 8; counter < 10; counter++) {
        dzien[counter - 8] = date_string[counter];
    }

    for(counter = 5; counter < 8; counter++) { 
        month[counter - 5] =  date_string[counter];
    }

    for(counter = 0; counter < 12; counter++) {
        returned = strcmp(month, months[counter]);
        if(!returned){
            month_num = counter + 1;
        }
    }

    date.dzien = atoi(dzien);
    date.miesiac = month_num;

    return date;
}

Próbowałem coś ze strukturą tm, ale chyba nie działało

1

Może dlatego, że brakuje Augusta w tabeli :)

2

http://www.cplusplus.com/reference/ctime/gmtime/
http://www.cplusplus.com/reference/ctime/tm/


tajfunek napisał(a):

Próbowałem coś ze strukturą tm, ale chyba nie działało
"Chyba" to nie jest opis problemu! Zapewne problemem jest strefa czasowa (gmtime zawraca wartość dla UTC, a ty pewnie chcesz czes lokalny).
W tym wypadku zastosuj: localtime
http://www.cplusplus.com/reference/ctime/localtime/

1 użytkowników online, w tym zalogowanych: 0, gości: 1