夏令时按日期运行

发布于 2025-01-17 04:12:57 字数 1197 浏览 0 评论 0 原文

我正在尝试进行夏令时转换。 我在“void IsDst(date){”中确定“日期”尺寸时遇到困难 我尝试过“void IsDst(datetime date){” 不确定它是如何工作的?

dstdata.htm

 date = new DateTime(yr, mo-1, dy, hr24, mn, 0);
   Textbox7.value = IsDst(日期);

dstdata.js

 private void IsDst(日期){
     if (日期.月份 < 2 || 日期.月份 > 10){
            返回0; 
     }elseif (日期.月份 >= 2 && 月份 <= 10){ 
          var aaa=0;
          if (日期.月份==2){
              var dstshr=2;
              for (var Dys=1;dys<=25;dys++){
                  var dsts= new DateTime(yr, 2, Dys, dstshr, 0, 0);
                  if ((dsts.DayOfWeek==0) && (aaa==1)){
                      if (date.Date 2 && 月份 < 10){         
      返回1;
  }

I am trying to do Daylight Savings Time conversion.
I am having difficulties dimensioning "date" in the "void IsDst(date){"
I have tried "void IsDst(datetime date){"
Not sure how it works?

dstdata.htm

   date = new DateTime(yr, mo-1, dy, hr24, mn, 0);
   Textbox7.value = IsDst(date);

dstdata.js

   private void IsDst(date){
     if (date.month < 2 || date.month > 10){
            return 0; 
     }elseif (date.month >= 2 && month <= 10){ 
          var aaa=0;
          if (date.month==2){
              var dstshr=2;
              for (var dys=1;dys<=25;dys++){
                  var dsts= new DateTime(yr, 2, dys, dstshr, 0, 0);
                  if ((dsts.DayOfWeek==0) && (aaa==1)){
                      if (date.Date<dsts.Date){
                          return 0;
                      }else{
                          return 1;   
                      }
                  }elseif ((dsts.DayOfWeek==0) && (aaa==0)){              
                      aaa=1;
                      dstshr=3;
                   }
               }
          }
      }elseif(date.month==10){
          var bbb=0;
          var dstehr=2;
          for (var dye=1;dye<=25;dye++){
              dste= new DateTime(yr, 10, dye, dstehr, 0, 0);
              if ((dste.DayOfWeek==0) && (bbb==0)){
                  dste.hour=dste.hour+1;
                  bbb=1;
                  if ((date.Date<dste.Date){
                      return 0;
                  }else{
                      return 1;   
                  }
              }
         }            
      }
  }elseif (date.month > 2 && month < 10){         
      return 1;
  }

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

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

发布评论

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

评论(2

遗忘曾经 2025-01-24 04:12:57

一个混乱(和问题)的案例。

方法 IsDst 似乎有一个 void 返回类型,其名称表明它返回一个布尔值,并且返回整数。

One case of confusion (and problems).

Method IsDst seems to have a void return type, has a name that suggests that it returns a boolean, and returns integer.

葬花如无物 2025-01-24 04:12:57

该代码似乎不是 ECMAScript。逻辑看起来相当复杂,并且 block:

} elseif(date.month==10) {

永远不会被输入,因为如果它为 true,它将已经被前一个 block: 捕获:

} elseif (date.month >= 2 && month <= 10) {

如果您试图根据主机设置确定 DST 在特定日期是否有效,那么您可以测试:

  1. 主机是否遵守夏令时?
  2. DST 在特定日期生效吗?

例如

// Return true if DST is observed in the year of date
function hasDST(date = new Date()) {
  let year = date.getFullYear();
  return !(new Date(year, 0).getTimezoneOffset() == new Date(year, 6).getTimezoneOffset());
}
// Return true if DST is observed for the year of the date and
// the offset on the date is the same as the DST offset for
// that year
function inDST(date = new Date()) {
  let year = date.getFullYear();
  // Offset has opposite sign to convention so use min not max
  let dstOffset = Math.min(
    new Date(year, 0).getTimezoneOffset(),
    new Date(year, 6).getTimezoneOffset(),
  );
  return hasDST(date) && date.getTimezoneOffset() == dstOffset;
}

// Test
let y = new Date().getFullYear();
[new Date(2022,0),  // 1 Jan 2022
 new Date(2022,6),  // 1 Jul 2022
 new Date(2023,0),  // 1 Jan 2023
 new Date(2023,6),  // 1 Jul 2023
 new Date(y   ,0),  // 1 Jan current year
 new Date(y   ,6),  // 1 Jul current year
].forEach(d => console.log(
   `${d.toString()} is in DST? ${inDST(d)}`
));

上面假设如果 1 月 1 日和 7 月 1 日的偏移量不同,则该年观察到夏令时,并且这两个偏移量中的最小值为 DST 偏移量(请注意,ECMAScript 偏移量具有与约定相反的含义,即它们格林威治以西为 +ve,以东为 -ve)。然而,两个偏移量不同的事实并不一定意味着遵守夏令时(见下文)。

一旦针对 2023 年 11 月 5 日美国可能对永久夏令时的更改更新了实现:

  1. 对于 2023 年的所有日期,hasDST 将返回 true,而 false > 对于此后的所有日期,
  2. 对于 2023 年 11 月 5 日至 2023 年 12 月 31 日期间的所有日期,inDST 将返回 true
  3. 对于 2023 年之后的日期(即 2024 年 1 月 1 日起),hasDSTinDST 将返回 false

因此对于 2023 年 11 月 5 日至 12 月 31 日, hasDSTinDST 将在本应为 false 时返回 true。对于年内改变偏移量的其他地方也会出现类似的错误。

鉴于特定位置的日期是否处于夏令时实际上无关紧要,重要的是偏移量,这两个函数的全部意义都是值得怀疑的。有很多地方根本没有夏令时。

爱尔兰不仅没有夏令时,而且还有相反的情况:夏季的标准时间 (IST) 是 UTC+1,然后在冬季时钟会调回 UTC+0 (GMT)。上面认为IST是夏令时,GMT是标准时间。

因此,最好将日期保存为不带时区的日期,将日期时间保存为 UTC,然后根据转换时已知的偏移规则计算出特定地点所需的偏移量。

The code doesn't appear to be ECMAScript. The logic appears quite convoluted and the block:

} elseif(date.month==10) {

will never be entered because if it's true, it will already be caught by the previous block:

} elseif (date.month >= 2 && month <= 10) {

If you are trying to work out if DST is in force for a particular date based on the host settings, then you can test:

  1. Does the host observe DST?
  2. Is DST in force on the particular date?

E.g.

// Return true if DST is observed in the year of date
function hasDST(date = new Date()) {
  let year = date.getFullYear();
  return !(new Date(year, 0).getTimezoneOffset() == new Date(year, 6).getTimezoneOffset());
}
// Return true if DST is observed for the year of the date and
// the offset on the date is the same as the DST offset for
// that year
function inDST(date = new Date()) {
  let year = date.getFullYear();
  // Offset has opposite sign to convention so use min not max
  let dstOffset = Math.min(
    new Date(year, 0).getTimezoneOffset(),
    new Date(year, 6).getTimezoneOffset(),
  );
  return hasDST(date) && date.getTimezoneOffset() == dstOffset;
}

// Test
let y = new Date().getFullYear();
[new Date(2022,0),  // 1 Jan 2022
 new Date(2022,6),  // 1 Jul 2022
 new Date(2023,0),  // 1 Jan 2023
 new Date(2023,6),  // 1 Jul 2023
 new Date(y   ,0),  // 1 Jan current year
 new Date(y   ,6),  // 1 Jul current year
].forEach(d => console.log(
   `${d.toString()} is in DST? ${inDST(d)}`
));

The above assumes that if the offsets for 1 Jan and 1 Jul are different, then daylight saving is observed in that year and that the minimum of those two offsets is the DST offset (noting that ECMAScript offsets have the opposite sense to convention, i.e. they are +ve to the west and -ve to the east of Greenwich). However, the fact that the two offsets are different doesn't necessarily mean DST is observed (see below).

Once implementations are updated for the probable change to permanent DST in the US on 5 Nov 2023:

  1. hasDST will return true for all dates in 2023 and false for all dates thereafter
  2. inDST will return true for all dates from 5 Nov 2023 to 31 Dec 2023
  3. hasDST and inDST will return false for dates after 2023 (i.e. 1 Jan 2024 onward)

So for 5 Nov to 31 Dec 2023 both hasDST and inDST will return true when they should be false. Similar errors will occur for other places that change offsets during the year.

The whole point of both these functions is questionable given whether a date is in DST or not for a particular location really is irrelevant, what matters is the offset. There are many places that don't have DST at all.

Ireland not only doesn't have DST, it has the opposite: its standard time over summer (IST) is UTC+1, then clocks are set back to UTC+0 (GMT) over winter. The above thinks that IST is daylight saving and GMT is standard time.

Hence the reason why it's best to just save dates as dates without a timezone and datetimes as UTC, then work out the offset only as required for a specific place based on offset rules known at the time of conversion.

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