Powershell simplest method to get current time expressed as UTC - powershell

I have reviewed the post Creating a DateTime object with a specific UTC DateTime in PowerShell, but it does not seem to directly answer the question I am asking:
What is the most direct method in PowerShell (3.0) to return a sortable string representing "now" as UTC?
I expected the correct answer to be:
Get-Date -Format (Get-Culture).DateTimeFormat.UniversalSortableDateTimePattern
OR
get-date -format u
but this is not the case.
Example: At 1300 hrs (1pm) on September 1st, 2016 in the Pacific Time Zone during DST, I get the response:
2016-09-01 13:00:00Z (the local time with a "Z" appended)
when I was expecting:
2016-09-01 20:00:00Z (correct UTC/GMT time)
So basically, my code is just getting a string representing the "local" time and appending a "Z".
Now, I know I can manipulate to get to that point, but I'm looking for the minimal (simplest, cleanest) way to get here.
Bonus Points (as if they existed): How do I get that same, sortable result, but displaying with "UTC" and/or "GMT" as the suffix. Same minimal requirement.

Probably something like this:
[DateTime]::UtcNow.ToString('u')
Which is equivalent to:
[DateTime]::UtcNow.ToString((Get-Culture).DateTimeFormat.UniversalSortableDateTimePattern)
For the bonus, I think the most straightforward way is just to replace Z with UTC:
[DateTime]::UtcNow.ToString('u').Replace('Z','UTC')
I'm assuming you'll always want UTC since that what it seems like from your question. There doesn't appear to be a format string to get just the 3 letter time zone.

I tried this, and it also gives the result I want:
"[DateTime]::UtcNow.ToString('yyyyMMdd_HHmmss_UTC')"
It is showing time in the format 20180108_152407_UTC
so you can play with the date/time formatting as you wish basically

Related

Rundeck time format

In the "Activity for jobs' page in Rundeck the execution time has a relative time field (example: "Today at 10:15 AM" or "Last Sunday at 4:51 AM") after the timestamp.
It is easy to change the date format of the timestamp by adding jobslist...format[.ko] in the i18n/messages.properties file.
It seems impossible however to change the format of the relative time message. It seems to be hard-coded in en_US with AM/PM which doesn't look too good in in non-English-speaking countries. The format is always the same regardless of the ?lang=xx parameter or the default language in the browser. Interestingly, other objects (like hovering over the field with the mouse and the duration get translated).
Has anyone successfully changed this?
Example. See the duration field
I have been trying this with the docker images (4.8.0, 4.9.0 and SNAPSHOT)
I've looked at the source code and apparently this lies somewhere in the moment.js code.
In some parts, the date formats are hard coded as you say, please add your use case on this thread.

Why is [datetime] converting this UTC time correctly and how can I replicate that with ParseExact

