Closed
Labels
Issue-Enhancementthe issue is more of a feature request than a bugResolution-No ActivityIssue has had no activity for 6 months or moreWG-Cmdletsgeneral cmdlet issues
Description
Similar to the recent PR that added the -AsUTC switch to Get-Date, we should also have the ability to set Local DateTimeKind as well.
Examples of how this would work:
Get-Date "1/12/2020 4:00pm" -Format "MM/dd/yyyy HH:mm K"
# The DateTimeKind here is Unspecified, so result is 01/12/2020 16:00
Get-Date ((Get-Date "1/12/2020 4:00pm").ToUniversalTime()) -Format "MM/dd/yyyy HH:mm K"
Get-Date "1/12/2020 4:00pm" -AsUTC -Format "MM/dd/yyyy HH:mm K"
# The DateTimeKind here is Utc, so result is 01/12/2020 22:00 Z
Get-Date ((Get-Date "1/12/2020 4:00pm").ToLocalTime()) -Format "MM/dd/yyyy HH:mm K"
Get-Date "1/12/2020 4:00pm" -AsLocal -Format "MM/dd/yyyy HH:mm K"
# The DateTimeKind here is Local, so result is 01/12/2020 10:00 -06:00
Reacted by Michael Klement
Metadata
Metadata
Assignees
Labels
Issue-Enhancementthe issue is more of a feature request than a bugResolution-No ActivityIssue has had no activity for 6 months or moreWG-Cmdletsgeneral cmdlet issues
Activity
mklement0 commentedon Jan 30, 2020
Great idea.
For the sake of completeness,
-AsUnspecifiedwould be helpful too, if you're expressly looking for an abstract point in time.I assume the intent of the
-As*switches is to either directly output a[datetime]instance with the indicated.Kindvalue (UTC,Local,Unspecified), or to make such an instance the basis for applying the-Format/-UFormatformat.We have to be careful about the logic of interpreting the input, however:
From what I can tell, only
Get-Date -Datewith either a string argument (except strings explicitly indicating UTC or containing a time-zone offset) or a[datetime]instance whose.KindisUnspecifiedcurrently results in the output[datetime]instance having.KindUnspecified; otherwise, it isLocal(or will beUtcwith-AsUtc).The tricky part is that
Unspecifiedinstances are treated situationally either as representing UTC or as representing LOCAL time:.ToUniversalTime()interprets anUnspecifiedinstance asLocal.ToLocalTime()interprets anUnspecifiedinstance asUtc(!)Therefore, as also implied by your last example command:
This is .NET's behavior, over which we have no control, but I wonder if surfacing this behavior via
-AsLocalwould cause confusion:My expectation would be that the nominal date given (in text form) would be interpreted as-is as a local date; which is the equivalent of:
Note how the unspecified input date string was now interpreted as a local time, as reflected in the output
[datetime]instance, which has.KindLocal, as requested.SeeminglyScience commentedon Jan 31, 2020
+1 Michael Klement (@mklement0). I would expect it to use:
mklement0 commentedon Jan 31, 2020
Thanks, Patrick Meinecke (@SeeminglyScience), but note that since the
-Dateparameter is[datetime]-typed, I think it is the parameter binder that performs the parsing up front (culture-sensitively, due to being a cmdlet argument, as opposed to the culture-invariant parsing that happens with casts or advanced functions, which is a won't-fix inconsistency; see #6989)Therefore, I think
Get-Datealready sees the parsed result, which is a[datetime]instance with a.Kindvalue ofUnspecified- hence my suggestion to use[datetime]::new(<ticks>, <kind>).scotthardwick commentedon Jan 31, 2020
Michael Klement (@mklement0) and Patrick Meinecke (@SeeminglyScience) Thanks for joining in!
If we are including all three DateTimeKind options, do you think we need a parameter more like
-AsDateTimeKind Utc
-AsDateTimeKind Local
-AsDateTimeKind Unspecified
?
-AsUTC hasn't been part of a formal release yet, so now would be the time to change it if the PowerShell team was going to?
mklement0 commentedon Jan 31, 2020
I'd say, given that there's only 3 enumeration values whose number is unlikely to grow, it's easier on users to use distinct switches,
-AsUtc,-AsLocal,-AsUnspecified.scotthardwick commentedon Jan 31, 2020
Michael Klement (@mklement0) Fair point. I can accept that ;-)
mklement0 commentedon Jan 31, 2020
Then again, for invocation with programmatically constructed parameters something like
-AsKind Utc|Local|Unspecifiedis preferable...Having our cake and eating it too (providing
-AsUtcas an effective alias of-AsKind Utc, ...) is also an option, but might get confusing / be a pain to implement (many parameter sets).scotthardwick commentedon Jan 31, 2020
;-) I have only started posting suggestions for PowerShell in the last few days and I hate to ask for the moon, so I am trying to be semi-conservative in my asks. I will take it either way ;-)
ThomasNieto commentedon Feb 4, 2020
Since we're adding multiple options here for the kind it should be an enum not switch parameter. Right now if we add
-AsUtcand-AsLocaland-AsUnspecifiedthose three switch parameters will need to be in separate parameter sets since you can't have multiple passed at the same time.mklement0 commentedon Feb 4, 2020
As stated before, for programmatic construction of arguments
-AsKind <kind>is preferable; that doesn't necessary preclude also providing convenience aliases (-AsUtcfor-AsKind Utc, ...; they wouldn't be the first of their kind), but I get that this (a) could be a paint to implement (proliferation of parameter sets) and (b) the duplication could cause conceptual confusion.In short: I'm personally fine with only implementing
-AsKind <kind>, if that's the consensus.However, we should then act quickly, before 7.0 GA ships with the recently implemented
-AsUtcswitch.ThomasNieto commentedon Feb 5, 2020
Do you have any examples of a alias switch parameter mapping to another parameter?
mklement0 commentedon Feb 5, 2020
The one that readily comes to mind is
Get-Location -StackforGet-Location -StackName '', but, if memory serves, there are more; they're not easy to search for.mklement0 commentedon Feb 6, 2020
More relatable examples:
In
Get-ChildItem:-Directory/ forAttributes -Directory,-Filefor-Attributes !Directory,-Hiddenfor-Attributes Hidden,-ReadOnlyfor-Attributes ReadOnly,-SystemforAttributes System.In
Import-Module:-Globalfor-Scope Global("TheGlobalparameter is equivalent to theScopeparameter with a value ofGlobal." - help).I'm curious, though, Thomas Nieto (@ThomasNieto): Are you asking not just because you were curious / weren't convinced that there is such a thing, or are you philosophically opposed to the concept?
2 remaining items
mklement0 commentedon Jul 16, 2020
To continue the discussion from #13084 (comment), where Ilya (@iSazonov) wrote:
Agreed that
[datetimeoffset]is always the better choice, butGet-Dateis currently a wrapper for[datetime]only and for reasons of backward compatibility it will have to remain that way, at least by default.Therefore, it makes sense to support it more fully, and to also offer
-AsLocaland-AsUnspecified/-AsKind <kind>.A possible alternative is to eliminate the
Unspecifiedkind fromGet-Dateas follows, in which case only implementing-AsUtcwould be sufficient:-AsUtcis passed, always return aLocaldate.This would mean that commands such as
Get-Date 2020-1-1, which currently return anUnspecifieddate, would then return aLocaldate, which may be more in line with what users expect.Technically, though, it is a breaking change.
iSazonov commentedon Jul 16, 2020
AsUTC was added in Preview only (#11611) and we can replace it with general parameter like
-AsKind <kind>or-DateTimeKind <kind>. No needs exist for other parameters./cc Brendan Burns (@brendandburns)
aetos382 commentedon Jul 24, 2020
I think it would be complete to take TimeZoneInfo (or its
Id) instead of DateTimeKind (in which case the output would be DateTimeOffset).As a special case of
TimeZoneInfo, it supportsUTCandLocal.For example, I may want to know what time '10:00 AM PDT' is in Japanese time (this format is often used as the start time for some event).
So, I think it would be useful if I could convert the time zone as follows.
Get-Date -Date '2020-07-25 10:00' -TimeZone PDT -AsTimeZone JSTBut, time zone abbreviations such as 'PDT' and 'JST' are not specified in ISO 8601 and are not official.
Also, it is unclear whether 'JST' refers to 'Japan Standard Time' or 'Jerusalem Standard Time'.
Thus, I am not sure if we should accept these notations as arguments.
This command will accept the following formats.
But, if the command only accepts values in the following form, I don't think this specification is so useful.
Because before I can use the command, I have to find out the official name of the time zone or its offset.
Get-Date -Date '2020-07-25 10:00' -TimeZone 'America/Los_Angeles' -AsTimeZone 'Asia/Tokyo'Get-Date -Date '2020-07-25 10:00' -Offset -7 -AsOffset +9aetos382 commentedon Jul 24, 2020
I think we should be cautious about using
SwitchParameterandParameterSetNametogether, as they don't work well together.For example, not setting
-AsUTCis equivalent to specifying-AsUTC:$false.However, if
-AsUTCand-AsLocalare mutually exclusive by parameter set, then if-AsUTCis not set,-AsLocalcan be set, but if-AsUTC:$falseis specified,-AsLocalcannot be set.vexx32 commentedon Jul 24, 2020
aetos (@aetos382) RE: parameter sets - yes, that's how it's designed, you're not supposed to be able to use them together. Switches supporting a
-Switch:$falseoption doesn't change that, really, I don't think that allowing both for that reason is very clear or sensible. It's much clearer to show users via parameter sets (which are directly reflected in the syntax diagrams in theGet-Helpoutput) which switches can and can't be used in tandem. It would be pointless and confusing to display a syntax diagram that seems to allow both, when in practice only one can be used.As for the time zone suggestion, it would be best if you requested that in a new issue as it is an entirely separate request IMO.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.