如何将COledatetime转换为unix时间/php时间?

发布于 2024-08-22 05:11:14 字数 97 浏览 10 评论 0原文

如何将 COleDateTime::GetCurrentTime() 转换为 unix 时间戳,或者更具体地说,我想转换该值,以便可以在 PHP 中通过 date() 函数使用它?

How can I convert a COleDateTime::GetCurrentTime() to a unix time stamp or more specifically I want to convert the value so I can use it in PHP with the date() function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不再让梦枯萎 2024-08-29 05:11:15

下面是一个简单的 Python 函数,用于将 COleDateTime 值转换为 Python 日期时间对象。如果没有别的事,您可以使用它将 COleDateTime 转换为 POSIX 时间格式以供 PHP 摄取。假设您的 COleDateTime 值(原始情况下为 40224.00000000)被传递到浮点值“dateval”。

def oledatetime_to_datetime(dateval):
    from datetime import datetime, timedelta
    import math

    basedate = datetime(year=1899, month=12, day=30, hour=0, minute=0)
    parts = math.modf(dateval)
    days = timedelta(parts[1])
    day_frac = timedelta(abs(parts[0]))
    return basedate + days + day_frac

我只是在 MSDN 文档中指定的开始日期和时间 (1899-12-30 @ 0:00) 创建一个基本日期时间,并添加天数和部分天数的时间增量。它不处理时区,但 COleDateTime 也不处理(据我所知)。如果没有其他的,它对您的测试用例有效,返回日期 2010-02-15 和时间 0:00 的 Python 日期时间对象。

Here's a simple Python function to convert COleDateTime values to Python datetime objects. If nothing else, you can use this to convert the COleDateTime to a POSIX time format for ingestion by PHP. Assume your COleDateTime value (40224.00000000 in the original case) is passed into a floating point value "dateval".

def oledatetime_to_datetime(dateval):
    from datetime import datetime, timedelta
    import math

    basedate = datetime(year=1899, month=12, day=30, hour=0, minute=0)
    parts = math.modf(dateval)
    days = timedelta(parts[1])
    day_frac = timedelta(abs(parts[0]))
    return basedate + days + day_frac

I just create a base datetime at the start date and time specified in the MSDN documentation (1899-12-30 @ 0:00) and add timedeltas for the number of days and partial days. It doesn't deal with timezones, but neither does COleDateTime (so far as I know). If nothing else it works for your test case, returning a Python datetime object for the date 2010-02-15 and time 0:00.

就是爱搞怪 2024-08-29 05:11:15

这听起来像 DATE 类型

DATE 类型是使用8 字节浮点数。天数以整数增量表示,从 1899 年 12 月 30 日开始,午夜为时间零。小时值表示为数字小数部分的绝对值。

测试代码:

COleDateTime cdt(2010, 2, 15, 0, 0, 0); // 2010-02-15 12:00 AM
DATE d = cdt; // using COleDateTime::operator DATE  
printf("%f", d);

打印40224.000000

我不认为有一个内置函数可以将其转换回任何“本机”php 日期/时间值(尽管可能是错误的)。

编辑:转换函数的(可能是幼稚的)起点...
正常,但

LPCTSTR data[] = {
  _T("1976-03-27 05:54:00"),
  _T("1984-04-01 11:55:55"),
  _T("1996-01-25 08:30:00"),
  _T("2000-01-01 08:30:00"),
  _T("2010-02-14 00:00:00"),
  _T("2010-02-14 23:59:59"),
  _T("2010-02-15 00:00:00"),
  _T("2010-02-15 00:00:01"),
  _T("2010-02-23 15:30:00"),
  NULL
};
COleDateTime cdt;
for(int i=0; data[i]; i++) {
  if ( !cdt.ParseDateTime(data[i]) ) {
    _tprintf(_T("%s: error\n"), data[i]);
  }
  else {
    _tprintf(_T("'%s'=>'%f'\n"), data[i], (DATE)cdt);
  }
}

似乎工作

function DATE2unix($d) {
  static $jd2DATE_offset = 2415019;
  $date = intval($d); 
  $time = fmod($d, 1); 
  $ts = jdtounix($date+$jd2DATE_offset) + round($time*86400);
  return $ts;
}

$data = array(
  '1976-03-27 05:54:00'=>'27846.245833',
  '1984-04-01 11:55:55'=>'30773.497164',
  '1996-01-25 08:30:00'=>'35089.354167',
  '2000-01-01 08:30:00'=>'36526.354167',
  '2010-02-14 00:00:00'=>'40223.000000',
  '2010-02-14 23:59:59'=>'40223.999988',
  '2010-02-15 00:00:00'=>'40224.000000',
  '2010-02-15 00:00:01'=>'40224.000012',
  '2010-02-23 15:30:00'=>'40232.645833'
);
foreach($data as $target=>$d ) {
  $ts = DATE2unix($d);
  $date = gmdate('Y-m-d H:i:s', $ts);
  echo 'd=', $d, ' | target=', $target, ' | date=', $date, ' : ', ($target===$date) ? 'ok':'error', "\n";
}

虽然这 9 个示例

