DateTimeOffset adding TimeSpan returns invalid UTC offset for its TimeZoneInfo - offset

I am in the process of building a temporal expression library that must be properly globalized and therefore work in all available Time Zones.
Now I seem to be stuck as I'm not sure how to retrieve adjusted dates from DateTimeOffset objects in the correct Daylight Savings Time (DST) when the transition boundary is crossed using any variety of .Add to move days, hours, etc.
Interestingly, I figured out a work around for the local system's Time Zone but haven't found any way to apply the same strategy to any arbitrary Time Zone.
I was able to find a snippet (didn't keep source, sorry!) which tries to reverse lookup the Time Zone Info by offset but as there are multiple potential results, each of which likely have different DST rules that will not work. (There may be some optimizations available but the base premis is flawed I think)
public TimeZoneInfo GetTimeZoneInfo(DateTimeOffset Value)
{
// Search available sytem time zones for a matching one
foreach (var tzi in TimeZoneInfo.GetSystemTimeZones())
{
// Compare value offset with time zone offset
if (tzi.GetUtcOffset(Value).Equals(Value.Offset))
{
return tzi;
}
}
}
This stuff can be a bit tedious to prove out so I've extracted the core issue into a couple methods and unit tests which will hopefully demonstrate the issue I'm facing.
public DateTimeOffset GetNextDay_Wrong(DateTimeOffset FromDateTimeOffset)
{
// Cannot create a new DateTimeOffset using simply the supplied value's UtcOffset
// because in PST, for example, it could be -7 or -8 depending on DST
return new DateTimeOffset(FromDateTimeOffset.Date.AddDays(1), FromDateTimeOffset.Offset);
}
[TestMethod]
public void GetNextDay_WrongTest()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var workingDate = new DateTime(2009, 11, 2, 0, 0, 0);
var failingDate = new DateTime(2009, 11, 1, 0, 0, 0);
var workingDate_tz = new DateTimeOffset(workingDate, tz.GetUtcOffset(workingDate));
var failingDate_tz = new DateTimeOffset(failingDate, tz.GetUtcOffset(failingDate));
var actual_workingDate_tz = GetNextDay_Wrong(workingDate_tz);
var actual_failingDate_tz = GetNextDay_Wrong(failingDate_tz);
var expected_workingDate = new DateTime(2009, 11, 3, 0, 0, 0);
var expected_failingDate = new DateTime(2009, 11, 2, 0, 0, 0);
var expected_workingDate_tz = new DateTimeOffset(expected_workingDate, tz.GetUtcOffset(expected_workingDate));
var expected_failingDate_tz = new DateTimeOffset(expected_failingDate, tz.GetUtcOffset(expected_failingDate));
Assert.AreEqual(expected_workingDate_tz, actual_workingDate_tz, "Should have found the following day's midnight");
Assert.AreEqual(expected_failingDate_tz, actual_failingDate_tz, "Failing date does not have the correct offset for it's DST");
}
public DateTimeOffset GetNextDay_LooksRight(DateTimeOffset FromDateTimeOffset)
{
// Because we cannot create a new DateTimeOffset we simply adjust the one provided!
var temp = FromDateTimeOffset;
// Move back to midnight of the current day
temp = temp.Subtract(new TimeSpan(temp.Hour, temp.Minute, temp.Second));
// Now move to the next day
temp = temp.AddDays(1);
// Let the DateTimeOffset class do it's magic
temp = temp.ToLocalTime();
// Check if the time zone has changed
if (FromDateTimeOffset.Offset != temp.Offset)
{
// Calculate the change amount (could be 30 mins or even stranger)
var delta = FromDateTimeOffset.Offset - temp.Offset;
// Adjust the temp value by the delta
temp = temp.Add(delta);
}
return temp.ToLocalTime();
}
[TestMethod]
public void GetNextDay_LooksRightTest()
{
// Everything is looking good and the test passes now, so we're home free yeah?
// { To work this needs to match your system's configured Local Time Zone, I'm in PST }
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var workingDate = new DateTime(2009, 11, 2, 0, 0, 0);
var failingDate = new DateTime(2009, 11, 1, 0, 0, 0);
var workingDate_tz = new DateTimeOffset(workingDate, tz.GetUtcOffset(workingDate));
var failingDate_tz = new DateTimeOffset(failingDate, tz.GetUtcOffset(failingDate));
var actual_workingDate_tz = GetNextDay_LooksRight(workingDate_tz);
var actual_failingDate_tz = GetNextDay_LooksRight(failingDate_tz);
var expected_workingDate = new DateTime(2009, 11, 3, 0, 0, 0);
var expected_failingDate = new DateTime(2009, 11, 2, 0, 0, 0);
var expected_workingDate_tz = new DateTimeOffset(expected_workingDate, tz.GetUtcOffset(expected_workingDate));
var expected_failingDate_tz = new DateTimeOffset(expected_failingDate, tz.GetUtcOffset(expected_failingDate));
Assert.AreEqual(expected_workingDate_tz, actual_workingDate_tz, "Should have found the following day's midnight");
Assert.AreEqual(expected_failingDate_tz, actual_failingDate_tz, "Failing date does not have the correct offset for it's DST");
}
[TestMethod]
public void GetNextDay_LooksRight_FAILTest()
{
// Here is where the frustrating part is... aparantly the "magic" that DateTimeOffset provides only works for your systems Local Time Zone...
// { To properly fail this cannot match your system's configured Local Time Zone, I'm in PST so I use EST }
var tz = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
var workingDate = new DateTime(2009, 11, 2, 0, 0, 0);
var failingDate = new DateTime(2009, 11, 1, 0, 0, 0);
var workingDate_tz = new DateTimeOffset(workingDate, tz.GetUtcOffset(workingDate));
var failingDate_tz = new DateTimeOffset(failingDate, tz.GetUtcOffset(failingDate));
var actual_workingDate_tz = GetNextDay_LooksRight(workingDate_tz);
var actual_failingDate_tz = GetNextDay_LooksRight(failingDate_tz);
var expected_workingDate = new DateTime(2009, 11, 3, 0, 0, 0);
var expected_failingDate = new DateTime(2009, 11, 2, 0, 0, 0);
var expected_workingDate_tz = new DateTimeOffset(expected_workingDate, tz.GetUtcOffset(expected_workingDate));
var expected_failingDate_tz = new DateTimeOffset(expected_failingDate, tz.GetUtcOffset(expected_failingDate));
Assert.AreEqual(expected_workingDate_tz, actual_workingDate_tz, "Should have found the following day's midnight");
Assert.AreEqual(expected_failingDate_tz, actual_failingDate_tz, "Failing date does not have the correct offset for it's DST");
}

