统一DateTime::Now()表示当前时间

This commit is contained in:
nnhy 2016-05-28 08:26:30 +00:00
parent d759369e2a
commit 3d0f6a75bb
8 changed files with 29 additions and 28 deletions

View File

@ -224,7 +224,7 @@ void HardRTC::SaveTicks()
if(g_NextSave == 0)
{
debug_printf("LoadTime: ");
Time.Now().Show(true);
DateTime::Now().Show(true);
g_Counter = 0;
}
@ -244,7 +244,7 @@ void HardRTC::SaveTicks()
// 设置计数器
RTC_SetCounter(ms);
#else
RTC_SetCounter(Time.Now());
RTC_SetCounter(DateTime::Now());
#endif
// 必须打开时钟和后备域,否则写不进去时间

17
Sys.cpp
View File

@ -198,7 +198,7 @@ TSys::TSys()
void ShowTime(void* param)
{
debug_printf("\r");
Time.Now().Show();
DateTime::Now().Show();
debug_printf(" ");
}
@ -387,7 +387,7 @@ void TSys::ShowInfo() const
debug_printf("Stack:(0x%08x, 0x%08x) = 0x%x (%dk)\r\n", start, end, size, size >> 10);
debug_printf("Time : ");
Time.Now().Show(true);
DateTime::Now().Show(true);
debug_printf("Support: http://www.NewLifeX.com\r\n");
debug_printf("\r\n");
@ -439,7 +439,7 @@ bool TSys::SetTaskPeriod(uint taskid, int period) const
if(period)
{
task->Period = period;
// 改变任务周期的同时重新计算下一次调度时间NextTime让它立马生效
// 否则有可能系统按照上一次计算好的NextTime再调度一次任务
task->NextTime = Ms() + period;
@ -459,7 +459,7 @@ void TSys::Start()
#endif
Started = true;
#if DEBUG
//AddTask(ShowTime, nullptr, 2000000, 2000000);
#endif
@ -512,6 +512,15 @@ UInt64 TSys::Ms() const { return Time.Current(); }
// 系统绝对当前时间,秒
uint TSys::Seconds() const { return Time.Seconds + Time.BaseSeconds; }
// 当前时间
DateTime DateTime::Now()
{
DateTime dt(Sys.Seconds());
dt.Ms = Sys.Ms();
return dt;
}
void TSys::Sleep(uint ms) const
{
// 优先使用线程级睡眠

View File

@ -1,4 +1,6 @@
#include "Task.h"
#include "DateTime.h"
#include "Task.h"
#include "Time.h"
Task::Task()
@ -312,7 +314,7 @@ void TaskScheduler::ShowStatus(void* param)
auto host = (TaskScheduler*)param;
debug_printf("Task::ShowStatus 平均 %dus 最大 %dus 当前 ", host->Cost, host->MaxCost);
Time.Now().Show();
DateTime::Now().Show();
debug_printf(" 启动 ");
DateTime dt(Sys.Ms() / 1000);
dt.Show(true);

View File

@ -135,7 +135,7 @@ static void TestConcat()
debug_printf("字符串连接测试\r\n");
auto now = Time.Now();
auto now = DateTime::Now();
//char cs[32];
//debug_printf("now: %d %s\r\n", now.Second, now.GetString('F', cs));
@ -198,7 +198,7 @@ static void TestAdd()
TS("TestAdd");
String str = R("字符串连加 ");
str = str + 1234 + "#" + R("99xx") + '$' + -33.883 + "@" + Time.Now();
str = str + 1234 + "#" + R("99xx") + '$' + -33.883 + "@" + DateTime::Now();
str.Show(true);
// 字符串连加 1234@0000-00-00 00:00:00#99xx
assert(str.Contains("字符串连加 1234#99xx$-33.88@"), "friend StringHelper& operator + (const StringHelper& lhs, const char* cstr)");

View File

@ -173,14 +173,6 @@ void TTime::SetTime(UInt64 seconds)
#pragma arm section code
// 当前时间
DateTime TTime::Now() const
{
DateTime dt(Seconds + BaseSeconds);
//dt.Millisecond = Milliseconds;
return dt;
}
#if !defined(TINY) && defined(STM32F0)
#pragma arm section code = "SectionForSys"
#endif

3
Time.h
View File

@ -41,9 +41,6 @@ public:
void Sleep(uint ms, bool* running = nullptr) const;
// 微秒级延迟
void Delay(uint us) const;
// 当前时间。
DateTime Now() const;
};
extern const TTime Time;

View File

@ -15,7 +15,7 @@ HelloMessage::HelloMessage() : Cipher(1), Key(0)
ushort code = _REV16(Sys.Code);
Type = Buffer(&code, 2).ToHex();
Name = Sys.Company;
LocalTime = Time.Now().TotalMs();
LocalTime = DateTime::Now().TotalMs();
Cipher[0] = 1;
Protocol = 17;
@ -150,8 +150,9 @@ String& HelloMessage::ToStr(String& str) const
TokenPingMessage::TokenPingMessage()
{
LocalTime = Time.Now().TotalMs();
LocalTime = DateTime::Now().TotalMs();
}
TokenPingMessage::TokenPingMessage(const TokenPingMessage& msg)
{
LocalTime = msg.LocalTime;
@ -165,6 +166,7 @@ bool TokenPingMessage::Read(Stream& ms)
return true;
}
// 把消息写入数据流中
void TokenPingMessage::Write(Stream& ms) const
{

View File

@ -215,7 +215,7 @@ void TokenClient::SayHello(bool broadcast, int port)
HelloMessage ext(Hello);
ext.Reply = false;
ext.LocalTime = Time.Now().TotalMs();
ext.LocalTime = DateTime::Now().TotalMs();
// 设置握手广播的本地地址和端口
//auto socket = dynamic_cast<ISocket*>(Control->Port);
@ -273,9 +273,8 @@ bool TokenClient::OnHello(TokenMessage& msg, Controller* ctrl)
// 握手完成后马上注册或登录
Sys.SetTask(_task, true, 0);
DateTime dt(ext.LocalTime / 1000UL);
// 同步本地时间
if(ext.LocalTime > 0) ((TTime&)Time).SetTime(dt.TotalSeconds());
if(ext.LocalTime > 0) ((TTime&)Time).SetTime(ext.LocalTime / 1000);
}
return true;
@ -311,7 +310,7 @@ bool TokenClient::OnLocalHello(TokenMessage& msg, Controller* ctrl)
ext2.Cipher = "RC4";
//ext2.LocalTime = ext.LocalTime;
// 使用当前时间
ext2.LocalTime = Time.Now().TotalMs();
ext2.LocalTime = DateTime::Now().TotalMs();
ext2.WriteMessage(rs);
Reply(rs, ctrl);
@ -571,7 +570,7 @@ bool TokenClient::OnPing(TokenMessage& msg, Controller* ctrl)
pinMsg.ReadMessage(msg);
UInt64 start = pinMsg.LocalTime;
UInt64 now = Time.Now().TotalMs();
UInt64 now = DateTime::Now().TotalMs();
int cost = (int)(now - start);
if(cost < 0) cost = -cost;
// if(cost > 1000) ((TTime&)Time).SetTime(start / 1000);