summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorJuan Carlos <juancarlospaco@gmail.com>2023-05-24 12:55:48 -0300
committerGitHub <noreply@github.com>2023-05-24 17:55:48 +0200
commitcb3f6fdc6665298ec2b75ade95ac7bc9af5a5f66 (patch)
tree5005bac4981f1f50118f492725d6cfdaa58ee553 /lib
parent4d6be458a00a642555e95055ff640daba59513f7 (diff)
downloadNim-cb3f6fdc6665298ec2b75ade95ac7bc9af5a5f66.tar.gz
Improve times (#21901)
* .

* Improve times
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/times.nim33
1 files changed, 21 insertions, 12 deletions
diff --git a/lib/pure/times.nim b/lib/pure/times.nim
index ae101bc34..61971ba3a 100644
--- a/lib/pure/times.nim
+++ b/lib/pure/times.nim
@@ -422,7 +422,7 @@ else:
   # Still track when using older versions
   {.pragma: parseFormatRaises, raises: [TimeParseError, TimeFormatParseError, Defect].}
   {.pragma: parseRaises, raises: [TimeParseError, Defect].}
-  
+
 
 #
 # Helper procs
@@ -608,8 +608,8 @@ proc stringifyUnit(value: int | int64, unit: TimeUnit): string =
   ## Stringify time unit with it's name, lowercased
   let strUnit = $unit
   result = ""
-  result.add($value)
-  result.add(" ")
+  result.addInt value
+  result.add ' '
   if abs(value) != 1:
     result.add(strUnit.toLowerAscii())
   else:
@@ -1502,16 +1502,25 @@ proc getDateStr*(dt = now()): string {.rtl, extern: "nt$1", tags: [TimeEffect].}
   runnableExamples:
     echo getDateStr(now() - 1.months)
   assertDateTimeInitialized dt
-  result = $dt.year & '-' & intToStr(dt.monthZero, 2) &
-    '-' & intToStr(dt.monthday, 2)
+  result = newStringOfCap(10)  # len("YYYY-MM-DD") == 10
+  result.addInt dt.year
+  result.add '-'
+  result.add intToStr(dt.monthZero, 2)
+  result.add '-'
+  result.add intToStr(dt.monthday, 2)
 
 proc getClockStr*(dt = now()): string {.rtl, extern: "nt$1", tags: [TimeEffect].} =
   ## Gets the current local clock time as a string of the format `HH:mm:ss`.
   runnableExamples:
     echo getClockStr(now() - 1.hours)
   assertDateTimeInitialized dt
-  result = intToStr(dt.hour, 2) & ':' & intToStr(dt.minute, 2) &
-    ':' & intToStr(dt.second, 2)
+  result = newStringOfCap(8)  # len("HH:mm:ss") == 8
+  result.add intToStr(dt.hour, 2)
+  result.add ':'
+  result.add intToStr(dt.minute, 2)
+  result.add ':'
+  result.add intToStr(dt.second, 2)
+
 
 #
 # Iso week
@@ -2154,19 +2163,19 @@ proc toDateTimeByWeek(p: ParsedTime, zone: Timezone, f: TimeFormat,
   var isoyear = p.isoyear.get(0)
   var yearweek = p.yearweek.get(1)
   var weekday = p.weekday.get(dMon)
-  
+
   if p.amPm != apUnknown:
     raiseParseException(f, input, "Parsing iso weekyear dates does not support am/pm")
-  
+
   if p.year.isSome:
     raiseParseException(f, input, "Use iso-year GG or GGGG as year with iso week number")
-  
+
   if p.month.isSome:
     raiseParseException(f, input, "Use either iso week number V or VV or month")
-  
+
   if p.monthday.isSome:
     raiseParseException(f, input, "Use weekday ddd or dddd as day with with iso week number")
-  
+
   if p.isoyear.isNone:
     raiseParseException(f, input, "Need iso-year with week number")