<pre name="code" class="cpp">/************************************************************************* > File Name: 三种进制输出 > Author: kanejzh > Mail: jingzhanghao@gmail.com > Created Time: Sun 03 May 2015 09:13:35 AM CST ************************************************************************/ static const char hexconv[] = "0123456789abcdef"; static const char binconv[] = "01"; /*---------------------------------------------------------------------------*/ void putstring(char *s) { while(*s) { putchar(*s++); } } /*---------------------------------------------------------------------------*/ void puthex(uint8_t c) { putchar(hexconv[c >> 4]); putchar(hexconv[c & 0x0f]); } /*---------------------------------------------------------------------------*/ void putbin(uint8_t c) { unsigned char i = 0x80; while(i) { putchar(binconv[(c & i) != 0]); i >>= 1; } } /*---------------------------------------------------------------------------*/ /************************************************************************* > File Name: 判断当天是当年第几周的第几天 > Author: kanejzh > Mail: jingzhanghao@gmail.com > Created Time: Sun 03 May 2015 09:13:35 AM CST ************************************************************************/ #include<stdlib.h> #include<stdio.h> #include<time.h> static bool is_leap(int n){ return (n%400==0)||(n%4==0&&n%100!=0); } static int get_xth_day_of_date(int year, int month, int day){ int a[] = {0, 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int d = day; a[2] = is_leap(year) ? 29 : 28; for ( int i=1; i<month; ++i ){ d += a[i]; } return d; } static int get_xday_of_date(int year, int month, int day){ // 1900-01-01: Monday int d = 0, i, t; for ( i=1900; i<year; ++i ){ d += is_leap(i) ? 366 : 365; } d += get_xth_day_of_date(year, month, day); t = d%7; return t ? t : 7; } static int get_xweek_of_date(int year, int month, int day){ int first_monday_th, day_th; int t, i; for ( i=1; i<=7; ++i ){ t = get_xday_of_date(2012, 1, i); if ( t == 1 ){ break; } } first_monday_th = i; day_th = get_xth_day_of_date(year, month, day); if ( first_monday_th == 1 ){ return (day_th-first_monday_th)/7+1; } else { if ( day_th < first_monday_th ){ return 1; } else { return (day_th-first_monday_th)/7+2; } } } void print_xth_week_xth_day(int year, int month, int day){ printf("%d-%d-%d 第%d周第%d天\n", year, month, day, get_xweek_of_date(year, month, day), get_xday_of_date(year, month, day)); } struct tm *get_date_of_today(){ time_t nowTime; time(&nowTime); struct tm *sysTime = localtime(&nowTime); sysTime->tm_year += 1900; sysTime->tm_mon += 1; return sysTime; } int main(){ struct tm *sysTime = get_date_of_today(); print_xth_week_xth_day(sysTime->tm_year, sysTime->tm_mon, sysTime->tm_mday); return 0; }
原文地址:http://blog.csdn.net/jzh1988903/article/details/45739935