당신은 시간을 time_t와 유사한 (연속) 숫자로 & 날짜가있는 경우, 당신은 단순히 등등 전체 분 (60 %), 시간,를 얻기 위해 모듈을 사용할 수 있습니다.
내 경험에 따르면 값은 실시간으로 정렬됩니다 (모듈로 60은 1 분에 발생 함). 그러나 이것이 어디에서나 보장 될 가능성은 거의 없습니다. 초 트림하는 코드 위
/*
* Returns millisecond timing (in seconds) for the current time.
*
* Note: This function should be called once in single-threaded mode in Win32,
* to get it initialized.
*/
double now_secs(void) {
#if (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC)
/*
* Windows FILETIME values are "100-nanosecond intervals since
* January 1, 1601 (UTC)" (MSDN). Well, we'd want Unix Epoch as
* the offset and it seems, so would they:
*
* <http://msdn.microsoft.com/en-us/library/ms724928(VS.85).aspx>
*/
SYSTEMTIME st;
FILETIME ft;
ULARGE_INTEGER uli;
static ULARGE_INTEGER uli_epoch; // Jan 1st 1970 0:0:0
if (uli_epoch.HighPart==0) {
st.wYear= 1970;
st.wMonth= 1; // Jan
st.wDay= 1;
st.wHour= st.wMinute= st.wSecond= st.wMilliseconds= 0;
//
st.wDayOfWeek= 0; // ignored
if (!SystemTimeToFileTime(&st, &ft))
FAIL("SystemTimeToFileTime", GetLastError());
uli_epoch.LowPart= ft.dwLowDateTime;
uli_epoch.HighPart= ft.dwHighDateTime;
}
GetSystemTime(&st); // current system date/time in UTC
if (!SystemTimeToFileTime(&st, &ft))
FAIL("SystemTimeToFileTime", GetLastError());
uli.LowPart= ft.dwLowDateTime;
uli.HighPart= ft.dwHighDateTime;
/* 'double' has less accuracy than 64-bit int, but if it were to degrade,
* it would do so gracefully. In practise, the integer accuracy is not
* of the 100ns class but just 1ms (Windows XP).
*/
return (double)(uli.QuadPart - uli_epoch.QuadPart)/10000000.0;
#else
struct timeval tv;
// {
// time_t tv_sec; /* seconds since Jan. 1, 1970 */
// suseconds_t tv_usec; /* and microseconds */
// };
int rc= gettimeofday(&tv, NULL /*time zone not used any more (in Linux)*/);
assert(rc==0);
return ((double)tv.tv_sec) + ((tv.tv_usec)/1000)/1000.0;
#endif
}
나는 이것을 매우 좋아한다. 3.5 프레임 워크를 사용했다면, 더 좋은 것이없는 한 내가 취할 수있는 길이다. 불행히도 2.0을 사용하고 있으므로 첫 번째 대답을 고수해야합니다. 감사! –
범용 확장 메서드로 만들려면 DateTimeKind (지정되지 않음/Utc/Local)를 유지하는 것이 좋습니다. return new DateTime (date.Ticks - date.Ticks % roundTicks, date.Kind); – Joe
... 또는 Kind 속성도 유지하는 한 줄 짜기 : d = d.AddTicks (- (d.Ticks + 30 * TimeSpan.TicksPerSecond) % TimeSpan.TicksPerMinute); – Joe