d=27846.245833 | target=1976-03-27 05:54:00 | date=1976-03-27 05:54:00 : ok
d=30773.497164 | target=1984-04-01 11:55:55 | date=1984-04-01 11:55:55 : ok
d=35089.354167 | target=1996-01-25 08:30:00 | date=1996-01-25 08:30:00 : ok
d=36526.354167 | target=2000-01-01 08:30:00 | date=2000-01-01 08:30:00 : ok
d=40223.000000 | target=2010-02-14 00:00:00 | date=2010-02-14 00:00:00 : ok
d=40223.999988 | target=2010-02-14 23:59:59 | date=2010-02-14 23:59:59 : ok
d=40224.000000 | target=2010-02-15 00:00:00 | date=2010-02-15 00:00:00 : ok
d=40224.000012 | target=2010-02-15 00:00:01 | date=2010-02-15 00:00:01 : ok
d=40232.645833 | target=2010-02-23 15:30:00 | date=2010-02-23 15:30:00 : ok

我不确定 intval()/round() 以及 php 代码是否必须遵循其他约定。如果您要使用此功能,请对其进行测试,然后使用更多样本再次进行测试。
请记住,这仅限于小于 COleDateTime 范围的 unix 时间戳范围。

That sounds like the DATE type:

The DATE type is implemented using an 8-byte floating-point number. Days are represented by whole number increments starting with 30 December 1899, midnight as time zero. Hour values are expressed as the absolute value of the fractional part of the number.

test code:

COleDateTime cdt(2010, 2, 15, 0, 0, 0); // 2010-02-15 12:00 AM
DATE d = cdt; // using COleDateTime::operator DATE  
printf("%f", d);

prints 40224.000000.

I don't think there is a built-in function to convert this back to any "native" php date/time value (could be wrong though).

edit: A (probably naive) starting point for a conversion function...
some sample data create by

LPCTSTR data[] = {
  _T("1976-03-27 05:54:00"),
  _T("1984-04-01 11:55:55"),
  _T("1996-01-25 08:30:00"),
  _T("2000-01-01 08:30:00"),
  _T("2010-02-14 00:00:00"),
  _T("2010-02-14 23:59:59"),
  _T("2010-02-15 00:00:00"),
  _T("2010-02-15 00:00:01"),
  _T("2010-02-23 15:30:00"),
  NULL
};
COleDateTime cdt;
for(int i=0; data[i]; i++) {
  if ( !cdt.ParseDateTime(data[i]) ) {
    _tprintf(_T("%s: error\n"), data[i]);
  }
  else {
    _tprintf(_T("'%s'=>'%f'\n"), data[i], (DATE)cdt);
  }
}

lead to

function DATE2unix($d) {
  static $jd2DATE_offset = 2415019;
  $date = intval($d); 
  $time = fmod($d, 1); 
  $ts = jdtounix($date+$jd2DATE_offset) + round($time*86400);
  return $ts;
}

$data = array(
  '1976-03-27 05:54:00'=>'27846.245833',
  '1984-04-01 11:55:55'=>'30773.497164',
  '1996-01-25 08:30:00'=>'35089.354167',
  '2000-01-01 08:30:00'=>'36526.354167',
  '2010-02-14 00:00:00'=>'40223.000000',
  '2010-02-14 23:59:59'=>'40223.999988',
  '2010-02-15 00:00:00'=>'40224.000000',
  '2010-02-15 00:00:01'=>'40224.000012',
  '2010-02-23 15:30:00'=>'40232.645833'
);
foreach($data as $target=>$d ) {
  $ts = DATE2unix($d);
  $date = gmdate('Y-m-d H:i:s', $ts);
  echo 'd=', $d, ' | target=', $target, ' | date=', $date, ' : ', ($target===$date) ? 'ok':'error', "\n";
}

which prints

d=27846.245833 | target=1976-03-27 05:54:00 | date=1976-03-27 05:54:00 : ok
d=30773.497164 | target=1984-04-01 11:55:55 | date=1984-04-01 11:55:55 : ok
d=35089.354167 | target=1996-01-25 08:30:00 | date=1996-01-25 08:30:00 : ok
d=36526.354167 | target=2000-01-01 08:30:00 | date=2000-01-01 08:30:00 : ok
d=40223.000000 | target=2010-02-14 00:00:00 | date=2010-02-14 00:00:00 : ok
d=40223.999988 | target=2010-02-14 23:59:59 | date=2010-02-14 23:59:59 : ok
d=40224.000000 | target=2010-02-15 00:00:00 | date=2010-02-15 00:00:00 : ok
d=40224.000012 | target=2010-02-15 00:00:01 | date=2010-02-15 00:00:01 : ok
d=40232.645833 | target=2010-02-23 15:30:00 | date=2010-02-23 15:30:00 : ok

Though these 9 samples seem to work ok I'm not sure about intval()/round() and whether there are other conventions the php code must follow. If you're going to use this function, test it And then test it again with a lot more samples.
And keep in mind that this is limited to the unix timestamp range which is smaller than the range of COleDateTime.

萧瑟寒风 2024-08-29 05:11:15

COleDateTime::GetCurrentTime() 返回一个对象,据我在 MSDN 文档中所知,它不会输出字符串。如果您可以提供在 XML 中获取的字符串的示例,那么为您提供答案会容易得多。

COleDateTime::GetCurrentTime() returns an object, it doesn't output a string as far as I can tell in the MSDN Documentation. If you can provide an example of the string you are getting in the XML, it would be much easier to provide an answer for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文