delphi中有C语言中time_t类型到日期型转换的函数吗?(100分)

  • 主题发起人 主题发起人 hawkview
  • 开始时间 开始时间
H

hawkview

Unregistered / Unconfirmed
GUEST, unregistred user!
请指教。
 
time_t类型,C学得不好,还真不记得有time_t这玩意
 
是个长整型,表示从1970年0分0秒到现在所经过的秒数。
 
time_t在C中好象没有吧!<br>在Delphi可以用FormatDateTime<br>
 
time_t是长整型在Delphi中对应LongInt
 
我知道它是longint,我想问的是有无将此数值转换成Tsystemtime的函数。
 
#include &lt;time.h&gt;<br><br>&nbsp; &nbsp; &nbsp; &nbsp;time_t time(time_t *t);<br>-------------------------------------------------------------------------------<br>&nbsp; &nbsp; * time 返回日历时间, 即 1970 年 1 月 1 日 0:00:00 (UTC) 开始的秒数, 称为 Epoch.<br>===============================================================================<br><br>日历时间到分解时间的转换: gmtime, localtime <br>===============================================================================<br>&nbsp; &nbsp; &nbsp; &nbsp;#include &lt;time.h&gt;<br><br>&nbsp; &nbsp; &nbsp; &nbsp;struct tm *gmtime (const time_t *timep);<br>&nbsp; &nbsp; &nbsp; &nbsp;struct tm *localtime (const time_t *timep);<br>-------------------------------------------------------------------------------<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; struct tm<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int tm_sec; &nbsp; &nbsp; &nbsp; &nbsp; /* seconds &nbsp;[0, 61]*/<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int tm_min; &nbsp; &nbsp; &nbsp; &nbsp; /* minutes [0, 59]*/<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int tm_hour; &nbsp; &nbsp; &nbsp; &nbsp;/* hours [0, 23]*/<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int tm_mday; &nbsp; &nbsp; &nbsp; &nbsp;/* day of the month [1, 31]*/<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int tm_mon; &nbsp; &nbsp; &nbsp; &nbsp; /* month [0, 11]*/<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int tm_year; &nbsp; &nbsp; &nbsp; &nbsp;/* years since 1900*/<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int tm_wday; &nbsp; &nbsp; &nbsp; &nbsp;/* day of the week [0, 6]*/<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int tm_yday; &nbsp; &nbsp; &nbsp; &nbsp;/* day in the year [0, 365]*/<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int tm_isdst; &nbsp; &nbsp; &nbsp; /* daylight saving time */<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };<br>-------------------------------------------------------------------------------<br>&nbsp; &nbsp; * gmtime 不转换为本地时间, 即忽略系统的时区设置.<br>&nbsp; &nbsp; * localtime 转换为本地时间, 包括时区和夏时制.<br>===============================================================================<br><br>分解时间到日历时间的转换: mktime <br>===============================================================================<br>&nbsp; &nbsp; &nbsp; &nbsp;#include &lt;time.h&gt;<br><br>&nbsp; &nbsp; &nbsp; &nbsp;time_t mktime(struct tm *timeptr);<br>-------------------------------------------------------------------------------<br>&nbsp; &nbsp; * 受 TZ 和 LC_TIME 的影响<br>===============================================================================<br><br>生成可读时间字符串: asctime, ctime <br>===============================================================================<br>&nbsp; &nbsp; &nbsp; &nbsp;#include &lt;time.h&gt;<br><br>&nbsp; &nbsp; &nbsp; &nbsp;char *asctime(const struct tm *timeptr);<br>&nbsp; &nbsp; &nbsp; &nbsp;char *ctime(const time_t *timep);<br>-------------------------------------------------------------------------------<br>&nbsp; &nbsp; * asctime 的参数是分解时间<br>&nbsp; &nbsp; * ctime 的参数是日历时间<br>&nbsp; &nbsp; * ctime 受 TZ 和 LC_TIME 的影响<br>===============================================================================<br><br>格式化时间: strftime <br>===============================================================================<br>&nbsp; &nbsp; &nbsp; &nbsp;#include &lt;time.h&gt;<br><br>&nbsp; &nbsp; &nbsp; &nbsp;size_t strftime (char *s, size_t max, const char *format,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;const struct tm *tm);<br>-------------------------------------------------------------------------------<br>&nbsp; &nbsp; * format 指定格式信息<br>&nbsp; &nbsp; * 有将近 50 个格式化字符, 包括: %a (简写星期), %B (完整月份名称) 等等.<br>-------------------------------------------------------------------------------<br><br>
 
就用strtodate()
 
C语言里我知道怎么转,问题是我接受的数据包里(从unix主机到delphi)只有time_t(longint)型的一个字段,我就是想问在delphi里有无类似C中localtime()的函数?
 
简单的数学计算问题<br><br>year,month,day,hour,minute,second:Word;<br>startTime,destTime:TDateTime;<br><br>begin<br>year:=1970<br>month:=1<br>day:=1<br>Hour:=0;<br>Minute:=0;<br>Second:=0;<br><br>startTime:=EncodeDateTime(year,month,day,hour,minute,second);<br>destTime:=IncSecond(startTime,mytime_t); // mytime_t就是你的数据<br><br>end;
 
Delphi的日期时间函数:<br>日期时间类型有:<br>TDateTime(可以和字符串互相转换,DateTimeToStr和StrtoDateTime)、<br>TTimeStamp(包含Time和Date两个32位整型成员)、<br>Comp(64位整型,以milliseconds毫秒表示的时间,不是秒,如果time_t是以秒表示的,那么转换时须将它乘以1000)<br>TSystemTime(Windows标准类型)<br><br>转换函数:<br>Datetime和Timestamp:DatetimeToTimestamp,TimestampToDatetime<br>Timestamp和Comp:TimestampToMsecs,MsecsToTimestamp<br>Datetime和Systime:DateTimeToSystemTime,SystemTimeToDateTime<br><br>time_t是个整型,转换到其它类型:<br>t1:=MsecsToTimestamp(time_t);<br>t2:=TimestampToDatetime(t1);<br>t3:=DatetimeToSystemTime(t2);<br><br>
 
如果楼主用的是Delphi 7的话,那么可以用下面两个函数,记得use DateUtils,<br>之前的版本有没有我没有注意,但是Delphi 7是有的。<br>function DateTimeToUnix(const AValue: TDateTime): Int64;<br>function UnixToDateTime(const AValue: Int64): TDateTime;<br>
 
后退
顶部