最輕便&可靠的方法是使用ANSI C的mktime &本地時間功能,這將是C或者C++
更新工作:
回答有關如何實現自己的算法,而不是使用標準C庫米問題ktime函數,一個好的起點是查看已知的代碼,例如the glibc mktime source code。你會需要相關的花絮包括:
從glibc 2.17 (HEAD) mktime.c圍繞電線141:
#define TM_YEAR_BASE 1900
/* Return 1 if YEAR + TM_YEAR_BASE is a leap year. */
static inline int
leapyear (int year)
{
/* Don't add YEAR to TM_YEAR_BASE, as that might overflow.
Also, work even if YEAR is negative. */
return
((year & 3) == 0
&& (year % 100 != 0
|| ((year/100) & 3) == (- (TM_YEAR_BASE/100) & 3)));
}
從glibc 2.17 (HEAD) mktime.c開始行160:
const unsigned short int __mon_yday[2][13] =
{
/* Normal years. */
{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
/* Leap years. */
{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
};
一個C++類實現一個轉換年&構造dayOfYear到相應的月份& dayOfMonth可能看起來像這樣:
#include <iostream>
using namespace std;
#define MONTHS_IN_YEAR 12
class MyDateClass {
public:
MyDateClass(int year, int dayOfYear) {
int yearOffset = dayOfYear - TM_YEAR_BASE;
int leapYearIndex = leapyear(year) ? 1 : 0;
int daysInYear = leapYearIndex ? 366 : 365;
this->year = year;
this->dayOfYear = dayOfYear;
if (dayOfYear >= 1 && dayOfYear <= daysInYear) {
for (int mon = 0; mon < MONTHS_IN_YEAR; mon++) {
if (dayOfYear <= __mon_yday[leapYearIndex][mon+1]) {
month = mon + 1;
dayOfMonth = dayOfYear - __mon_yday[leapYearIndex][mon];
break;
}
}
} else {
cerr << "day of year must be between 1 and " << daysInYear << endl;
month = 0;
dayOfMonth = 0;
}
}
// Get month 1=January, 12=December
inline int getMonth() { return month; }
// Get day of month
inline int getDayOfMonth() { return dayOfMonth; }
// Get year
inline int getYear() { return year; }
// Get day of yar
inline int getDayOfYear() { return dayOfYear; }
private:
int month;
int dayOfMonth;
int year;
int dayOfYear;
};
希望我不會因爲顯示可能完成這項工作的示例代碼而投票贊成。隨意實現它,然而你想要的當然。這只是一個示例方法。
如果你寧願使用現有mktime功能(推薦),這應該最有可能出現在你的編程在任何平臺上的標準C庫,我原來的答覆內容如下...
當使用mktime你需要確保設置tm_mon = 0和tm_mday到一年(see mktime documentation for a better explanation)的日子,但簡而言之mktime忽略tm_wday和tm_yday只有轉化tm_mday,它本質上解釋爲如果您還設置了tm_mon = 0;
下面是說明了這一點,這將在C或C++工作的一些工作示例代碼:
#include <stdio.h> /* printf, scanf */
#include <time.h> /* time_t, struct tm, time, mktime */
#include <strings.h> /* bzero */
int main() {
time_t loctime;
struct tm timeinfo, *loctimeinfo;
int year, day;
/* prompt user for year and day-of-the-year */
printf ("Enter year: "); scanf ("%d",&year);
printf ("Enter day of the year: "); scanf ("%d",&day);
/* initialize timeinfo and modify it to the user's choice */
bzero(&timeinfo, sizeof(struct tm));
timeinfo.tm_isdst = -1; /* Allow mktime to determine DST setting. */
timeinfo.tm_mon = 0;
timeinfo.tm_mday = day;
timeinfo.tm_year = year - 1900;
loctime = mktime (&timeinfo);
loctimeinfo = localtime(&loctime);
printf ("The date for that day of the year is %s.\n", asctime(loctimeinfo));
return 0;
}
編譯&樣品運行:
$ g++ -o t2 t2.c
$ ./t2
Enter year: 2013
Enter day of the year: 1
The date for that day of the year is Tue Jan 1 00:00:00 2013
$ ./t2
Enter year: 2013
Enter day of the year: 365
The date for that day of the year is Tue Dec 31 00:00:00 2013
即使適用於閏年像2012:
$ ./t2
Enter year: 2012
Enter day of the year: 366
The date for that day of the year is Mon Dec 31 00:00:00 2012
轉換'YEAR-01-01 00:00:00'到時代。加'(天 - 1)* 3600 * 24'秒,然後轉換回日期。 – syam
我會確定一年的第一天的時間戳,然後添加'(day-1)* 24 * 60 * 60'並將新的時間戳轉換爲日期。這會給你一個你想要的格式的日期。 – gpalex
你到目前爲止嘗試過什麼 - 我問的原因是要了解你正在採取哪種方法,以便我們能給你「正確的」答案。使用C的 –