C/C++ | time_t timestamp_sec = time(NULL); |
C++ 11 |
time_t timestamp_sec = std::chrono::duration_cast time_t timestamp_millisec = std::chrono::duration_cast |
Java |
long timestamp_sec = System.currentTimeMillis()/1000; long timestamp_millisec = System.currentTimeMillis(); |
C# |
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); long timestamp_sec = Convert.ToInt64(ts.TotalSeconds); long timestamp_millisec = Convert.ToInt64(ts.TotalMilliseconds); |
PHP |
$timestamp_sec = time(); list($msec, $sec) = explode(' ', microtime()); @timestamp_millisec = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000); |
Lua | timestamp_sec = = os.time([date]) |
Go | time.Now().Unix() |
JavaScript | var timestamp_sec = Math.floor(new Date().getTime()/1000.0); var timestamp_millisec = new Date().getTime(); |
Linux Shell | date +%s |
MySQL | SELECT unix_timestamp(now()) |
PostgreSQL | SELECT extract(epoch FROM now()); |
SQL Server | SELECT DATEDIFF(s, '1970-01-01 00:00:00', GETUTCDATE()) |
Oracle PL/SQL | SELECT (CAST(SYS_EXTRACT_UTC(SYSTIMESTAMP) AS DATE) - TO_DATE('01/01/1970','DD/MM/YYYY')) * 24 * 60 * 60 FROM DUAL; |
C/C++ |
char datetime[] = "2018-01-01 01:01:01";
struct tm tm_time; int unixtime; strptime(datetime, "%Y-%m-%d %H:%M:%S", &tm_time); unixtime = mktime(&tm_time); return unixtime; |
Java | long epoch = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2001-01-01 01:00:00"); |
C# |
DateTime dt = DateTime.ParseExact(dateString, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0)); long timestamp = (dt.Ticks - startTime.Ticks) / 10000; |
PHP | mktime(hour, minute, second, month, day, year) |
JavaScript | var humanTime = new Date(Date.UTC(year, month - 1, day, hour, minute, second)) |
Linux Shell | date +%s -d"Jan 1, 1970 00:00:01" |
MySQL | SELECT unix_timestamp(time) |
PostgreSQL | SELECT extract(epoch FROM date('YYYY-MM-DD HH:MM:SS')); |
SQL Server | SELECT DATEDIFF(s, '1970-01-01 00:00:00', time) |
C/C++ |
time_t t; char humanTime[20]; struct tm *tm_t; tm_t = localtime(&t); strftime(pcTime,128,"%F %T",tm_t); |
Java |
String time="2018-09-29 16:39:00"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); long lt = new Long(time); Date date = new Date(lt); |
C# | DateTime start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); DateTime date = start.AddMilliseconds(unixDate).ToLocalTime(); string strdatetime = date.ToString("yyyy-MM-dd HH:mm:ss"); |
PHP | date('r', Unix timestamp) |
JavaScript |
var unixTimestamp = new Date(Unix timestamp * 1000);
var humanTime = unixTimestamp.toLocaleString(); |
Linux Shell | date -d @Unix timestamp |
MySQL | SELECT from_unixtime(Unix timestamp) |
PostgreSQL | SELECT TIMESTAMP WITH TIME ZONE 'epoch' + Unix timestamp * INTERVAL '1 second'; |
SQL Server | DATEADD(s, Unix timestamp, '1970-01-01 00:00:00') |
深入解释:最初计算机操作系统是32位,而时间也是用32位表示。32位能表示的最大值是2147483647。另外1年365天的总秒数是31536000,2147483647/31536000 = 68.1,也就是说32位能表示的最长时间是68年,而实际上到2038年01月19日03时14分07秒,便会到达最大时间,过了这个时间点,所有32位操作系统时间便会变为10000000 00000000 00000000 00000000,也就是1901年12月13日20时45分52秒,这样便会出现时间回归的现象,很多软件便会运行异常了。
所以,因为用32位来表示时间的最大间隔是68年,而最早出现的UNIX操作系统考虑到计算机产生的年代和应用的时限综合取了1970年1月1日作为UNIX TIME的纪元时间(开始时间),至于时间回归的现象相信随着64位操作系统的产生逐渐得到解决,因为用64位操作系统可以表示到292,277,026,596年12月4日15时30分08秒。