【问题标题】:PowerShell to get count of employees working in every hour of the dayPowerShell 计算一天中每个小时工作的员工数
【发布时间】:2023-01-20 01:33:23
【问题描述】:

我有一个基本上包含进出时间时间表的 CSV 文件。这是一个样本。

"Employee Id","Work Date","In","Out"
"1011","01/16/2023","1/16/23 11:04 PM","1/17/23 6:52 AM"
"1012","01/16/2023","1/16/23 11:18 PM","1/17/23 6:05 AM"
"1012","01/17/2023","1/17/23 10:49 PM","1/18/23 7:26 AM"
"1021","01/16/2023","1/16/23 11:18 PM","1/17/23 6:04 AM"
"1021","01/17/2023","1/17/23 10:46 PM","1/18/23 8:12 AM"
"10261","01/16/2023","1/16/23 6:02 AM","1/16/23 12:01 PM"

我已经创建了预期的输出文件,但我一直在研究如何计算每小时的人数。这是输出创建。

$data = 0..23 | ForEach-Object {
    [PsCustomObject]@{
        WorkDate = "1/17/2023"
        Hour     =  $_
        Count = 0
    }
}
$data

这是空白输出。

"WorkDate","Hour","Count"
"1/17/2023","0","0"
"1/17/2023","1","0"
"1/17/2023","2","0"
"1/17/2023","3","0"
"1/17/2023","4","0"
"1/17/2023","5","0"
"1/17/2023","6","0"
"1/17/2023","7","0"
"1/17/2023","8","0"
"1/17/2023","9","0"
"1/17/2023","10","0"
"1/17/2023","11","0"
"1/17/2023","12","0"
"1/17/2023","13","0"
"1/17/2023","14","0"
"1/17/2023","15","0"
"1/17/2023","16","0"
"1/17/2023","17","0"
"1/17/2023","18","0"
"1/17/2023","19","0"
"1/17/2023","20","0"
"1/17/2023","21","0"
"1/17/2023","22","0"
"1/17/2023","23","0"

我知道我需要做什么的基础知识,但对 Powershell 来说是新手,所以很难将它们放在一起。我需要遍历输出并创建 Workdate+Hour 的日期时间(即 1/17/2023 0:00)并遍历数据以检查是否 >= In 和 <= Out,然后迭代变量并更新伯爵。然后转到下一个小时。

这将是我当前输入示例的输出。例如 1012 在数据集中有 2 个条目。第一个将在 1/17 的 0、1、2、3、4、5、6 小时内计算。 1011 和 1012 也一样,但 10261 根本不起作用 1/17。

WorkDate,Hour,Count
1/17/2023,0,3
1/17/2023,1,3
1/17/2023,2,3
1/17/2023,3,3
1/17/2023,4,3
1/17/2023,5,3
1/17/2023,6,3
1/17/2023,7,0
1/17/2023,8,0
1/17/2023,9,0
1/17/2023,10,0
1/17/2023,11,0
1/17/2023,12,0
1/17/2023,13,0
1/17/2023,14,0
1/17/2023,15,0
1/17/2023,16,0
1/17/2023,17,0
1/17/2023,18,0
1/17/2023,19,0
1/17/2023,20,0
1/17/2023,21,0
1/17/2023,22,0
1/17/2023,23,0

【问题讨论】:

  • 只是改写一下,这样我就可以更好地理解了。那么,您要根据 In 日期计算每小时工作的用户总数?您的 csv 中是否会有重复项,例如用户可能已经回来或类似的情况?您介意更新您的帖子以显示基于您的示例 csv 的预期结果吗?
  • 是的,他们可以是每个员工的多个条目,但没有一个会重叠。它将基于他们的入场时间和出场时间,因为他们可以在 1/17 午夜之前开始,但需要计算到 1/17 之前的早上时间。我已经添加了输出应该是什么,它去掉了“”,但这不是什么大问题。

标签: powershell


【解决方案1】:

这是一个使用内存中 CSV 输入和输出的独立示例(ConvertFrom-CsvConvertTo-Csv);在您的真实代码中,使用基于文件的等效项(Import-CsvExport-Csv):

# Initialize an ordered hashtable whose entries will
# map calendar days to arrays with 24 elements each representing
# an hour of the day, with the element values containing the 
# count of clocked-in employees for the given hour.
$hourMap = [ordered] @{}

@'
"Employee Id","Work Date","In","Out"
"1011","01/16/2023","1/16/23 11:04 PM","1/17/23 6:52 AM"
"1012","01/16/2023","1/16/23 11:18 PM","1/17/23 6:05 AM"
"1012","01/17/2023","1/17/23 10:49 PM","1/18/23 7:26 AM"
"1021","01/16/2023","1/16/23 11:18 PM","1/17/23 6:04 AM"
"1021","01/17/2023","1/17/23 10:46 PM","1/18/23 8:12 AM"
"10261","01/16/2023","1/16/23 6:02 AM","1/16/23 12:01 PM"
'@ | 
ConvertFrom-Csv |
ForEach-Object {
  # Get the in an out timestamps, reset to the start of the hour.
  [datetime] $in, [datetime] $out =
    ($_.In, $_.Out).ForEach({ Get-Date $_ -Minute 0 -Second 0 -Millisecond 0 })
  # Loop over all hours in the time between in and out.
  $timestamp = $in
  while ($timestamp -le $out) {
    # For the timestamp's calendar day, create a 24-element array 
    # representing the hours of the day; a given element's value will
    # receive the count of clocked-in employees for that hour.
    if (-not $hourMap.Contains($timestamp.Date)) {
      $hourMap[$timestamp.Date] = [int[]]::new(24)
    }
    $hourMap[$timestamp.Date][$timestamp.Hour]++
    $timestamp = $timestamp.AddHours(1)
  }
}

# Create the output objects for a given calendar day.
$workDate = '1/17/2023'
$hoursToCounts = $hourMap[[datetime] $workDate]
0..23 | ForEach-Object {
  [pscustomobject] @{
    WorkDate = $workDate
    Hour     = $_
    Count    = $hoursToCounts[$_]
  }
} |
  ConvertTo-Csv

输出:

"WorkDate","Hour","Count"
"1/17/2023","0","3"
"1/17/2023","1","3"
"1/17/2023","2","3"
"1/17/2023","3","3"
"1/17/2023","4","3"
"1/17/2023","5","3"
"1/17/2023","6","3"
"1/17/2023","7","0"
"1/17/2023","8","0"
"1/17/2023","9","0"
"1/17/2023","10","0"
"1/17/2023","11","0"
"1/17/2023","12","0"
"1/17/2023","13","0"
"1/17/2023","14","0"
"1/17/2023","15","0"
"1/17/2023","16","0"
"1/17/2023","17","0"
"1/17/2023","18","0"
"1/17/2023","19","0"
"1/17/2023","20","0"
"1/17/2023","21","0"
"1/17/2023","22","2"
"1/17/2023","23","2"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多