Make comparison IR ops layout agnostic
[hiphop-php.git] / hphp / runtime / base / timestamp.cpp
blobd8de9754c678a019b86b424555d633ff38da4548
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
16 #include "hphp/runtime/base/timestamp.h"
18 #include <folly/portability/SysTime.h>
20 extern "C" {
21 #include <timelib.h>
24 #include <chrono>
26 #include "hphp/runtime/base/array-init.h"
27 #include "hphp/runtime/base/datetime.h"
28 #include "hphp/runtime/base/resource-data.h"
29 #include "hphp/runtime/base/type-array.h"
30 #include "hphp/runtime/base/type-string.h"
31 #include "hphp/util/timer.h"
33 namespace HPHP {
34 ///////////////////////////////////////////////////////////////////////////////
35 // creation
37 int64_t TimeStamp::Current() {
38 return time(0);
41 double TimeStamp::CurrentSecond() {
42 auto now_ns = gettime_ns(CLOCK_REALTIME);
43 using DoubleSeconds =
44 std::chrono::duration<double, std::chrono::seconds::period>;
45 auto now_double_secs = DoubleSeconds(std::chrono::nanoseconds(now_ns));
46 return now_double_secs.count();
49 const StaticString
50 s_sec("sec"),
51 s_usec("usec"),
52 s_minuteswest("minuteswest"),
53 s_dsttime("dsttime");
55 Array TimeStamp::CurrentTime() {
56 struct timeval tp;
57 gettimeofday(&tp, nullptr);
59 timelib_time_offset *offset =
60 timelib_get_time_zone_info(tp.tv_sec, TimeZone::Current()->getTZInfo());
62 auto const ret = make_darray(
63 s_sec, (int)tp.tv_sec,
64 s_usec, (int)tp.tv_usec,
65 s_minuteswest, (int)(-offset->offset / 60),
66 s_dsttime, (int)offset->is_dst
68 timelib_time_offset_dtor(offset);
69 return ret;
72 String TimeStamp::CurrentMicroTime() {
73 struct timeval tp;
74 gettimeofday(&tp, nullptr);
75 char ret[100];
76 snprintf(ret, 100, "%.8F %ld", (double)tp.tv_usec / 1000000, tp.tv_sec);
77 return String(ret, CopyString);
80 int64_t TimeStamp::Get(bool &error, int hou, int min, int sec, int mon, int day,
81 int yea, bool gmt) {
82 auto dt = req::make<DateTime>(Current());
83 if (gmt) {
84 dt->setTimezone(req::make<TimeZone>("UTC"));
86 dt->set(hou, min, sec, mon, day, yea);
87 return dt->toTimeStamp(error);
90 ///////////////////////////////////////////////////////////////////////////////