/*---------------------------------------------------------------------- | NPT_DateTime::ToTimeStamp +---------------------------------------------------------------------*/ NPT_Result NPT_DateTime::ToTimeStamp(NPT_TimeStamp& timestamp) const { // default value timestamp.SetNanos(0); // check bounds NPT_Result result = CheckDate(*this); if (NPT_FAILED(result)) return result; // compute the number of days elapsed since 1900 NPT_UInt32 days = ElapsedDaysSince1900(*this); // compute the number of nanoseconds NPT_Int64 seconds = (NPT_Int64)days * (24*60*60) + (NPT_Int64)m_Hours * (60*60) + (NPT_Int64)m_Minutes * (60) + (NPT_Int64)m_Seconds; seconds -= (NPT_Int64)m_TimeZone*60; // adjust to the number of seconds since 1900 seconds -= (NPT_Int64)NPT_SECONDS_PER_YEAR*70 + (NPT_Int64)(17*NPT_SECONDS_PER_DAY); // 17 leap year between 1900 and 1970 timestamp.FromNanos(seconds * 1000000000 + m_NanoSeconds); return NPT_SUCCESS; }
/*---------------------------------------------------------------------- | NPT_System::GetCurrentTimeStamp +---------------------------------------------------------------------*/ NPT_Result NPT_System::GetCurrentTimeStamp(NPT_TimeStamp& now) { struct timeval now_tv; // get current time from system if (gettimeofday(&now_tv, NULL)) { now.SetNanos(0); return NPT_FAILURE; } // convert format now.SetNanos((NPT_UInt64)now_tv.tv_sec * 1000000000 + (NPT_UInt64)now_tv.tv_usec * 1000); return NPT_SUCCESS; }
/*---------------------------------------------------------------------- | NPT_DateTime::ChangeTimeZone +---------------------------------------------------------------------*/ NPT_Result NPT_DateTime::ChangeTimeZone(NPT_Int32 timezone) { if (timezone < -12*60 || timezone > 12*60) { return NPT_ERROR_OUT_OF_RANGE; } NPT_TimeStamp ts; NPT_Result result = ToTimeStamp(ts); if (NPT_FAILED(result)) return result; ts.SetNanos(ts.ToNanos()+(NPT_Int64)timezone*(NPT_Int64)60*(NPT_Int64)1000000000); result = FromTimeStamp(ts); m_TimeZone = timezone; return result; }
/*---------------------------------------------------------------------- | NPT_System::GetCurrentTimeStamp +---------------------------------------------------------------------*/ NPT_Result NPT_System::GetCurrentTimeStamp(NPT_TimeStamp& now) { struct _timeb time_stamp; #if defined(_MSC_VER) && (_MSC_VER >= 1400) _ftime_s(&time_stamp); #else _ftime(&time_stamp); #endif now.SetNanos(((NPT_UInt64)time_stamp.time) * 1000000000UL + ((NPT_UInt64)time_stamp.millitm) * 1000000); return NPT_SUCCESS; }