git-gui / lib / date.tclon commit Merge branch 'sw/safe-create-leading-dir-race' (37ba4c6)
   1# git-gui date processing support
   2# Copyright (C) 2007 Shawn Pearce
   3
   4set git_month(Jan)  1
   5set git_month(Feb)  2
   6set git_month(Mar)  3
   7set git_month(Apr)  4
   8set git_month(May)  5
   9set git_month(Jun)  6
  10set git_month(Jul)  7
  11set git_month(Aug)  8
  12set git_month(Sep)  9
  13set git_month(Oct) 10
  14set git_month(Nov) 11
  15set git_month(Dec) 12
  16
  17proc parse_git_date {s} {
  18        if {$s eq {}} {
  19                return {}
  20        }
  21
  22        if {![regexp \
  23                {^... (...) (\d{1,2}) (\d\d):(\d\d):(\d\d) (\d{4}) ([+-]?)(\d\d)(\d\d)$} $s s \
  24                month day hr mm ss yr ew tz_h tz_m]} {
  25                error [mc "Invalid date from Git: %s" $s]
  26        }
  27
  28        set s [clock scan [format {%4.4i%2.2i%2.2iT%2s%2s%2s} \
  29                        $yr $::git_month($month) $day \
  30                        $hr $mm $ss] \
  31                        -gmt 1]
  32
  33        regsub ^0 $tz_h {} tz_h
  34        regsub ^0 $tz_m {} tz_m
  35        switch -- $ew {
  36        -  {set ew +}
  37        +  {set ew -}
  38        {} {set ew -}
  39        }
  40
  41        return [expr "$s $ew ($tz_h * 3600 + $tz_m * 60)"]
  42}
  43
  44proc format_date {s} {
  45        if {$s eq {}} {
  46                return {}
  47        }
  48        return [clock format $s -format {%a %b %e %H:%M:%S %Y}]
  49}
  50
  51proc reformat_date {s} {
  52        return [format_date [parse_git_date $s]]
  53}