Differences between revisions 13 and 14
Revision 13 as of 2018-10-11 15:09:58
Size: 5443
Comment:
Revision 14 as of 2018-10-11 15:11:13
Size: 5496
Comment:
Deletions are marked like this. Additions are marked like this.
Line 138: Line 138:
 * [[attachment:Python/Prog/Example/02/xxxx|xxxx]]  * [[attachment:..・Code/Ex_02_005a_date_and_time_builtin.py|Ex_02_005a_date_and_time_builtin.py]]

例題(5)


概要

[ edit ]

*


ポイント

[ edit ]

*


実行例

[ edit ]

  • 実行例(1) ... クリックすると拡大します

fig-01

*


本質的な部分 (授業中に順次公開します)

[ edit ]

import datetime

def today():
    return( datetime.date.today() )

import datetime

def now():
    return( datetime.datetime.now() )

import datetime

def today_str():
    date_of_today = today()
    return( str( date_of_today.year  ) + "" + 
            str( date_of_today.month ) + "" +
            str( date_of_today.day   ) + "" )

import datetime

def now_str():
    current_time = now()
    return( str( current_time.hour   ) + "" + 
            str( current_time.minute ) + "" + 
            str( current_time.second ) + "" + 
            str( current_time.microsecond ) + "μ秒" )
*


高度な話題 (授業中,もしくは授業後に順次公開します)

[ edit ]

  • 高度な話題へのリンク: 授業の流れを阻害しないように別ページにします

    • (後日の回の授業内容にはなる可能性がありますが,この回の授業内容には含めません).
    • 「文字列の書式化」とλ式(ラムダ式)に関するものです.


配布コード (授業中に順次公開します)

*

*

   1 import datetime
   2 
   3 def today():
   4     return( datetime.date.today() )

   1 import datetime
   2 
   3 def now():
   4     return( datetime.datetime.now() )

   1 import datetime
   2 
   3 def today_str():
   4     date_of_today = today()
   5     return( str( date_of_today.year  ) + "" + 
   6             str( date_of_today.month ) + "" +
   7             str( date_of_today.day   ) + "" )

   1 import datetime
   2 
   3 def now_str():
   4     current_time = now()
   5     return( str( current_time.hour   ) + "" + 
   6             str( current_time.minute ) + "" + 
   7             str( current_time.second ) + "" + 
   8             str( current_time.microsecond ) + "μ秒" )
  • 例題ファイルとしては,コメントを付け加えた,下記を配布します.
    • コメントはもう少し増えるかもしれません.

   1 #!/usr/bin/env python
   2 # -*- coding: utf-8 -*-
   3 # ==============================================================================
   4 # * Copyright (c) 2018 IIJIMA, Tadashi
   5 # *       (IIJIMA Laboratory, Dept. of Science and Technology, Keio University).
   6 # ==============================================================================
   7 # ソフトウェア工学[02] 例題[02]-(005a)  Ex_02_005a_date_and_time_builtin.py
   8 # Ex(Example) 02-005a: 【組込関数】 [日付]と[時刻]にかかわる組込み関数の使い方.
   9 #                               今日の[日付]を返す関数と,現在の[時刻]を返す関数を定義する. 
  10 #        2018-10-03 飯島 正 (iijima@ae.keio.ac.jp)
  11 # ==============================================================================
  12 # ----- 日付時刻を扱うためのdatetimeモジュールをインポートする -----
  13 import datetime
  14 # ==============================================================================
  15 # ===== 【関数定義】 ===== 
  16 def today():
  17     return( datetime.date.today() )
  18 # ==============================================================================
  19 # ===== 【関数定義】 ===== 
  20 def now():
  21     return( datetime.datetime.now() )
  22 # ==============================================================================
  23 # ===== 【関数定義】 ===== 
  24 def today_str():
  25     date_of_today = today()
  26     return( str( date_of_today.year  ) + "" + 
  27             str( date_of_today.month ) + "" +
  28             str( date_of_today.day   ) + "" )
  29 # ==============================================================================
  30 # ===== 
  31 def now_str():
  32     current_time = now()
  33     return( str( current_time.hour   ) + "" + 
  34             str( current_time.minute ) + "" + 
  35             str( current_time.second ) + "" + 
  36             str( current_time.microsecond ) + "μ秒" )
  37 # ==============================================================================
  38 # ===== 【メイン・プログラム】  =====
  39 # ----- オープニングメッセージ -----
  40 print( "標準モジュールdatetimeを使って,年月日からその日の曜日を求めます: " )
  41 
  42 # ----- 実行結果の表示 -----
  43 # ----- 今日の日付 -----
  44 print( "今日の日付は", today() )
  45 print( "今日の日付は", today_str() )
  46 
  47 # ----- 現在の日時 -----
  48 print( "今日の日時は", today() )
  49 
  50 # ----- 現在の時刻 -----
  51 print( "現在の時刻は", now_str() )
  52 # ==============================================================================


SoftEng: Python/Prog/Example/02/Ex_005a (last edited 2018-10-25 13:00:42 by TadashiIijima)