| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
"""Parse and format date-time points and durations. |
| 18 |
|
| 19 |
SYNOPSIS |
| 20 |
# 1. Print date time point |
| 21 |
# 1.1 Current date time with an optional offset |
| 22 |
isodatetime [--offset=OFFSET] |
| 23 |
isodatetime now [--offset=OFFSET] |
| 24 |
# 1.2 A specific date time with an optional offset |
| 25 |
isodatetime 20380119T031407Z [--offset=OFFSET] |
| 26 |
# 1.4 A reference date time env var with an optional offset |
| 27 |
export ISODATETIMEREF=20371225T000000Z |
| 28 |
isodatetime ref [--offset=OFFSET] |
| 29 |
|
| 30 |
# 2. Print duration |
| 31 |
# 2.1 Between now (+ OFFSET1) and a future date time (+ OFFSET2) |
| 32 |
isodatetime now [--offset1=OFFSET1] 20380119T031407Z [--offset2=OFFSET2] |
| 33 |
# 2.2 Between a date time in the past and now |
| 34 |
isodatetime 19700101T000000Z now |
| 35 |
# 2.3 Between reference date time (+ OFFSET1) and a future date time |
| 36 |
export ISODATETIMEREF=20371225T000000Z |
| 37 |
isodatetime ref [--offset1=OFFSET1] 20380119T031407Z |
| 38 |
# 2.4 Between reference date time and now (+ OFFSET2) |
| 39 |
export ISODATETIMEREF=20371225T000000Z |
| 40 |
isodatetime ref now [--offset2=OFFSET2] |
| 41 |
# 2.5 Between a date time in the past and the reference date time |
| 42 |
export ISODATETIMEREF=20371225T000000Z |
| 43 |
isodatetime 19700101T000000Z ref |
| 44 |
# 2.6 Between 2 specific date times |
| 45 |
isodatetime 19700101T000000Z 20380119T031407Z |
| 46 |
|
| 47 |
# 3. Print ISO8601 duration as total amount of a unit |
| 48 |
# Into the total number of hours (H), minutes (M) or seconds (S) |
| 49 |
# it represents. |
| 50 |
isodatetime --as-total=s PT1H |
| 51 |
|
| 52 |
# 4. Print a number of time points in a ISO8601 recurrence |
| 53 |
# 4.1 Print N (default max 10) time points from start point |
| 54 |
isodatetime R/2020/P1Y |
| 55 |
isodatetime R5/2020/2024 |
| 56 |
# 4.2 Print N (default max 10) time points (in reverse) from end point |
| 57 |
isodatetime R/P1Y/2020 |
| 58 |
|
| 59 |
DESCRIPTION |
| 60 |
Parse and print 1. a date time point, 2. a duration or 3. a duration |
| 61 |
in a given unit. |
| 62 |
|
| 63 |
1. With 0 or 1 argument. Print the current or the specified date time |
| 64 |
point with an optional offset. |
| 65 |
|
| 66 |
2. With 2 arguments. Print the duration between the 2 arguments. |
| 67 |
The --as-total=UNIT option can also be used instead of the normal |
| 68 |
print format. |
| 69 |
|
| 70 |
3. With --as-total=UNIT option and a duration argument. Print the |
| 71 |
duration in the given UNIT. |
| 72 |
|
| 73 |
4. With a recurrence as argument, print N time points of the recurrence. |
| 74 |
The --max=N (default=10) can be used to control the maximum number |
| 75 |
of time points to print in the result. |
| 76 |
|
| 77 |
Note: Negative durations can be provided, e.g.: |
| 78 |
--offset "-PT1H" |
| 79 |
|
| 80 |
CALENDAR MODE |
| 81 |
The calendar mode is determined (in order) by: |
| 82 |
|
| 83 |
1. The `--calendar=MODE` option. |
| 84 |
2. The `ISODATETIMECALENDAR` environment variable. |
| 85 |
3. Default to "gregorian". |
| 86 |
|
| 87 |
ENVIRONMENT VARIABLES |
| 88 |
ISODATETIMECALENDAR=gregorian|360day|365day|366day |
| 89 |
Specify the calendar mode. |
| 90 |
ISODATETIMEREF |
| 91 |
Specify a reference date time, e.g. the curent task cycle point in |
| 92 |
a Cylc workflow. If the `ref` argument is used, the value of this |
| 93 |
environment variable is used by the command as the reference time |
| 94 |
instead of the current time. |
| 95 |
|
| 96 |
OFFSET FORMAT |
| 97 |
`OFFSET` must follow the ISO 8601 duration representations such as |
| 98 |
`PnW` or `PnYnMnDTnHnMnS - P` followed by a series of `nU` where `U` is |
| 99 |
the unit (`Y`, `M`, `D`, `H`, `M`, `S`) and `n` is a positive integer, |
| 100 |
where `T` delimits the date series from the time series if any time units |
| 101 |
are used. `n` may also have a decimal (e.g. `PT5.5M`) part for a unit |
| 102 |
provided no smaller units are supplied. It is not necessary to |
| 103 |
specify zero values for units. If `OFFSET` is negative, prefix a `-`. |
| 104 |
For example: |
| 105 |
|
| 106 |
* `P6D` - 6 day offset |
| 107 |
* `PT6H` - 6 hour offset |
| 108 |
* `PT1M` - 1 minute offset |
| 109 |
* `-PT1M` - (negative) 1 minute offset |
| 110 |
* `P3M` - 3 month offset |
| 111 |
* `P2W` - 2 week offset (note no other units may be combined with weeks) |
| 112 |
* `P2DT5.5H` - 2 day, 5.5 hour offset |
| 113 |
* `-P2YT4S` - (negative) 2 year, 4 second offset |
| 114 |
|
| 115 |
PARSE FORMAT |
| 116 |
The format for parsing a date time point should be compatible with the |
| 117 |
POSIX strptime template format (see the strptime command help), with the |
| 118 |
following subset supported across all date/time ranges: |
| 119 |
|
| 120 |
`%F`, `%H`, `%M`, `%S`, `%Y`, `%d`, `%j`, `%m`, `%s`, `%z` |
| 121 |
|
| 122 |
If not specified, the system will attempt to parse `DATE-TIME` using |
| 123 |
the following formats: |
| 124 |
|
| 125 |
* ctime: `%a %b %d %H:%M:%S %Y` |
| 126 |
* Unix date: `%a %b %d %H:%M:%S %Z %Y` |
| 127 |
* Basic ISO8601: `%Y-%m-%dT%H:%M:%S`, `%Y%m%dT%H%M%S` |
| 128 |
* Cylc: `%Y%m%d%H` |
| 129 |
|
| 130 |
If none of these match, the date time point will be parsed according to |
| 131 |
the full ISO 8601 date/time standard. |
| 132 |
|
| 133 |
PRINT FORMAT |
| 134 |
For printing a date time point, the print format will default to the same |
| 135 |
format as the parse format. Also supports the isodatetime library dump |
| 136 |
syntax for these operations which follows ISO 8601 example syntax - for |
| 137 |
example: |
| 138 |
|
| 139 |
* `CCYY-MM-DDThh:mm:ss` -> `1955-11-05T09:28:00`, |
| 140 |
* `CCYY` -> `1955`, |
| 141 |
* `CCYY-DDD` -> `1955-309`, |
| 142 |
* `CCYY-Www-D` -> `1955-W44-6`. |
| 143 |
|
| 144 |
Usage of this ISO 8601-like syntax should be as ISO 8601-compliant |
| 145 |
as possible. |
| 146 |
|
| 147 |
Note that specifying an explicit timezone in this format (e.g. |
| 148 |
`CCYY-MM-DDThh:mm:ss+0100` or `CCYYDDDThhmmZ` will automatically |
| 149 |
adapt the date/time to that timezone i.e. apply the correct |
| 150 |
hour/minute UTC offset. |
| 151 |
|
| 152 |
For printing a duration, the following can be used in format |
| 153 |
statements: |
| 154 |
|
| 155 |
* `y`: years |
| 156 |
* `m`: months |
| 157 |
* `d`: days |
| 158 |
* `h`: hours |
| 159 |
* `M`: minutes |
| 160 |
* `s`: seconds |
| 161 |
|
| 162 |
For example, for a duration `P57DT12H` - `y,m,d,h` -> `0,0,57,12` |
| 163 |
""" |
| 164 |
|
| 165 |
|
| 166 |
from argparse import ArgumentParser, RawDescriptionHelpFormatter |
| 167 |
import sys |
| 168 |
|
| 169 |
from . import __version__ |
| 170 |
from .datetimeoper import DateTimeOperator |
| 171 |
|
| 172 |
|
| 173 |
def parse_args(sys_args=None): |
| 174 |
arg_parser = ArgumentParser( |
| 175 |
prog='isodatetime', |
| 176 |
formatter_class=RawDescriptionHelpFormatter, |
| 177 |
description=__doc__) |
| 178 |
for o_args, o_kwargs in [ |
| 179 |
[ |
| 180 |
["items"], |
| 181 |
{ |
| 182 |
"help": ( |
| 183 |
"Time point, duration or recurrence string." |
| 184 |
" To read from stdin use '-'." |
| 185 |
), |
| 186 |
"metavar": "ITEM", |
| 187 |
"nargs": "*", |
| 188 |
}, |
| 189 |
], |
| 190 |
[ |
| 191 |
["--as-total"], |
| 192 |
{ |
| 193 |
"action": "store", |
| 194 |
"choices": ['H', 'M', 'S', 'h', 'm', 's'], |
| 195 |
"dest": "duration_print_format", |
| 196 |
"help": "Print duration as total of the specified unit.", |
| 197 |
"metavar": "UNIT", |
| 198 |
}, |
| 199 |
], |
| 200 |
[ |
| 201 |
["--calendar"], |
| 202 |
{ |
| 203 |
"action": "store", |
| 204 |
"choices": ["360day", "365day", "366day", "gregorian"], |
| 205 |
"help": "Set the calendar mode.", |
| 206 |
"metavar": "MODE", |
| 207 |
}, |
| 208 |
], |
| 209 |
[ |
| 210 |
["--max="], |
| 211 |
{ |
| 212 |
"action": "store", |
| 213 |
"default": 10, |
| 214 |
"dest": "max_results", |
| 215 |
"help": "Specify maximum number of results.", |
| 216 |
"metavar": "N", |
| 217 |
"type": int, |
| 218 |
}, |
| 219 |
], |
| 220 |
[ |
| 221 |
["--offset1", "--offset", "-s", "-1"], |
| 222 |
{ |
| 223 |
"action": "append", |
| 224 |
"dest": "offsets1", |
| 225 |
"metavar": "OFFSET", |
| 226 |
"help": "Specify offsets for 1st date time point.", |
| 227 |
}, |
| 228 |
], |
| 229 |
[ |
| 230 |
["--offset2", "-2"], |
| 231 |
{ |
| 232 |
"action": "append", |
| 233 |
"dest": "offsets2", |
| 234 |
"metavar": "OFFSET", |
| 235 |
"help": "Specify offsets for 2nd date time point.", |
| 236 |
}, |
| 237 |
], |
| 238 |
[ |
| 239 |
["--parse-format", "-p"], |
| 240 |
{ |
| 241 |
"metavar": "FORMAT", |
| 242 |
"help": "Specify the format for parsing inputs.", |
| 243 |
}, |
| 244 |
], |
| 245 |
[ |
| 246 |
["--print-format", "--format", "-f"], |
| 247 |
{ |
| 248 |
"metavar": "FORMAT", |
| 249 |
"help": "Specify the format for printing results.", |
| 250 |
}, |
| 251 |
], |
| 252 |
[ |
| 253 |
["--ref", "-R"], |
| 254 |
{ |
| 255 |
"action": "store", |
| 256 |
"dest": "ref_point_str", |
| 257 |
"help": "Specify a reference point string.", |
| 258 |
"metavar": "REF", |
| 259 |
}, |
| 260 |
], |
| 261 |
[ |
| 262 |
["--utc", "-u"], |
| 263 |
{ |
| 264 |
"action": "store_true", |
| 265 |
"default": False, |
| 266 |
"dest": "utc_mode", |
| 267 |
"help": "Switch on UTC mode.", |
| 268 |
}, |
| 269 |
], |
| 270 |
[ |
| 271 |
["--version", "-V"], |
| 272 |
{ |
| 273 |
"action": "store_true", |
| 274 |
"default": False, |
| 275 |
"dest": "version_mode", |
| 276 |
"help": "Print version and exit.", |
| 277 |
}, |
| 278 |
], |
| 279 |
]: |
| 280 |
arg_parser.add_argument(*o_args, **o_kwargs) |
| 281 |
|
| 282 |
if sys_args is None: |
| 283 |
sys_args = sys.argv[1:] |
| 284 |
sys_args = [ |
| 285 |
rf'\{arg}' if arg.startswith('-P') else arg |
| 286 |
for arg in sys_args |
| 287 |
] |
| 288 |
if hasattr(arg_parser, 'parse_intermixed_args'): |
| 289 |
args = arg_parser.parse_intermixed_args(sys_args) |
| 290 |
else: |
| 291 |
args = arg_parser.parse_args(sys_args) |
| 292 |
|
| 293 |
if args.offsets1: |
| 294 |
args.offsets1 = [item.replace("\\", "") for item in args.offsets1] |
| 295 |
|
| 296 |
if args.offsets2: |
| 297 |
args.offsets2 = [item.replace("\\", "") for item in args.offsets2] |
| 298 |
|
| 299 |
return args |
| 300 |
|
| 301 |
|
| 302 |
def main(sys_args=None): |
| 303 |
"""Implement "isodatetime" command.""" |
| 304 |
args = parse_args(sys_args) |
| 305 |
|
| 306 |
if args.version_mode: |
| 307 |
print(__version__) |
| 308 |
return |
| 309 |
date_time_oper = DateTimeOperator( |
| 310 |
parse_format=args.parse_format, |
| 311 |
utc_mode=args.utc_mode, |
| 312 |
calendar_mode=args.calendar, |
| 313 |
ref_point_str=args.ref_point_str) |
| 314 |
|
| 315 |
if list(args.items) == ['-']: |
| 316 |
args.items = sys.stdin.read().splitlines() |
| 317 |
|
| 318 |
try: |
| 319 |
if len(args.items) >= 2: |
| 320 |
out = date_time_oper.diff_time_point_strs( |
| 321 |
args.items[0], |
| 322 |
args.items[1], |
| 323 |
args.offsets1, |
| 324 |
args.offsets2, |
| 325 |
args.print_format, |
| 326 |
args.duration_print_format) |
| 327 |
elif args.items and args.items[0].startswith("R"): |
| 328 |
outs = [] |
| 329 |
for item in date_time_oper.iter_recurrence_str( |
| 330 |
args.items[0], |
| 331 |
args.print_format, |
| 332 |
): |
| 333 |
outs.append(item) |
| 334 |
if len(outs) >= args.max_results: |
| 335 |
break |
| 336 |
out = '\n'.join(outs) |
| 337 |
elif args.items and args.duration_print_format: |
| 338 |
out = date_time_oper.format_duration_str( |
| 339 |
args.items[0], args.duration_print_format) |
| 340 |
else: |
| 341 |
time_point_str = None |
| 342 |
if args.items: |
| 343 |
time_point_str = args.items[0] |
| 344 |
out = date_time_oper.process_time_point_str( |
| 345 |
time_point_str, args.offsets1, args.print_format) |
| 346 |
except ValueError as exc: |
| 347 |
sys.exit(exc) |
| 348 |
else: |
| 349 |
print(out) |
| 350 |
|
| 351 |
|
| 352 |
if __name__ == "__main__": |
| 353 |
main() |