Google Apps脚本和RFC 3339问题 [英] Google Apps Script and RFC 3339 issue

查看:103
本文介绍了Google Apps脚本和RFC 3339问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述






编辑下面的第一个答案



在代码中稍作修改后,我设法让Google的代码片段工作,但由于JSON转换JS日期对象的方式,时区丢失问题仍然存在。



新的代码和记录器结果如下:

  function testJSONDate(){
Logger.log('起始值:2016/3/31 12:00:00');
var jsDate = JSON.stringify(new Date(2016/3/31 12:00:00)); //时间是12:00我在GMT + 2
Logger.log ('JSON.stringify value:'+ jsDate);
Logger.log('JSON parse jsDate:'+ JSON.parse(jsDate)); //时间是10:00,UTC
var jsDateWithoutQuotesAndMillisecAndZ = jsDate.replace(//','').spanger.log('jsDateWithoutQuotesAndMillisecAndZ = '+ jsDateWithoutQuotesAndMillisecAndZ);
var date = parseDate(jsDateWithoutQuotesAndMillisecAndZ);
Logger.log('使用Google\代码解析RFC3339日期:'+ date); //不返回有效日期
var otherFunction = parseDate2(jsDateWithoutQuotesAndMillisecAndZ);
Logger.log('使用其他代码解析RFC3339日期:'+ otherFunction); //在右边tz
}返回有效日期

你在上下文中使用了一个小帮助函数,它只是作为一个临时设备来获取特定API返回的字符串( Google Calendar API )在Apps Script中正确解析。它不是任何一种通用的日期转换器。当提交问题时,项目成员将其扔在一起,并且该线程中的后续消息指出该函数不处理的另一个细节。


$ b

截至目前,Apps Script中的日期解析器正确地解析了以下格式:

  function testdate(){
Logger.log(new Date(2016/03/31 10:00:00)); //本地时间
Logger.log(新日期(2016/03/31 10:00:00 +2:00)); //具有给定的偏移量
Logger.log(new Date(2016-03-31T08:00:00.000Z));请注意,UTC时间戳需要几毫秒,但不包含UTC时间戳。b





允许其他人使用。

你如何处理需要解析但不属于上述内容的日期时间字符串取决于其格式。如果您有 2016-03-31T10:00:00 (显然,这是Google Calendar API返回的内容),并且这意味着在当地时间,那么您需要准确无误引用的解析函数做了什么:用空格替换T,用 - 替换。如果相同的字符串表示UTC时间,则需要在最后添加 .000Z 。等等。

In the Google reference documentation I found a short function to convert RFC3339 date string to a valid Date object. The code is very simple and goes like this :

function parseDate(string) {
  var parts = string.split('T');
  parts[0] = parts[0].replace(/-/g, '/');
  return new Date(parts.join(' '));
}

The problem is that it does not work.(I'm surprised they publish a code that doesn't work... am I missing something ?)

I also had an issue while using JSON to stringify and parse dates because the JSON method returns a UTC value (a Z at the end) and because of that I lose the Time zone information. Google's code does not handle that issue either (even if it worked).

Below is a demo code I used to test it and a solution I wrote to get what I want. Not sure it's very efficient nor well written but at least I get the result I want (I'm executing this code in a script set to GMT+2, Belgium summer time). I'm open to any suggestion to improve this code.(and that would be the subject of this question)

I added a lot of logs and comments in the code to make it as clear as possible :

function testJSONDate() {
  Logger.log('starting value : "2016/3/31 12:00:00"');
  var jsDate = JSON.stringify(new Date("2016/3/31 12:00:00"));// time is 12:00 I'm in GMT+2
  Logger.log('JSON.stringify value : '+jsDate);
  Logger.log('JSON parse jsDate : '+JSON.parse(jsDate)); // time is 10:00, UTC
  var jsDateWithoutQuotes = jsDate.replace(/"/,'');
  var date = parseDate(jsDateWithoutQuotes); 
  Logger.log('parsed RFC3339 date using Google\'s code : '+date);  // does not return a valid date
  var otherFunction = parseDate2(jsDateWithoutQuotes);
  Logger.log('parsed RFC3339 date using other code : '+otherFunction); // does return a valid date in my TZ
}

function parseDate(string) {
  var parts = string.split('T');
  parts[0] = parts[0].replace(/-/g, '/');
  return new Date(parts.join(' '));
}
function parseDate2(string) {
  var refStr = new Date().toString();
  var fus = Number(refStr.substr(refStr.indexOf('GMT')+4,2));
  Logger.log('TZ offset = '+fus);
  var parts = string.split('T');
  parts[0] = parts[0].replace(/-/g, '/');
  var t = parts[1].split(':');
  return new Date(new Date(parts[0]).setHours(+t[0]+fus,+t[1],0));
}

Logger results :


EDIT following first answer

After a small change in the code I managed to get Google's snippet to work but the problem of time zone being lost still remains because of the way JSON converts JS date objects.

new code and logger result below:

function testJSONDate() {
  Logger.log('starting value : 2016/3/31 12:00:00');
  var jsDate = JSON.stringify(new Date("2016/3/31 12:00:00"));// time is 12:00 I'm in GMT+2
  Logger.log('JSON.stringify value : '+jsDate);
  Logger.log('JSON parse jsDate : '+JSON.parse(jsDate)); // time is 10:00, UTC
  var jsDateWithoutQuotesAndMillisecAndZ = jsDate.replace(/"/g,'').split('.')[0];
  Logger.log('jsDateWithoutQuotesAndMillisecAndZ = '+jsDateWithoutQuotesAndMillisecAndZ);
  var date = parseDate(jsDateWithoutQuotesAndMillisecAndZ); 
  Logger.log('parsed RFC3339 date using Google\'s code : '+date);  // does not return a valid date
  var otherFunction = parseDate2(jsDateWithoutQuotesAndMillisecAndZ);
  Logger.log('parsed RFC3339 date using other code : '+otherFunction); // does return a valid date in the right tz
}

解决方案

You have taken a little helper function out of context. It was only meant as a stopgap device to get the strings returned by a particular API (Google Calendar API) to parse correctly in Apps Script. It is not any kind of universal date converter. A project member threw it together when filing an issue, and a follow-up message in that thread points out another detail that the function doesn't handle.

As of now, the date parser in Apps Script correctly parses the following formats:

function testdate() {
  Logger.log(new Date("2016/03/31 10:00:00"));       // local time
  Logger.log(new Date("2016/03/31 10:00:00 +2:00")); // with given offset
  Logger.log(new Date("2016-03-31T08:00:00.000Z"));  // in UTC 
}

Note that milliseconds are required for UTC timestamp, but are not allowed for the others.

What you do with a datetime string that needs to be parsed but is not one of the above, depends on its format. If you have 2016-03-31T10:00:00 (apparently, this is what Google Calendar API returns) and this is meant to be in local time, then you need exactly what the quoted parse function does: replace T by space and - by /. If the same string represents UTC time, one needs to add .000Z at the end. And so on.

这篇关于Google Apps脚本和RFC 3339问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