Skip to content

Commit

Permalink
formating datetimes with cultures
Browse files Browse the repository at this point in the history
  • Loading branch information
ninmonkey committed Dec 10, 2023
1 parent 2dae13b commit f5efdaf
Showing 1 changed file with 346 additions and 0 deletions.
346 changes: 346 additions & 0 deletions Pwsh/CultureInfo/ConvertingDatetimes-WithDifferentCultures.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,346 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"dotnet_interactive": {
"language": "pwsh"
},
"polyglot_notebook": {
"kernelName": "pwsh"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"AD 2023-12-09\n",
"AP 1402-09-18\n",
"2023-12-09 20:30:10\n",
"2023-12-09 20:30:10\n",
"2023-12-09 20:30:10\n"
]
}
],
"source": [
"$Now = Get-Date\n",
"$MyCult = Get-Culture\n",
"\n",
"# you can use custom arguments when piping, including culture info\n",
"Get-Date | % ToString 'g yyyy-MM-dd'\n",
"Get-Date | % ToString 'g yyyy-MM-dd' (get-culture lrc)\n",
"\n",
"\n",
"# Here's essentially the same thing\n",
"$Now.ToString( \"yyyy-MM-dd HH:mm:ss\", $MyCult )\n",
"(Get-Date).ToString( \"yyyy-MM-dd HH:mm:ss\", $MyCult )\n",
"Get-Date | % ToString \"yyyy-MM-dd HH:mm:ss\""
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"dotnet_interactive": {
"language": "pwsh"
},
"polyglot_notebook": {
"kernelName": "pwsh"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\u001b[32;1mCount\u001b[0m\u001b[32;1m Name \u001b[0m\u001b[32;1m Group\u001b[0m\n",
"\u001b[32;1m-----\u001b[0m \u001b[32;1m---- \u001b[0m \u001b[32;1m-----\u001b[0m\n",
" 7 g yyyy-MM-dd {ckb-IR, lrc, lrc-IR, mzn…}\n",
" 126 yyyy-MM-dd {aa, aa-DJ, aa-ER, aa-ET…}\n",
" 3 yyyy.MM.dd {mn, mn-MN, mn-Mong-MN}\n",
" 5 yyyy/MM/dd {ak, ak-GH, en-ZA, ja…}\n",
"\n",
"g yyyy-MM-dd\n",
"AP 1402-09-18\n",
"AD 2023-12-09\n"
]
}
],
"source": [
"# Find cultures that use a short date like yyyy.MM.dd\n",
"$found = (Get-culture -ListAvailable ) | ?{ $_.DateTimeFormat.ShortDatePattern -match 'yyyy.MM.dd' }\n",
"$group = $found | Group { $_.DateTimeFormat.ShortDatePattern }\n",
"$Group | ft -auto\n",
"\n",
"$Cult2 = Get-Culture 'lrc'\n",
"$cult2.DateTimeFormat.ShortDatePattern # is 'g yyyy-MM-dd'\n",
"$now.ToString( $cult2.DateTimeFormat.ShortDatePattern, $Cult2 ) # is 'AP 1402-09-18'\n",
"$now.ToString( $cult2.DateTimeFormat.ShortDatePattern, $MyCult ) # is 'AD 2023-12-09'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Inspecting your culture info\n",
"\n",
"It's a nested structure, that you can drill down"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"dotnet_interactive": {
"language": "pwsh"
},
"polyglot_notebook": {
"kernelName": "pwsh"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Spock : [Places Hands On Nomad And Conducts Mild Meld] I Am Nomad. I Am Performing My Function. Deep, Emptiness. It Approaches, Collision, Damage, Blackness.\n",
"\n",
"\u001b[32;1mAMDesignator : \u001b[0mAM\n",
"\u001b[32;1mCalendar : \u001b[0mSystem.Globalization.GregorianCalendar\n",
"\u001b[32;1mDateSeparator : \u001b[0m/\n",
"\u001b[32;1mFirstDayOfWeek : \u001b[0mSunday\n",
"\u001b[32;1mCalendarWeekRule : \u001b[0mFirstDay\n",
"\u001b[32;1mFullDateTimePattern : \u001b[0mdddd, MMMM d, yyyy h:mm:ss tt\n",
"\u001b[32;1mLongDatePattern : \u001b[0mdddd, MMMM d, yyyy\n",
"\u001b[32;1mLongTimePattern : \u001b[0mh:mm:ss tt\n",
"\u001b[32;1mMonthDayPattern : \u001b[0mMMMM d\n",
"\u001b[32;1mPMDesignator : \u001b[0mPM\n",
"\u001b[32;1mRFC1123Pattern : \u001b[0mddd, dd MMM yyyy HH':'mm':'ss 'GMT'\n",
"\u001b[32;1mShortDatePattern : \u001b[0mM/d/yyyy\n",
"\u001b[32;1mShortTimePattern : \u001b[0mh:mm tt\n",
"\u001b[32;1mSortableDateTimePattern : \u001b[0myyyy'-'MM'-'dd'T'HH':'mm':'ss\n",
"\u001b[32;1mTimeSeparator : \u001b[0m:\n",
"\u001b[32;1mUniversalSortableDateTimePattern : \u001b[0myyyy'-'MM'-'dd HH':'mm':'ss'Z'\n",
"\u001b[32;1mYearMonthPattern : \u001b[0mMMMM yyyy\n",
"\u001b[32;1mAbbreviatedDayNames : \u001b[0m{Sun, Mon, Tue, Wed…}\n",
"\u001b[32;1mShortestDayNames : \u001b[0m{S, M, T, W…}\n",
"\u001b[32;1mDayNames : \u001b[0m{Sunday, Monday, Tuesday, Wednesday…}\n",
"\u001b[32;1mAbbreviatedMonthNames : \u001b[0m{Jan, Feb, Mar, Apr…}\n",
"\u001b[32;1mMonthNames : \u001b[0m{January, February, March, April…}\n",
"\u001b[32;1mIsReadOnly : \u001b[0mFalse\n",
"\u001b[32;1mNativeCalendarName : \u001b[0mGregorian Calendar\n",
"\u001b[32;1mAbbreviatedMonthGenitiveNames : \u001b[0m{Jan, Feb, Mar, Apr…}\n",
"\u001b[32;1mMonthGenitiveNames : \u001b[0m{January, February, March, April…}\n",
"\n",
"\u001b[32;1mCurrencyDecimalDigits : \u001b[0m2\n",
"\u001b[32;1mCurrencyDecimalSeparator : \u001b[0m.\n",
"\u001b[32;1mIsReadOnly : \u001b[0mFalse\n",
"\u001b[32;1mCurrencyGroupSizes : \u001b[0m{3}\n",
"\u001b[32;1mNumberGroupSizes : \u001b[0m{3}\n",
"\u001b[32;1mPercentGroupSizes : \u001b[0m{3}\n",
"\u001b[32;1mCurrencyGroupSeparator : \u001b[0m,\n",
"\u001b[32;1mCurrencySymbol : \u001b[0m¤\n",
"\u001b[32;1mNaNSymbol : \u001b[0mNaN\n",
"\u001b[32;1mCurrencyNegativePattern : \u001b[0m1\n",
"\u001b[32;1mNumberNegativePattern : \u001b[0m1\n",
"\u001b[32;1mPercentPositivePattern : \u001b[0m1\n",
"\u001b[32;1mPercentNegativePattern : \u001b[0m1\n",
"\u001b[32;1mNegativeInfinitySymbol : \u001b[0m-∞\n",
"\u001b[32;1mNegativeSign : \u001b[0m-\n",
"\u001b[32;1mNumberDecimalDigits : \u001b[0m3\n",
"\u001b[32;1mNumberDecimalSeparator : \u001b[0m.\n",
"\u001b[32;1mNumberGroupSeparator : \u001b[0m,\n",
"\u001b[32;1mCurrencyPositivePattern : \u001b[0m0\n",
"\u001b[32;1mPositiveInfinitySymbol : \u001b[0m∞\n",
"\u001b[32;1mPositiveSign : \u001b[0m+\n",
"\u001b[32;1mPercentDecimalDigits : \u001b[0m3\n",
"\u001b[32;1mPercentDecimalSeparator : \u001b[0m.\n",
"\u001b[32;1mPercentGroupSeparator : \u001b[0m,\n",
"\u001b[32;1mPercentSymbol : \u001b[0m%\n",
"\u001b[32;1mPerMilleSymbol : \u001b[0m‰\n",
"\u001b[32;1mNativeDigits : \u001b[0m{0, 1, 2, 3…}\n",
"\u001b[32;1mDigitSubstitution : \u001b[0mNone\n",
"\n",
"\u001b[32;1mMinSupportedDateTime : \u001b[0m1/1/0001 12:00:00 AM\n",
"\u001b[32;1mMaxSupportedDateTime : \u001b[0m12/31/9999 11:59:59 PM\n",
"\u001b[32;1mAlgorithmType : \u001b[0mSolarCalendar\n",
"\u001b[32;1mCalendarType : \u001b[0mLocalized\n",
"\u001b[32;1mEras : \u001b[0m{1}\n",
"\u001b[32;1mTwoDigitYearMax : \u001b[0m2049\n",
"\u001b[32;1mIsReadOnly : \u001b[0mFalse\n",
"\n",
"\n",
"\u001b[32;1mParent : \u001b[0m\n",
"\u001b[32;1mLCID : \u001b[0m9\n",
"\u001b[32;1mKeyboardLayoutId : \u001b[0m9\n",
"\u001b[32;1mName : \u001b[0men\n",
"\u001b[32;1mIetfLanguageTag : \u001b[0men\n",
"\u001b[32;1mDisplayName : \u001b[0mEnglish\n",
"\u001b[32;1mNativeName : \u001b[0mEnglish\n",
"\u001b[32;1mEnglishName : \u001b[0mEnglish\n",
"\u001b[32;1mTwoLetterISOLanguageName : \u001b[0men\n",
"\u001b[32;1mThreeLetterISOLanguageName : \u001b[0meng\n",
"\u001b[32;1mThreeLetterWindowsLanguageName : \u001b[0mENU\n",
"\u001b[32;1mCompareInfo : \u001b[0mCompareInfo - en\n",
"\u001b[32;1mTextInfo : \u001b[0mTextInfo - en\n",
"\u001b[32;1mIsNeutralCulture : \u001b[0mTrue\n",
"\u001b[32;1mCultureTypes : \u001b[0mNeutralCultures, InstalledWin32Cultures\n",
"\u001b[32;1mNumberFormat : \u001b[0mSystem.Globalization.NumberFormatInfo\n",
"\u001b[32;1mDateTimeFormat : \u001b[0mSystem.Globalization.DateTimeFormatInfo\n",
"\u001b[32;1mCalendar : \u001b[0mSystem.Globalization.GregorianCalendar\n",
"\u001b[32;1mOptionalCalendars : \u001b[0m{System.Globalization.GregorianCalendar}\n",
"\u001b[32;1mUseUserOverride : \u001b[0mTrue\n",
"\u001b[32;1mIsReadOnly : \u001b[0mFalse\n",
"\n"
]
}
],
"source": [
"\n",
"$MyCult.TextInfo.ToTitleCase( 'Spock : [Places hands on Nomad and conducts mild meld] I am Nomad. I am performing my function. Deep, emptiness. It approaches, collision, damage, blackness.' )\n",
"\n",
"# Inspecting my default cult\n",
"$MyCult.DateTimeFormat\n",
"$MyCult.NumberFormat\n",
"$MyCult.Calendar\n",
"$MyCult|fl"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "pwsh"
},
"polyglot_notebook": {
"kernelName": "pwsh"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\u001b[32;1mName \u001b[0m\u001b[32;1m Value\u001b[0m\n",
"\u001b[32;1m---- \u001b[0m \u001b[32;1m-----\u001b[0m\n",
"____gh_state {[RepoDescCache, System.Collections.Hashtable], [QueryCache, System…\n",
"__all_PSModulePaths {, C:\\Program Files (x86)\\Microsoft SQL Server\\150\\Tools\\PowerShell…\n",
"__fzfPreviewCache {}\n",
"__minimalPromptConfig {[Enable, System.Collections.Hashtable], [In, System.Collections.Ha…\n",
"__ninBag {[Profile, System.Collections.Hashtable]}\n",
"__ninNamespaceMapping {[System.Management.Automation.Subsystem.Prediction, sma.Subsystem.…\n",
"__ninPathInvokeTrace {H:\\data\\2023\\dotfiles.2023\\pwsh\\profile.ps1, H:\\data\\2023\\dotfiles…\n",
"? True\n",
"^ Get-Variable\n",
"$ local\n",
"args {}\n",
"base H:\\data\\2023\\dotfiles.2023\\pwsh\\src\n",
"Bdg_LastSelect {}\n",
"bpsItems {}\n",
"cBg \u001b[48;2;201;144;103m\u001b[0m\n",
"cFg \u001b[38;2;201;144;103m\u001b[0m\n",
"ConfirmPreference High\n",
"Cult2 lrc\n",
"DebugPreference SilentlyContinue\n",
"DescLoc . Source: \"H:\\data\\2023\\dotfiles.2023\\pwsh\\src\\Build-ProfileCustomM…\n",
"eaNone {[ErrorAction, ignore]}\n",
"eaSilent {[ErrorAction, silentlyContinue]}\n",
"EnabledExperimentalFeatures {}\n",
"Error {The handle is invalid., The handle is invalid., The handle is inva…\n",
"ErrorActionPreference Continue\n",
"ErrorView ConciseView\n",
"ExecutionContext System.Management.Automation.EngineIntrinsics\n",
"false False\n",
"FormatEnumerationLimit 4\n",
"found {aa, aa-DJ, aa-ER, aa-ET…}\n",
"group {Microsoft.PowerShell.Commands.GroupInfo, Microsoft.PowerShell.Comm…\n",
"HOME C:\\Users\\cppmo_000\n",
"Host System.Management.Automation.Internal.Host.InternalHost\n",
"HostPreference Continue\n",
"InformationPreference SilentlyContinue\n",
"input System.Collections.ArrayList+ArrayListEnumeratorSimple\n",
"IsCoreCLR True\n",
"IsLinux False\n",
"IsMacOS False\n",
"IsWindows True\n",
"Last_WhereFilterByChoice {}\n",
"Matches {[0, yyyy-MM-dd]}\n",
"MaximumHistoryCount 4096\n",
"myCult en\n",
"MyInvocation System.Management.Automation.InvocationInfo\n",
"NestedPromptLevel 0\n",
"NotePropertyMembers_hash \n",
"Now 12/9/2023 8:30:10 PM\n",
"null \n",
"OutputEncoding System.Text.UTF8Encoding\n",
"PID 41464\n",
"PROFILE C:\\Users\\cppmo_000\\SkyDrive\\Documents\\PowerShell\\Microsoft.dotnet-i…\n",
"ProgressPreference Continue\n",
"PSBoundParameters {}\n",
"PSCommandPath H:\\data\\2023\\dotfiles.2023\\pwsh\\src\\autoload_forDeveloperTools.ps1\n",
"PSCulture en\n",
"PSDefaultParameterValues {[Set-ClipBoard:PassThru, True], [Update-Module:Verbose, True], [Bu…\n",
"PSEdition Core\n",
"PSEmailServer \n",
"PSHOME C:\\Users\\cppmo_000\\.nuget\\packages\\microsoft.dotnet-interactive\\1.0…\n",
"PSNativeCommandArgumentPassing Legacy\n",
"PSNativeCommandUseErrorAction… False\n",
"PSScriptRoot H:\\data\\2023\\dotfiles.2023\\pwsh\\src\n",
"PSSessionApplicationName wsman\n",
"PSSessionConfigurationName http://schemas.microsoft.com/powershell/Microsoft.PowerShell\n",
"PSSessionOption System.Management.Automation.Remoting.PSSessionOption\n",
"PSStyle System.Management.Automation.PSStyle\n",
"PSUICulture en\n",
"PSVersionTable {[PSVersion, 7.4.0], [PSEdition, Core], [GitCommitId, 7.4.0], [OS, …\n",
"PWD H:\\data\\2023\\pwsh\\notebooks\\Pwsh\\CultureInfo\n",
"registerArgumentCompleterSplat {[CommandName, Get-SecureSetting], [ParameterName, Name], [ScriptBl…\n",
"SB_completer_PipeWorks_Secure… …\n",
"SB_completer_PipeWorks_Select… …\n",
"setAliasSplat {[ErrorAction, ignore], [Value, _fmt_mdTableRow], [Description, exp…\n",
"ShellId Microsoft.PowerShell\n",
"splat_Show {[PassThru, True]}\n",
"StackTrace at System.ConsolePal.GetBufferInfo(Boolean throwOnNoConsole, Boo…\n",
"StringModule_DontInjectJoinSt… True\n",
"true True\n",
"VerbosePreference SilentlyContinue\n",
"WarningPreference Continue\n",
"WhatIfPreference False\n",
"\n"
]
}
],
"source": [
"Get-Variable -scope Local"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (PowerShell)",
"language": "PowerShell",
"name": ".net-pwsh"
},
"language_info": {
"name": "polyglot-notebook"
},
"polyglot_notebook": {
"kernelInfo": {
"defaultKernelName": "pwsh",
"items": [
{
"aliases": [],
"languageName": "pwsh",
"name": "pwsh"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit f5efdaf

Please sign in to comment.