MonkeyNin

u/MonkeyNin


r/PowerShell•

The other day u/Practical_Air6315 had a couple of threads dealing with issues ex: not knowing if a file is ShiftJIS or UTF8NoBOM encoded

Can you just decode as utf8, checking for errors? If yes, use ShiftJIS otherise utf8? Or can you sometimes have zero decoding errors but it still maps to malformed json? Is there a better method?

I used:

function Test-ShiftJISDecodeError {
    # ...
    
    $Utf8Strict = [System.Text.UTF8Encoding]::new( 
        <# shouldEmitUtf8BOM #> $false, 
        <# should throw on decode error #> $true )
    
    $bytes = [System
24

r/PowerShell•

Have you wondered if it's safe to use the format string yyyy-MM-dd, or can it differ? It's not. Unless you use InvariantCulture ( or null )

Save the expected string for comparison

#requires -PSEdition Core
$fStr     = 'yyyy-MM-dd'
$now      = Get-Date
$expected = $now.tostring( $fStr, ( [CultureInfo]::InvariantCulture) )

Next we can try formatting with every culture on your system. To do that, there is an optional culture argument for ToString like this:

DateTime.ToString( DateFormat, CultureInfo ) 

Next build a summary table with the formatted value and culture:

$cults    = Get-Culture -List
$summary = $cults | Foreach-Object {
    $dateString = 
67


r/PowerBI•

Dax implicitly converts strings to dates, but this is better.

You can confirm this by making a new DAX table.

{ 
    dt"2024-06-06", 
    date(2024, 07, 07) + time(1, 1, 1) 
}

If you use a string "2024-06-06" your column's datatype becomes string

If you use the dt prefix, like dt"2024-06-06" -- the column's datatype is a real datetime

You can use this to simplify your code when it's using static values. This is from: https://learn.microsoft.com/en-us/dax/dax-syntax-reference#data-types

FILTER (
    FactInternetSales,
    [OrderDate] > (DATE(2015,1,9) + TIME(2,30,0)) && [OrderDate] < (DATE(2015,12,31) + TIME(11,59,59))
)

is equivalent to

70