I feel silly that I cannot figure this one out on my own.
I have dates coming from the CarbonBlack API e.g. 2022-02-15-172040 which are in UTC
The [datetime] type cast works fine if I remove the seconds portion of the string
PS M:\Scripts> [datetime]"2022-02-15-1720"
Tuesday, February 15, 2022 12:20:00 PM
I don't understand how it "knows" that is a UTC string. It is correct of course but I expected 5:20pm for the time portion. I wanted the seconds for the date so I went to parse exact as this doesn't match any format strings as far as I know
PS M:\Scripts> [datetime]::ParseExact("2022-02-15-172040", "yyyy-MM-dd-HHmmss" ,[Globalization.CultureInfo]::CurrentUICulture)
Tuesday, February 15, 2022 5:20:40 PM
Which is the time I expected but the incorrect time.
Why is the [datetime] working when I wouldn't expect it to and what do I need to do to the string or static method call for it to treat that as a UTC string with minimal manipulation?
This is because
([datetime]"2022-02-15-1720").Kind
yields 'Local', while
([datetime]::ParseExact("2022-02-15-172040", "yyyy-MM-dd-HHmmss",[CultureInfo]::InvariantCulture)).Kind
returns 'Unspecified'
If you want the result to handle the string as being Local time and then the result should be in UTC, use:
([datetime]::ParseExact("2022-02-15-172040", "yyyy-MM-dd-HHmmss",[CultureInfo]::InvariantCulture, 'AssumeLocal')).ToUniversalTime()
or
$date = [datetime]::ParseExact("2022-02-15-172040", "yyyy-MM-dd-HHmmss",[CultureInfo]::InvariantCulture)
[datetime]::SpecifyKind($date, 'Local').ToUniversalTime()
Going the other way around, so if you regard the date in the string represents UTC (Universal Time), and you want the result to be in
Local time , you need to do this:
[datetime]::ParseExact("2022-04-29-185121", "yyyy-MM-dd-HHmmss",[CultureInfo]::InvariantCulture, 'AssumeUniversal')
Here, ParseExact() treats the string as UTC and outputs a date converted to Local time
(.Kind --> 'Local'), which is the exact same output as
[datetime]"2022-04-29-1851"
would give: also here, .Kind --> 'Local'`
If then you want the resulting date to be in UTC, you need to convert it back again with .ToUniversalTime()
The tricky part is that using ParseExact() without the third parameter ('AssumeLocal' or 'AssumeUniversal') when parsing a string that has no
indication of it being Local or UTC like the example strings here, is always returning a datetime object with its .Kind property set to Unspecified.
Thanks to mklement0's comment,
in PowerShell (Core) 7+, strings like in the example are no longer recognized and should have a TimeZone indication like '2022-02-15 17:20Z'. ('Z' --> Zulu (UTC) Time)
If you do give it the third parameter, telling it what timezone it should use ('AssumeLocal' or 'AssumeUniversal'), the resulting datetime object will
always have its .Kind property set to Local and the result will be converted to local time in case you have given AssumeUniversal as parameter.

Google Calendar api ignores timezone

Im using the CalendarApi in flutter and in order to get events I use the list method like that:
calendarApi.events.list(
cls.calendarId,
timeZone: <specific timezone>
timeMin: DateTime.now().today().toUtc(),
timeMax: DateTime.now().tomorrow().toUtc(),
),
No matter what timezone I tried it always return the event start and end date in UTC format.
I tried using the timezones in multiple formats:
America/Los_Angeles
UTC - 08:00
GMT format
Pacific Standard Time
None of the following worked.
In addition in the in google calendar's settings the timezone is set correctly.
From the documentation:
Must be an RFC3339 timestamp with mandatory time zone offset, for example, 2011-06-03T10:00:00-07:00, 2011-06-03T10:00:00Z. Milliseconds may be provided but are ignored. If timeMax is set, timeMin must be smaller than timeMax.
Make sure the strings you are sending follow one of these pattens:
YYYY-MM-DDTHH:MM:SS±HH:MM
or
YYYY-MM-DDTHH:MM:SSZ
So for example, the time that this answer was posted would be:
2021-07-27T16:08:02+02:00
or:
2021-07-27T14:08:02Z
Where the Z signifies 'Zulu', or UTC.

Displaying the timezone properly in XSLT

Was using the below tag for displaying the timezone which was working fine until now when the daylight saving has happened and as our server is in UK displaying the time as 01/04/2015 03:43:00 PM + 0100, we would also like to have the timezone displayed, please advice.
Tag Used Previously:
date:format-date(date:date-time(), 'dd/MM/yyyy hh:mm:ss a Z')
Regards
Arvind
If by "properly" you mean you want it displayed as "BST" (for British Summer Time) then there isn't actually enough information in the date/time value to do this - a time-zone offset of +1 occurs in many different timezones near the Greenwich meridian.
You're using the EXSLT library for formatting dates and times. This is based on Java's SimpleDateFormat class, so you could try your luck with the timezone designator z instead of Z.
Alternatively, if you've got access to XSLT 2.0, you can use the format-dateTime() function. This suffers from the same problem (the dateTime value only stores an offset, which doesn't actually tell you the name of the timezone). But you can give the processor a clue by setting the 5th argument of format-dateTime() to "Europe/London", in which case it might be able to work it out.

Converting a string timestamp to Date results in reset to UNIX epoch

I'm having some problems converting a string to a date object in google apps script.
My dates are in the following format, from a 3rd party API:
2013-01-17T17:34:50.507
I am attempting to convert this to a Date object:
return Date(stringDate);
And this is being returned:
Thu Jan 01 01:00:00 GMT+01:00 1970
Can someone tell me what i'm doing wrong, and how to resolve this issue ?
With moment.js, it is as easy as this to parse any of ISO 8601 format.
var date = Moment.moment("2013-01-17T17:34:50.507").toDate();
You can use moment.js to parse your arbitrary date string as well.
To use moment.js in GAS, you just need to add it in the script editor.
Open your script in GAS script editor and go to "Resources" then "Libraries...", then put this project key MHMchiX6c1bwSqGM1PZiW_PxhMjh3Sh48 and click "Add". Choose the version from the dropdown, then click "Save". Now, you are ready to use moment.js in GAS.
moment.js can be used to parse date string, create formatted date string, and many other date manipulation. Thanks to the author!
You can find the moment.js documentation here.
It doesn't appear that the Date object knows how to handle that date. The date is in ISO 8601 format. Javascript can handle Dates if they are given timezone information.
You will have to do some testing, but if those dates given to you are in UTC time, then just add a Z to the end of the date string before you call new Date().
Edit: The Apps Script Date object can't seem to handle a timezone other than UTC when parsing a Date. I opened an issue for it.
It doesn't work in GScript, at least for me at the time I'm writing it.
This post serves a working alternative: How do I format this date string so that google scripts recognizes it?
As of now new Date() seems to work:
var dT = new Date("2013-01-17T17:34:50.507");
console.info("dT: %s or %d", dT, dT.getTime());
returns dT: Thu Jan 17 17:34:50 GMT+01:00 2013 or 1.358440490507E12 in Google Apps Script