Skip to content

Commit

Permalink
Add date format specifiers "j" for day-of-year and "w" for week-of-year
Browse files Browse the repository at this point in the history
closes #49
closes #50
  • Loading branch information
eltos committed Sep 7, 2024
1 parent eb3f822 commit a3c751c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
9 changes: 9 additions & 0 deletions PasteIntoFile/Main.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
Expand Down Expand Up @@ -540,6 +541,14 @@ public string Format(string format, object arg, IFormatProvider formatProvider)
}
throw new FormatException("Invalid format string '" + format + "'. Expected a positive integer specifying maximum length.");
}
// DateTime
if (arg is DateTime dt && !string.IsNullOrEmpty(format)) {
// `j` for day of year
format = Regex.Replace(format, @"j+", m => dt.DayOfYear.ToString(new string('0', m.Value.Length)));
// `w` for week of year
var week = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(dt, CultureInfo.CurrentCulture.DateTimeFormat.CalendarWeekRule, CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek);
format = Regex.Replace(format, @"w+", m => week.ToString(new string('0', m.Value.Length)));
}
// default formatting
return string.Format("{0:" + format + "}", arg);
}
Expand Down
3 changes: 2 additions & 1 deletion PasteIntoFile/TemplateEdit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ public TemplateEdit() {
Settings.Default.filenameTemplate,
"{0:yyyy-MM-dd HH-mm-ss}",
"{0:yyyyMMdd_HHmmss}",
"{0:yyyy-MM-dd}_{1:000}",
"{0:yyyy-jjj}_{1:000}",
"PasteIntoFile_{1:000}_{0:fffffff}",
"{0:yyyy-ww}/{0:ddd HHᵸmm´ss´´}",
"{2:10}",
});
textTemplate.Text = Settings.Default.filenameTemplate;
Expand Down
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,11 @@ For non-text files, the file path is copied as text in addition to the image con
<a id="template-format"></a>
### Filename template format
The filename and/or subfolder template string may contain the following placeholders:
- `{0}`: The current date and time.
The format can be specified, e.g. `{0:yyyy-MM-dd HH-mm-ss}`, see [format specifiers](https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings) for details.
- `{1}`: A counter for batch mode.
The format can be specified, e.g. `{1:000}` for zero padding, see [format specifiers](https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings) for details.
- `{2}`: The clipboard text.
The format can be specified, e.g. `{2:15}` to limit it to 15 characters.
- `{0}`: The current date and time, e.g. `{0:yyyy-MM-dd HH-mm-ss}`.
In addition to the [standard format specifiers](https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings),
`j` for day-of-year and `w` for week-of-year are supported.
- `{1}`: The save counter for batch mode. E.g. `{1:000}` for 3-digit zero padding. See [format specifiers](https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings) for details.
- `{2}`: The clipboard text. E.g. `{2:15}` to limit it to 15 characters.

### Command Line

Expand Down

0 comments on commit a3c751c

Please sign in to comment.