The underlying issue with this case is that there is not enough information to perform the appropriate Time Zone conversions. Simple offset is not specific enough to infer the Time Zone because for a given offset at a specific moment there can be multiple potential Time Zones. Because the DateTimeOffset object does not capture and maintain the information about the Time Zone it was created with it must be the responsibility of something external to that class to maintain this relationship.
The thing that threw me off the scent was the call ToLocalTime() which I later realized was in fact introducing an implied TimeZoneInfo into the calculation, that of the configured local time for the machine and I believe that internally the DateTimeOffset must be converting to UTC by simply removing the configured offset and then creating a new DateTimeOffset class using the constructor taking DateTime [in UTC] and TimeZoneInfo [from local system] to produce the correct dst aware resulting date.
Given this limitation I no longer see any value in the DateTimeOffset class over the equally accurate and more valuable combination of DateTime and TimeZoneInfo.

Related

App Scripts stepping months and date formatting

I'm new to App Script, please be gentle!
I have a table with a start date and a number of months, I need to list each recurring month from the start date to the number of months.
I'm struggling with 2 issues:
The first date placed in the array seems to be updating as I cycle the dates
The formats are in UNIX and I can't get them to be useful despite an awful lot of reading!
Thank you so much for any help!
Input file (https://i.stack.imgur.com/2EJ65.png)
Output file(https://i.stack.imgur.com/0DH5R.png)
Console log (https://i.stack.imgur.com/NVqck.png)
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Get Data sheet
var rawData = ss.getSheetByName('Sheet3');
// Get a date and set it as a date format
var start = new Date(rawData.getRange("B2").getValues());
console.log(start);
// Get Months
var months = rawData.getRange("C2").getValues();
// Define dates array
var dates = [];
// Add first date
const startDate = start
dates.push([startDate]);
//Add rest of dates dates in a loop
for (var run = 1;run < months;run++) {
//get the last pasted month name
var lastMonth = start.getMonth();
//push next month
dates.push([start.setMonth(lastMonth+1)]);
}
// Get processedData sheet
var processedData = ss.getSheetByName('sheet2');
// Post the outputArray to the sheet in a single call
console.log(start);
console.log(startDate);
console.log(dates);
console.log(dates.length);
console.log(dates[0].length);
processedData.getRange(1,1,6,1).setValues(dates);
}
Incrementing Dates by month and setting format
function lfunko() {
const ss = SpreadsheetApp.getActive();
let dt = new Date();
let start = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate());//need to change back to your value
const months = 10;//need to change back to your value
let dates = [];
dates.push([start]);
for (let run = 1; run < months; run++) {
dates.push([new Date(start.getFullYear(),start.getMonth() + run, start.getDate())])
}
ss.getSheetByName('Sheet0').getRange(1, 1, dates.length).setValues(dates).setNumberFormat("mm/dd/yyyy");//changed sheet name
Logger.log(dates);
}

Get the next immediate Time from a list of date time in dart

If I have a list of DateTime =
[
2021-08-17 11:00:00.000000,
2021-08-17 11:30:00.000000,
2021-08-17 12:00:00.000000,
2021-08-17 12:30:00.000000,
2021-08-17 13:00:00.000000,
]
How can I find the immediate next TIME using DateTime.now()?
ONLY TIME.
If DateTime.now() returns 2021-08-17 11:25:00.000000 I need to return 5 minutes
**CONTEXT: **
My application needs to save the DateTime for another work so I cannot just save the Time. The above mentioned list of DateTimes is saved under the title: Sunday. Checking if today is Sunday or not is quite easy but I need some sort of mechanism for getting the difference with next datetime but I only need the Time difference and not the date
Assuming that your List<DateTime> is sorted in order of earliest to latest, a straightforward, brute-force approach is to just iterate over your List<DateTime> and stop when you find one that is at or after the specified DateTime:
/// Returns the [Duration] to the next [DateTime] from [validTimes] that occurs at
/// or after [now].
///
/// Returns `null` if there is no [DateTime] at or after [now].
///
/// [validTimes] must be already sorted in order of earliest to latest.
Duration? timeToNext(List<DateTime> validTimes, DateTime now) {
for (var time in validTimes) {
if (time.compareTo(now) >= 0) {
return time.difference(now);
}
}
return null;
}
void main() {
var times = [
DateTime(2021, 08, 17, 11, 00),
DateTime(2021, 08, 17, 11, 30),
DateTime(2021, 08, 17, 12, 00),
DateTime(2021, 08, 17, 12, 30),
DateTime(2021, 08, 17, 13, 00),
];
var now = DateTime(2021, 08, 17, 11, 25);
var duration = timeToNext(times, now);
print(duration); // Prints: 0:05:00.000000
}
If your List<DateTime> is very large, then you could use lowerBound from package:collection:
import 'package:collection/collection.dart';
Duration? timeToNext(List<DateTime> validTimes, DateTime now) {
var i = lowerBound(validTimes, now);
if (i == validTimes.length) {
return null;
}
return validTimes[i].difference(now);
}
If i understood correctly,
the difference between DateTimes is pretty easy using difference method for the DateTime class look that in action:
int returnDiference(DateTime dateTime1, DateTime dateTime2) {
final int diference = dateTime1.difference(dateTime2).inSeconds;
return diference;
}
enter image description here
I hope that will be useful for you.

Apps Script Trigger Fails to Get Current Date

I have created an Apps Script to compile data and save the results to a new Google Sheet.
The code gets the current date with new Date() and uses that for the query and to name the new sheet it creates.
Here is the relevant part of the code:
function exportPayroll(setDate){
var date = new Date();
var newWeek = Utilities.formatDate(_getSunday(date),"GMT", "MM/dd/yyyy");
for (var company in companies){
getPayroll(companies[company],newWeek);
}
}
function _getSunday(d) {
d = new Date(d);
var day = d.getDay(),
diff = d.getDate() - day + (day == 0 ? -7:0); // adjust when day is sunday
return new Date(d.setDate(diff));
}
If I run exportPayroll manually, I get the exact results that I expect. So, I setup this trigger to automate the process:
When the trigger runs, the date value is 12/31/1969 instead of today.
Why does it act different with the trigger? Checking the execution transcript, I don't see any error messages.
Is there a better way to get today's date via a trigger?
Since you just wan't to get the Date of the previous sunday. Try running it this way.
function getLastSunday() {
var d = new Date();
var day = d.getDay();
var diff = d.getDate() - day + (day == 0 ? -7:0); // adjust when day is sunday
return new Date(d.setDate(diff));
}
function exportPayroll(){
var newWeek = Utilities.formatDate(getLastSunday(),"GMT", "MM/dd/yyyy");
for (var company in companies){
getPayroll(companies[company],newWeek);
}
}

How to select a calendar week in SAPUI5?

I am searching for a way to select just a calendar week with a DatePicker.
Is there a way to configure that control in a way, that it allows to pick a week and sends a DateTime element out of this week?
I detected the following entry with a list of format options, but week formatting seems not working:
http://scn.sap.com/community/developer-center/front-end/blog/2013/04/28/working-with-odata-dates
This code seems not working:
new sap.m.DatePicker({
value : {
path : "DateTime",
type : new sap.ui.model.type.Date({pattern: "w yy"})
}
}),
The binding is to OData property DateTime of type Edm:DateTime
Few things to be noticed:
Though SAPUI5 says it supports weeks in year, it doesn't currently! //I tested
why? in DateFormat.js file of SAPUI5
case "weekInYear":
sWeek = "";
//TODO getWeek does not exist on Date object
//-> this is a preparation for a future or custom week support
if (oDate.getWeek) {
sWeek += oDate.getWeek();
}
aBuffer.push(jQuery.sap.padLeft(sWeek, "0", oPart.iDigits));
break;
As you can see, its in TODO list!!
Workaround? Yes :
jQuery.sap.require("sap.ui.core.format.DateFormat");
//define getWeek function
Date.prototype.getWeek = function () {
var d = new Date(+this);
d.setHours(0, 0, 0);
d.setDate(d.getDate() + 4 - (d.getDay() || 7));
return Math.ceil((((d - new Date(d.getFullYear(), 0, 1)) / 8.64e7) + 1) / 7);
};
var oDateFormat = sap.ui.core.format.DateFormat.getDateInstance({
pattern: "w y"
});
var oDatePicker = new sap.m.DatePicker({
dateValue: new Date(),
displayFormat: "w y"
})
JSBin code piece is here

flipclock count up from a particular date

For instance, the given start time is 10/28/2014 08:00:00AM (server time). It will count up and show the hours minutes secs lapsed from the given start time? Is this possible with flipclock?
This is what I am using and seems to work fine.
$(document).ready(function(){
var date = new Date(2014, 11, 09);
var now = new Date();
var diff = now.getTime()/1000 - date.getTime()/1000;
clock = $('.clock').FlipClock(diff, {
clockFace: 'DailyCounter',
countdown: false
});
});