英文:
Powershell Add Member/Column Based on Another Column
问题
以下是您要翻译的内容:
$response = Invoke-RestMethod @Parameters
$readings = $response.readings
$readings2 = $readings | Select-Object -Property * -ExcludeProperty qa
# 获取日期字符串的函数
function ConvertToPST([string]$utcString) {
$format = 'yyyy-MM-ddTHH:mm:ss'
$dt_utc = [datetime]::ParseExact($utcString, $format, [System.Globalization.CultureInfo]::InvariantCulture)
$dt_utc = $dt_utc -as [System.DateTimeOffset]
$pst_str = $dt_utc.ToOffset([TimeSpan]::FromHours(-8)).ToString($format)
return $pst_str
}
# 在 $readings2 中添加一个名为 date_recorded_pst 的新成员
$readings2 | ForEach-Object {
$_ | Add-Member -MemberType NoteProperty -Name "date_recorded_pst" -Value (ConvertToPST($_.date_recorded))
}
这段代码使用PowerShell实现了与Python中的功能类似的功能,包括从API获取数据,进行日期转换,并在数据中添加一个新的日期列(date_recorded_pst)。希望这对您有帮助。
英文:
I have a script that I wrote in Python that I'm trying to replicate with Powershell, which I only started using for the first time yesterday.
It gets data from an API, transforms the data a bit, and then puts it to a SQL server database.
With Python, I get the json that is returned from the POST request and put it into a dataframe. The data includes a reading_id, date_recorded, and the measurement. The date in date_recorded is just a string, but it is in UTC. I created a new column in my dataframe named date_recorded_pst with the date in date_recorded converted to PST.
To do this I created a convertToPst function and then used df.apply like below (this probably isn't the best way to do it but it works)
def convertToPST(utcString):
format = '%Y-%m-%dT%H:%M:%S'
dt_utc = datetime.strptime(utcString, format)
dt_utc = dt_utc.replace(tzinfo=pytz.UTC)
dt_pst = dt_utc.astimezone(tz = pst)
pst_str = dt_pst.strftime(format)
return pst_str
df['date_recorded_pst'] = df['date_recorded'].apply(lambda x : convertToPST(x))
How can I do the same in Powershell? Sorry I'm not caught up to speed on PS lingo. Is adding this new column the same as adding a new "Member"?
This is what I have so far:
$response = Invoke-RestMethod @Parameters
$readings = $response.readings
$readings2 = $readings | Select-Object -Property * -ExcludeProperty qa
The last line was because I wanted to remove a column called qa. I just found it in another thread. Is there a better way to do that?
Here are the last two readings in $readings2
reading_id : 500000023713225
date_recorded : 2023-02-09T02:43:00
systolic_mmhg : 112
diastolic_mmhg : 73
reading_id : 500000023671605
date_recorded : 2023-02-08T15:52:00
systolic_mmhg : 112
diastolic_mmhg : 73
What I want:
reading_id : 500000023713225
date_recorded : 2023-02-09T02:43:00
systolic_mmhg : 112
diastolic_mmhg : 73
date_recorded_pst: 2023-02-08T18:43:00
reading_id : 500000023671605
date_recorded : 2023-02-08T15:52:00
systolic_mmhg : 112
diastolic_mmhg : 73
date_recorded_pst: 2023-02-08T07:52:00
Thanks!
答案1
得分: 1
以下是翻译的内容:
您可以使用ConvertTimeBySystemTimeZoneId
方法将一个datetime
转换为特定的时区。这个方法有两个重载,都可以在这种情况下使用,但其中一个需要您将这些字符串日期时间转换为datetime
实例,然后调用ToLocalTime()
,假设您的时区不是UTC。以下两个示例使用了您问题中的一个日期:
[System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId(
([datetime] '2023-02-09T02:43:00').ToLocalTime(),
'Pacific Standard Time'
).ToString('yyyy-MM-ddTHH:mm:ss') # => 2023-02-08T18:43:00
[System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId(
'2023-02-09T02:43:00', # 该方法隐式将`datetime`转换为此字符串
'UTC', # 来自时区
'Pacific Standard Time' # 到时区
).ToString('yyyy-MM-ddTHH:mm:ss') # => 2023-02-08T18:43:00
知道了这个,既然您已经在使用Select-Object
,我们可以使用计算属性来创建新的对象,添加这个 新的计算属性:
Invoke-RestMethod @Parameters |
ForEach-Object readings |
Select-Object *, @{
N = 'date_recorded_pst'
E = {
[TimeZoneInfo]::ConvertTimeBySystemTimeZoneId(
$_.date_recorded,
'UTC',
'Pacific Standard Time'
).ToString('yyyy-MM-ddTHH:mm:ss')
}
} -ExcludeProperty qa
英文:
<!-- language-all: sh -->
You can use the ConvertTimeBySystemTimeZoneId
method to convert a datetime
to a specific Time Zone. The method has 2 overloads of interest, both would work in this case but also one of them will require you to convert these string date times into a datetime
instance and then call ToLocalTime()
assuming your Time Zone is not UTC. Both examples below using one of the dates in your question:
[System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId(
([datetime] '2023-02-09T02:43:00').ToLocalTime(),
'Pacific Standard Time'
).ToString('yyyy-MM-ddTHH:mm:ss') # => 2023-02-08T18:43:00
[System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId(
'2023-02-09T02:43:00', # the method implicitly casts `datetime` to this string
'UTC', # from TimeZone
'Pacific Standard Time' # to TimeZone
).ToString('yyyy-MM-ddTHH:mm:ss') # => 2023-02-08T18:43:00
Knowing this and since you're already using Select-Object
, we can use a calculated property to create the new objects adding this new calculated property:
Invoke-RestMethod @Parameters |
ForEach-Object readings |
Select-Object *, @{
N = 'date_recorded_pst'
E = {
[TimeZoneInfo]::ConvertTimeBySystemTimeZoneId(
$_.date_recorded,
'UTC',
'Pacific Standard Time'
).ToString('yyyy-MM-ddTHH:mm:ss')
}
} -ExcludeProperty qa
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论